-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(corelib): Iterator::enumerate (#7048)
- Loading branch information
Showing
4 changed files
with
91 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,41 @@ | ||
/// An iterator that yields the current count and the element during iteration. | ||
/// | ||
/// This `struct` is created by the [`enumerate`] method on [`Iterator`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`enumerate`]: Iterator::enumerate | ||
/// [`Iterator`]: core::iter::Iterator | ||
#[must_use] | ||
#[derive(Drop, Clone, Debug)] | ||
pub struct Enumerate<I> { | ||
iter: I, | ||
count: usize, | ||
} | ||
|
||
pub fn enumerated_iterator<I>(iter: I) -> Enumerate<I> { | ||
Enumerate { iter, count: 0 } | ||
} | ||
|
||
impl EnumerateIterator< | ||
I, T, +Iterator<I>[Item: T], +Destruct<I>, +Destruct<T>, | ||
> of Iterator<Enumerate<I>> { | ||
type Item = (usize, T); | ||
|
||
/// # Overflow Behavior | ||
/// | ||
/// The method does no guarding against overflows, so enumerating more than | ||
/// `Bounded::<usize>::MAX` elements will always panic. | ||
/// | ||
/// [`Bounded`]: core::num::traits::Bounded | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the index of the element overflows a `usize`. | ||
#[inline] | ||
fn next(ref self: Enumerate<I>) -> Option<Self::Item> { | ||
let a = self.iter.next()?; | ||
let i = self.count; | ||
self.count += 1; | ||
Option::Some((i, a)) | ||
} | ||
} |
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