Skip to content

Commit 84f3356

Browse files
committed
Simplify Skip::nth and Skip::last implementations
The main improvement is to make `last` no longer recursive.
1 parent 80a65bc commit 84f3356

File tree

1 file changed

+7
-13
lines changed
  • src/libcore/iter/adapters

1 file changed

+7
-13
lines changed

src/libcore/iter/adapters/mod.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1801,17 +1801,15 @@ where
18011801
#[inline]
18021802
fn nth(&mut self, n: usize) -> Option<I::Item> {
18031803
// Can't just add n + self.n due to overflow.
1804-
if self.n == 0 {
1805-
self.iter.nth(n)
1806-
} else {
1804+
if self.n > 0 {
18071805
let to_skip = self.n;
18081806
self.n = 0;
18091807
// nth(n) skips n+1
18101808
if self.iter.nth(to_skip - 1).is_none() {
18111809
return None;
18121810
}
1813-
self.iter.nth(n)
18141811
}
1812+
self.iter.nth(n)
18151813
}
18161814

18171815
#[inline]
@@ -1827,17 +1825,13 @@ where
18271825

18281826
#[inline]
18291827
fn last(mut self) -> Option<I::Item> {
1830-
if self.n == 0 {
1831-
self.iter.last()
1832-
} else {
1833-
let next = self.next();
1834-
if next.is_some() {
1835-
// recurse. n should be 0.
1836-
self.last().or(next)
1837-
} else {
1838-
None
1828+
if self.n > 0 {
1829+
// nth(n) skips n+1
1830+
if self.iter.nth(self.n - 1).is_none() {
1831+
return None;
18391832
}
18401833
}
1834+
self.iter.last()
18411835
}
18421836

18431837
#[inline]

0 commit comments

Comments
 (0)