-
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.
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::LendingIterator; | ||
|
||
/// A lending iterator that yields the current count and the element during iteration. | ||
pub struct Enumerate<I> { | ||
count: usize, | ||
iter: I, | ||
} | ||
|
||
impl<I> Enumerate<I> { | ||
pub(crate) fn new(iter: I) -> Self { | ||
Enumerate { iter, count: 0 } | ||
} | ||
} | ||
|
||
impl<I: LendingIterator> LendingIterator for Enumerate<I> { | ||
type Item<'a> = (usize, I::Item<'a>) where Self: 'a; | ||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
let item = self.iter.next()?; | ||
let count = self.count; | ||
self.count += 1; | ||
Some((count, item)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::ToLendingIterator; | ||
|
||
// A non-fused iterator for testing that we match std's behavior | ||
struct Delay<I> { | ||
countdown: usize, | ||
iter: I, | ||
} | ||
|
||
impl<I> Delay<I> { | ||
fn new(countdown: usize, iter: I) -> Self { | ||
Delay { countdown, iter } | ||
} | ||
} | ||
|
||
// Generally we avoid implementing both Iterator and LendingIterator | ||
// for the same type. Here in testing the bounds of the arguments | ||
// are known not to collide. | ||
impl<I: LendingIterator> LendingIterator for Delay<I> { | ||
type Item<'a> = I::Item<'a> where Self: 'a; | ||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
if self.countdown == 0 { | ||
self.iter.next() | ||
} else { | ||
self.countdown -= 1; | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl<I: Iterator> Iterator for Delay<I> { | ||
type Item = I::Item; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.countdown == 0 { | ||
self.iter.next() | ||
} else { | ||
self.countdown -= 1; | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let first = Some((0, ())); | ||
let second = Some((1, ())); | ||
let mut delay_iter = Delay::new(1, std::iter::repeat(()).take(2)).enumerate(); | ||
let mut delay_lending = | ||
Delay::new(1, std::iter::repeat(()).into_lending().take(2)).enumerate(); | ||
|
||
assert_eq!((None, None), (delay_iter.next(), delay_lending.next())); | ||
assert_eq!((first, first), (delay_iter.next(), delay_lending.next())); | ||
assert_eq!((second, second), (delay_iter.next(), delay_lending.next())); | ||
assert_eq!((None, None), (delay_iter.next(), delay_lending.next())); | ||
} | ||
} |
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