diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-02-18 20:09:02 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-18 20:09:02 +0900 |
| commit | ae81241eae061ee9fb2af798b109508c27f208f1 (patch) | |
| tree | 069930e32fd1cce1611509e0ca9e5964bef3baaa /src | |
| parent | 6c4f8598b916087e2758d8c14c249ee5fa4fa0d5 (diff) | |
| parent | 84f3356e0dffc36f57d9be7902822db5d362e24f (diff) | |
| download | rust-ae81241eae061ee9fb2af798b109508c27f208f1.tar.gz rust-ae81241eae061ee9fb2af798b109508c27f208f1.zip | |
Rollup merge of #68597 - ollie27:skip_nth_last, r=Amanieu
Simplify `Skip::nth` and `Skip::last` implementations The main improvement is to make `last` no longer recursive.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/iter/adapters/mod.rs | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/src/libcore/iter/adapters/mod.rs b/src/libcore/iter/adapters/mod.rs index 7d10ef3d282..58606531a1a 100644 --- a/src/libcore/iter/adapters/mod.rs +++ b/src/libcore/iter/adapters/mod.rs @@ -1890,17 +1890,15 @@ where #[inline] fn nth(&mut self, n: usize) -> Option<I::Item> { // Can't just add n + self.n due to overflow. - if self.n == 0 { - self.iter.nth(n) - } else { + if self.n > 0 { let to_skip = self.n; self.n = 0; // nth(n) skips n+1 if self.iter.nth(to_skip - 1).is_none() { return None; } - self.iter.nth(n) } + self.iter.nth(n) } #[inline] @@ -1916,17 +1914,13 @@ where #[inline] fn last(mut self) -> Option<I::Item> { - if self.n == 0 { - self.iter.last() - } else { - let next = self.next(); - if next.is_some() { - // recurse. n should be 0. - self.last().or(next) - } else { - None + if self.n > 0 { + // nth(n) skips n+1 + if self.iter.nth(self.n - 1).is_none() { + return None; } } + self.iter.last() } #[inline] |
