diff options
Diffstat (limited to 'libcore/iter')
| -rw-r--r-- | libcore/iter/iterator.rs | 5 | ||||
| -rw-r--r-- | libcore/iter/mod.rs | 22 |
2 files changed, 25 insertions, 2 deletions
diff --git a/libcore/iter/iterator.rs b/libcore/iter/iterator.rs index 2033ae5..71ca5cc 100644 --- a/libcore/iter/iterator.rs +++ b/libcore/iter/iterator.rs @@ -172,6 +172,7 @@ pub trait Iterator { /// assert_eq!(a.iter().count(), 5); /// ``` #[inline] + #[rustc_inherit_overflow_checks] #[stable(feature = "rust1", since = "1.0.0")] fn count(self) -> usize where Self: Sized { // Might overflow. @@ -214,7 +215,7 @@ pub trait Iterator { /// Like most indexing operations, the count starts from zero, so `nth(0)` /// returns the first value, `nth(1)` the second, and so on. /// - /// `nth()` will return `None` if `n` is larger than the length of the + /// `nth()` will return `None` if `n` is greater than or equal to the length of the /// iterator. /// /// # Examples @@ -237,7 +238,7 @@ pub trait Iterator { /// assert_eq!(iter.nth(1), None); /// ``` /// - /// Returning `None` if there are less than `n` elements: + /// Returning `None` if there are less than `n + 1` elements: /// /// ``` /// let a = [1, 2, 3]; diff --git a/libcore/iter/mod.rs b/libcore/iter/mod.rs index abc199c..ae1e311 100644 --- a/libcore/iter/mod.rs +++ b/libcore/iter/mod.rs @@ -510,6 +510,7 @@ impl<A, B> Iterator for Chain<A, B> where } #[inline] + #[rustc_inherit_overflow_checks] fn count(self) -> usize { match self.state { ChainState::Both => self.a.count() + self.b.count(), @@ -542,6 +543,23 @@ impl<A, B> Iterator for Chain<A, B> where } #[inline] + fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where + P: FnMut(&Self::Item) -> bool, + { + match self.state { + ChainState::Both => match self.a.find(&mut predicate) { + None => { + self.state = ChainState::Back; + self.b.find(predicate) + } + v => v + }, + ChainState::Front => self.a.find(predicate), + ChainState::Back => self.b.find(predicate), + } + } + + #[inline] fn last(self) -> Option<A::Item> { match self.state { ChainState::Both => { @@ -915,6 +933,7 @@ impl<I> Iterator for Enumerate<I> where I: Iterator { /// /// Might panic if the index of the element overflows a `usize`. #[inline] + #[rustc_inherit_overflow_checks] fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> { self.iter.next().map(|a| { let ret = (self.count, a); @@ -930,6 +949,7 @@ impl<I> Iterator for Enumerate<I> where I: Iterator { } #[inline] + #[rustc_inherit_overflow_checks] fn nth(&mut self, n: usize) -> Option<(usize, I::Item)> { self.iter.nth(n).map(|a| { let i = self.count + n; @@ -991,6 +1011,7 @@ impl<I: Iterator> Iterator for Peekable<I> { } #[inline] + #[rustc_inherit_overflow_checks] fn count(self) -> usize { (if self.peeked.is_some() { 1 } else { 0 }) + self.iter.count() } @@ -1108,6 +1129,7 @@ impl<I: Iterator> Peekable<I> { /// ``` #[unstable(feature = "peekable_is_empty", issue = "32111")] #[inline] + #[rustc_deprecated(since = "1.10.0", reason = "replaced by .peek().is_none()")] pub fn is_empty(&mut self) -> bool { self.peek().is_none() } |