Skip to content

Commit

Permalink
Reduce genericity in Copied and Cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Aug 12, 2019
1 parent 27ddbf4 commit d940ddf
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/libcore/iter/adapters/mod.rs
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down

0 comments on commit d940ddf

Please sign in to comment.