-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
17
commits into
rust-lang:master
Choose a base branch
from
wmstack:PeekableIterator
base: master
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.
+93
−5
Open
Add PeekableIterator
trait
#144935
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
38e446a
Add peekable_iterator
wmstack 8d2bdb0
Implement peekable for slice::iter
wmstack ecc7b86
Implement new PeekableIterator for Iter
wmstack 3dd13bc
Replace T with U
wmstack bf0193c
Apply suggestions from code review
wmstack dc33dd2
Update next_if to use the bool
wmstack 5892463
Update next_if logic to handle Some(false) correctly
wmstack ec565f6
Implement PeekableIterator for Chars
wmstack ff668ec
Peek by cloning on Chars
wmstack 56d74c6
fix input parameter in Chars
wmstack c552fc8
Implement PeekableIterator for IntoIter
wmstack 6fc70f2
Forget temporary in iter_inner
wmstack 54ad568
Implement PeekableIterator for Peekable
wmstack 14a6ecd
Remove as_ref()
wmstack 746c699
Remove unneeded transmute
wmstack 2fa7343
Use assume_init_ref directly from slice
wmstack a2ce876
fix peek_with in macros
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,28 @@ | ||
#[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. | ||
fn peek_with<T>(&mut self, func: impl for<'a> FnOnce(Option<&'a Self::Item>) -> T) -> T; | ||
|
||
/// Executes the closure on the `next()` element without advancing the iterator, or returns `None` if the iterator is exhausted. | ||
fn peek_map<T>(&mut self, func: impl for<'a> FnOnce(&'a Self::Item) -> T) -> Option<T> { | ||
self.peek_with(|x| x.map(|y| func(y))) | ||
} | ||
|
||
/// Returns the `next()` element if the given predicate holds true. | ||
fn next_if(&mut self, func: impl FnOnce(&Self::Item) -> bool) -> Option<Self::Item> { | ||
match self.peek_map(func) { | ||
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.