-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add PeekableIterator trait
#144935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wmstack
wants to merge
23
commits into
rust-lang:main
Choose a base branch
from
wmstack:PeekableIterator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add PeekableIterator trait
#144935
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a2625c9
Add peekable_iterator
wmstack 1a92c37
Implement peekable for slice::iter
wmstack 332bddf
Implement new PeekableIterator for Iter
wmstack e049298
Replace T with U
wmstack 7eb6bbf
Apply suggestions from code review
wmstack 26dacee
Update next_if to use the bool
wmstack 8b0efa3
Update next_if logic to handle Some(false) correctly
wmstack 9b9ab45
Implement PeekableIterator for Chars
wmstack f18ad01
Peek by cloning on Chars
wmstack 74ad80c
fix input parameter in Chars
wmstack 10d9290
Implement PeekableIterator for IntoIter
wmstack a279e8c
Forget temporary in iter_inner
wmstack 750d963
Implement PeekableIterator for Peekable
wmstack 8243b79
Remove as_ref()
wmstack 25fabb4
Remove unneeded transmute
wmstack cd8d97c
Use assume_init_ref directly from slice
wmstack 308da3e
fix peek_with in macros
wmstack 078ecf1
Reduce unsafe scope with Iter/IterMut
wmstack 3ceb540
remove peek_map, add examples
wmstack 512e44f
Fix trailing whitespace, punctuation
wmstack 547d7ac
fix example chars
wmstack 2f1e47b
add 0 case
wmstack 22a1c0f
fix example syntax
wmstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| #[unstable(feature = "peekable_iterator", issue = "132973")] | ||
| /// Iterators which inherently support peeking without needing to be wrapped by a `Peekable`. | ||
| pub trait PeekableIterator: Iterator { | ||
| /// Executes the closure with a reference to the `next()` value without advancing the iterator. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Basic usage: | ||
| /// ``` | ||
| /// #![feature(peekable_iterator)] | ||
| /// use std::iter::PeekableIterator; | ||
| /// | ||
| /// let mut vals = [0, 1, 2].into_iter(); | ||
| /// | ||
| /// assert_eq!(vals.peek_with(|x| x.copied()), Some(0)); | ||
| /// // element is not consumed | ||
| /// assert_eq!(vals.next(), Some(0)); | ||
| /// | ||
| /// // examine the pending element | ||
| /// assert_eq!(vals.peek_with(|x| x.copied()), Some(1)); | ||
| /// assert_eq!(vals.next(), Some(1)); | ||
| /// | ||
| /// // determine if the iterator has an element without advancing | ||
| /// assert_eq!(vals.peek_with(|x| x.is_some()), false); | ||
| /// assert_eq!(vals.next(), Some(2)); | ||
| /// | ||
| /// // exhausted iterator | ||
| /// assert_eq!(vals.next(), None); | ||
| /// assert_eq!(vals.peek_with(|x| x.copied()), None); | ||
| /// ``` | ||
| fn peek_with<T>(&mut self, func: impl for<'a> FnOnce(Option<&'a Self::Item>) -> T) -> T; | ||
|
|
||
| /// Returns the `next()` element if the given predicate holds true. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Basic usage: | ||
| /// ``` | ||
| /// #![feature(peekable_iterator)] | ||
| /// use std::iter::PeekableIterator; | ||
| /// fn parse_number(s: &str) -> u32 { | ||
| /// if s == "0" { | ||
| /// return 0 | ||
| /// } | ||
| /// | ||
| /// let mut c = s.chars(); | ||
| /// | ||
| /// let base = if c.next_if_eq(&'0').is_some() { | ||
| /// match c.next_if(|c| "oxb".contains(*c)) { | ||
| /// Some('x') => 16, | ||
| /// Some('b') => 2, | ||
| /// _ => 8 | ||
| /// } | ||
| /// } else { | ||
| /// 10 | ||
| /// }; | ||
| /// | ||
| /// u32::from_str_radix(c.as_str(), base).unwrap() | ||
| /// } | ||
| /// | ||
| /// assert_eq!(parse_number("055"), 45); | ||
| /// assert_eq!(parse_number("0o42"), 34); | ||
| /// assert_eq!(parse_number("0x11"), 17); | ||
| /// assert_eq!(parse_number("0"), 0); | ||
| /// ``` | ||
| /// | ||
| fn next_if(&mut self, func: impl FnOnce(&Self::Item) -> bool) -> Option<Self::Item> { | ||
| match self.peek_with(|x| x.map(|y| func(y))) { | ||
| Some(true) => self.next(), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Moves forward and return the `next()` item if it is equal to the expected value. | ||
| fn next_if_eq<T>(&mut self, expected: &T) -> Option<Self::Item> | ||
| where | ||
| Self::Item: PartialEq<T>, | ||
| T: ?Sized, | ||
| { | ||
| self.next_if(|x| x == expected) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.