Reduce genericity in Copied and Cloned

This commit is contained in:
Josh Stone 2019-07-11 15:27:56 -07:00
parent 27ddbf4d16
commit d940ddf8f5

View File

@ -143,6 +143,18 @@ impl<I> Copied<I> {
}
}
fn copy_fold<T: Copy, Acc>(
mut f: impl FnMut(Acc, T) -> Acc,
) -> impl FnMut(Acc, &T) -> Acc {
move |acc, &elt| f(acc, elt)
}
fn copy_try_fold<T: Copy, Acc, R>(
mut f: impl FnMut(Acc, T) -> R,
) -> impl FnMut(Acc, &T) -> R {
move |acc, &elt| f(acc, elt)
}
#[stable(feature = "iter_copied", since = "1.36.0")]
impl<'a, I, T: 'a> Iterator for Copied<I>
where I: Iterator<Item=&'a T>, T: Copy
@ -157,16 +169,16 @@ impl<'a, I, T: 'a> Iterator for Copied<I>
self.it.size_hint()
}
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.it.try_fold(init, move |acc, &elt| f(acc, elt))
self.it.try_fold(init, copy_try_fold(f))
}
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.fold(init, move |acc, &elt| f(acc, elt))
self.it.fold(init, copy_fold(f))
}
}
@ -178,16 +190,16 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
self.it.next_back().copied()
}
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.it.try_rfold(init, move |acc, &elt| f(acc, elt))
self.it.try_rfold(init, copy_try_fold(f))
}
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.rfold(init, move |acc, &elt| f(acc, elt))
self.it.rfold(init, copy_fold(f))
}
}
@ -248,6 +260,12 @@ impl<I> Cloned<I> {
}
}
fn clone_try_fold<T: Clone, Acc, R>(
mut f: impl FnMut(Acc, T) -> R,
) -> impl FnMut(Acc, &T) -> R {
move |acc, elt| f(acc, elt.clone())
}
#[stable(feature = "iter_cloned", since = "1.1.0")]
impl<'a, I, T: 'a> Iterator for Cloned<I>
where I: Iterator<Item=&'a T>, T: Clone
@ -262,16 +280,16 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
self.it.size_hint()
}
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.it.try_fold(init, move |acc, elt| f(acc, elt.clone()))
self.it.try_fold(init, clone_try_fold(f))
}
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.fold(init, move |acc, elt| f(acc, elt.clone()))
self.it.map(T::clone).fold(init, f)
}
}
@ -283,16 +301,16 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
self.it.next_back().cloned()
}
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.it.try_rfold(init, move |acc, elt| f(acc, elt.clone()))
self.it.try_rfold(init, clone_try_fold(f))
}
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,
{
self.it.rfold(init, move |acc, elt| f(acc, elt.clone()))
self.it.map(T::clone).rfold(init, f)
}
}