-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add Peekable::until #75540
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
Add Peekable::until #75540
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1687,6 +1687,110 @@ impl<I: Iterator> Peekable<I> { | |||||||
{ | ||||||||
self.next_if(|next| next == expected) | ||||||||
} | ||||||||
|
||||||||
/// Creates an iterator that consumes elements until predicate is | ||||||||
/// true, without consuming the last matching element. | ||||||||
/// | ||||||||
/// `until()` takes a closure as an argument. It will call this | ||||||||
/// closure on each element of the iterator, and consume elements | ||||||||
/// until it returns true. | ||||||||
/// | ||||||||
/// After true is returned, until()'s job is over, and the iterator | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
/// is fused. | ||||||||
/// | ||||||||
/// This is the exact opposite of [`skip_while`]. | ||||||||
/// | ||||||||
/// # Example | ||||||||
/// Consume numbers until you find a '5'. | ||||||||
/// ``` | ||||||||
/// #![feature(peekable_next_if)] | ||||||||
/// let mut iter = (0..10).peekable(); | ||||||||
/// assert_eq!(iter.until(|&x| x == 5).collect::<String>(), "1234".to_string()); | ||||||||
apelisse marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
/// assert_eq!(iter.next(), Some(5)); | ||||||||
/// ``` | ||||||||
/// [`skip_while`]: trait.Iterator.html#method.skip_while | ||||||||
#[unstable(feature = "peekable_next_if", issue = "72480")] | ||||||||
pub fn until<P: FnMut(&I::Item) -> bool>(&mut self, func: P) -> Until<'_, I, P> { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't think be |
||||||||
Until::new(self, func) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/// An iterator that iterates over elements until `predicate` returns `false`. | ||||||||
/// | ||||||||
/// This `struct` is created by the [`until`] method on [`Peekable`]. See its | ||||||||
/// documentation for more. | ||||||||
/// | ||||||||
/// [`until`]: trait.Peekable.html#until | ||||||||
/// [`Iterator`]: trait.Iterator.html | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cross doc link, we probably don't need this. @jyn514 would be happy if you remove this |
||||||||
#[must_use = "iterators are lazy and do nothing unless consumed"] | ||||||||
#[unstable(feature = "peekable_next_if", issue = "72480")] | ||||||||
pub struct Until<'a, I, P> | ||||||||
where | ||||||||
I: Iterator, | ||||||||
{ | ||||||||
peekable: &'a mut Peekable<I>, | ||||||||
flag: bool, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove flag? And use |
||||||||
predicate: P, | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
impl<'a, I, P> Until<'a, I, P> | ||||||||
where | ||||||||
I: Iterator, | ||||||||
{ | ||||||||
fn new(peekable: &'a mut Peekable<I>, predicate: P) -> Self { | ||||||||
Until { peekable, flag: false, predicate } | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
#[stable(feature = "core_impl_debug", since = "1.9.0")] | ||||||||
impl<I, P> fmt::Debug for Until<'_, I, P> | ||||||||
where | ||||||||
I: fmt::Debug + Iterator, | ||||||||
I::Item: fmt::Debug, | ||||||||
{ | ||||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||||
f.debug_struct("Until").field("peekable", self.peekable).field("flag", &self.flag).finish() | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
#[unstable(feature = "peekable_next_if", issue = "72480")] | ||||||||
impl<I: Iterator, P> Iterator for Until<'_, I, P> | ||||||||
where | ||||||||
P: FnMut(&I::Item) -> bool, | ||||||||
{ | ||||||||
type Item = I::Item; | ||||||||
|
||||||||
#[inline] | ||||||||
fn next(&mut self) -> Option<I::Item> { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
impl<I: Iterator, P> Iterator for SkipWhile<I, P>
where
P: FnMut(&I::Item) -> bool,
{
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<I::Item> {
fn check<'a, T>(
flag: &'a mut bool,
pred: &'a mut impl FnMut(&T) -> bool,
) -> impl FnMut(&T) -> bool + 'a {
move |x| {
if *flag || !pred(x) {
*flag = true;
true
} else {
false
}
}
}
let flag = &mut self.flag;
let pred = &mut self.predicate;
self.iter.find(check(flag, pred))
} |
||||||||
if self.flag { | ||||||||
return None; | ||||||||
} | ||||||||
match self.peekable.peek() { | ||||||||
Some(matched) => { | ||||||||
if (self.predicate)(&matched) { | ||||||||
// matching value is not consumed. | ||||||||
self.flag = true; | ||||||||
None | ||||||||
} else { | ||||||||
self.peekable.next() | ||||||||
} | ||||||||
} | ||||||||
None => None, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
#[inline] | ||||||||
fn size_hint(&self) -> (usize, Option<usize>) { | ||||||||
let (_, upper) = self.peekable.size_hint(); | ||||||||
(0, upper) // can't know a lower bound, due to the predicate | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we override |
||||||||
} | ||||||||
|
||||||||
#[stable(feature = "fused", since = "1.26.0")] | ||||||||
impl<I, P> FusedIterator for Until<'_, I, P> | ||||||||
where | ||||||||
I: FusedIterator, | ||||||||
P: FnMut(&I::Item) -> bool, | ||||||||
{ | ||||||||
} | ||||||||
|
||||||||
/// An iterator that rejects elements while `predicate` returns `true`. | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.