Properly forward ByRefSized::fold
to the inner iterator
This commit is contained in:
parent
76c427d6e2
commit
7680c8b690
3 changed files with 38 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
||||||
use crate::ops::Try;
|
use crate::ops::{NeverShortCircuit, Try};
|
||||||
|
|
||||||
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
|
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
|
||||||
///
|
///
|
||||||
|
@ -8,28 +8,31 @@ use crate::ops::Try;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ByRefSized<'a, I>(pub &'a mut I);
|
pub struct ByRefSized<'a, I>(pub &'a mut I);
|
||||||
|
|
||||||
|
// The following implementations use UFCS-style, rather than trusting autoderef,
|
||||||
|
// to avoid accidentally calling the `&mut Iterator` implementations.
|
||||||
|
|
||||||
#[unstable(feature = "std_internals", issue = "none")]
|
#[unstable(feature = "std_internals", issue = "none")]
|
||||||
impl<I: Iterator> Iterator for ByRefSized<'_, I> {
|
impl<I: Iterator> Iterator for ByRefSized<'_, I> {
|
||||||
type Item = I::Item;
|
type Item = I::Item;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.0.next()
|
I::next(self.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
self.0.size_hint()
|
I::size_hint(self.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
|
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
|
||||||
self.0.advance_by(n)
|
I::advance_by(self.0, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
self.0.nth(n)
|
I::nth(self.0, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -37,7 +40,8 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
|
||||||
where
|
where
|
||||||
F: FnMut(B, Self::Item) -> B,
|
F: FnMut(B, Self::Item) -> B,
|
||||||
{
|
{
|
||||||
self.0.fold(init, f)
|
// `fold` needs ownership, so this can't forward directly.
|
||||||
|
I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -46,7 +50,7 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
|
||||||
F: FnMut(B, Self::Item) -> R,
|
F: FnMut(B, Self::Item) -> R,
|
||||||
R: Try<Output = B>,
|
R: Try<Output = B>,
|
||||||
{
|
{
|
||||||
self.0.try_fold(init, f)
|
I::try_fold(self.0, init, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,17 +58,17 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
|
||||||
impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
|
impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next_back(&mut self) -> Option<Self::Item> {
|
fn next_back(&mut self) -> Option<Self::Item> {
|
||||||
self.0.next_back()
|
I::next_back(self.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
|
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
|
||||||
self.0.advance_back_by(n)
|
I::advance_back_by(self.0, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||||
self.0.nth_back(n)
|
I::nth_back(self.0, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -72,7 +76,8 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
|
||||||
where
|
where
|
||||||
F: FnMut(B, Self::Item) -> B,
|
F: FnMut(B, Self::Item) -> B,
|
||||||
{
|
{
|
||||||
self.0.rfold(init, f)
|
// `rfold` needs ownership, so this can't forward directly.
|
||||||
|
I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -81,6 +86,6 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
|
||||||
F: FnMut(B, Self::Item) -> R,
|
F: FnMut(B, Self::Item) -> R,
|
||||||
R: Try<Output = B>,
|
R: Try<Output = B>,
|
||||||
{
|
{
|
||||||
self.0.try_rfold(init, f)
|
I::try_rfold(self.0, init, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
library/core/tests/iter/adapters/by_ref_sized.rs
Normal file
20
library/core/tests/iter/adapters/by_ref_sized.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
use core::iter::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_iterator_by_ref_sized() {
|
||||||
|
let a = ['a', 'b', 'c', 'd'];
|
||||||
|
|
||||||
|
let mut s = String::from("Z");
|
||||||
|
let mut it = a.iter().copied();
|
||||||
|
ByRefSized(&mut it).take(2).for_each(|x| s.push(x));
|
||||||
|
assert_eq!(s, "Zab");
|
||||||
|
ByRefSized(&mut it).fold((), |(), x| s.push(x));
|
||||||
|
assert_eq!(s, "Zabcd");
|
||||||
|
|
||||||
|
let mut s = String::from("Z");
|
||||||
|
let mut it = a.iter().copied();
|
||||||
|
ByRefSized(&mut it).rev().take(2).for_each(|x| s.push(x));
|
||||||
|
assert_eq!(s, "Zdc");
|
||||||
|
ByRefSized(&mut it).rfold((), |(), x| s.push(x));
|
||||||
|
assert_eq!(s, "Zdcba");
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
mod array_chunks;
|
mod array_chunks;
|
||||||
|
mod by_ref_sized;
|
||||||
mod chain;
|
mod chain;
|
||||||
mod cloned;
|
mod cloned;
|
||||||
mod copied;
|
mod copied;
|
||||||
|
|
Loading…
Reference in a new issue