Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hide the Iterator specialization behind a trait
  • Loading branch information
jonas-schievink committed Oct 5, 2019
commit 02f36e52a656f1baa717538e18ae96137cbc83f9
27 changes: 22 additions & 5 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,16 +871,33 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
fn nth(&mut self, n: usize) -> Option<I::Item> {
(**self).nth(n)
}
fn last(self) -> Option<I::Item> {
BoxIter::last(self)
}
}

trait BoxIter {
type Item;
fn last(self) -> Option<Self::Item>;
}

impl<I: Iterator + ?Sized> BoxIter for Box<I> {
type Item = I::Item;
default fn last(self) -> Option<I::Item> {
let mut last = None;
for x in self { last = Some(x); }
last
#[inline]
fn some<T>(_: Option<T>, x: T) -> Option<T> {
Some(x)
}

self.fold(None, some)
}
}

/// Specialization for sized `I`s that uses `I`s implementation of `last()`
/// instead of the default.
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: Iterator + Sized> Iterator for Box<I> {
fn last(self) -> Option<I::Item> where I: Sized {
impl<I: Iterator> BoxIter for Box<I> {
fn last(self) -> Option<I::Item> {
(*self).last()
}
}
Expand Down