-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ratmice/skip
add skip
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::LendingIterator; | ||
|
||
/// A lending iterator that skips over the first `n` items of `iter`. | ||
#[derive(Clone)] | ||
pub struct Skip<I> { | ||
n: usize, | ||
iter: I, | ||
} | ||
|
||
impl<I> Skip<I> { | ||
pub(crate) fn new(iter: I, n: usize) -> Skip<I> { | ||
Skip { iter, n } | ||
} | ||
} | ||
|
||
impl<I> LendingIterator for Skip<I> | ||
where | ||
I: LendingIterator, | ||
{ | ||
type Item<'a> = I::Item<'a> where I: 'a; | ||
|
||
fn next(&mut self) -> Option<I::Item<'_>> { | ||
if self.n > 0 { | ||
self.iter.nth(core::mem::take(&mut self.n)) | ||
} else { | ||
self.iter.next() | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (lower, upper) = self.iter.size_hint(); | ||
|
||
let lower = lower.saturating_sub(self.n); | ||
let upper = upper.map(|x| x.saturating_sub(self.n)); | ||
(lower, upper) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::ToLendingIterator; | ||
#[test] | ||
fn test() { | ||
assert_eq!((0..5).into_lending().skip(1).nth(1), (0..5).skip(1).nth(1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters