-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
616: Add `take_while_inclusive` method r=jswrenn a=e-rhodes Adds `take_while_inclusive` method that returns elements as long as a predicate is satisfied, but including the first element that doesn't satisfy the predicate. First discussed addition to std in <rust-lang/rust#62208>. Closes #597. Co-authored-by: Erik Rhodes <erik@space-nav.com>
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 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
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,68 @@ | ||
use core::iter::FusedIterator; | ||
use std::fmt; | ||
|
||
/// An iterator adaptor that consumes elements while the given predicate is | ||
/// `true`, including the element for which the predicate first returned | ||
/// `false`. | ||
/// | ||
/// See [`.take_while_inclusive()`](crate::Itertools::take_while_inclusive) | ||
/// for more information. | ||
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] | ||
pub struct TakeWhileInclusive<'a, I: 'a, F> { | ||
iter: &'a mut I, | ||
predicate: F, | ||
done: bool, | ||
} | ||
|
||
impl<'a, I, F> TakeWhileInclusive<'a, I, F> | ||
where | ||
I: Iterator, | ||
F: FnMut(&I::Item) -> bool, | ||
{ | ||
/// Create a new [`TakeWhileInclusive`] from an iterator and a predicate. | ||
pub fn new(iter: &'a mut I, predicate: F) -> Self { | ||
Self { iter, predicate, done: false} | ||
} | ||
} | ||
|
||
impl<'a, I, F> fmt::Debug for TakeWhileInclusive<'a, I, F> | ||
where I: Iterator + fmt::Debug, | ||
{ | ||
debug_fmt_fields!(TakeWhileInclusive, iter); | ||
} | ||
|
||
impl<'a, I, F> Iterator for TakeWhileInclusive<'a, I, F> | ||
where | ||
I: Iterator, | ||
F: FnMut(&I::Item) -> bool | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.done { | ||
None | ||
} else { | ||
self.iter.next().map(|item| { | ||
if !(self.predicate)(&item) { | ||
self.done = true; | ||
} | ||
item | ||
}) | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
if self.done { | ||
(0, Some(0)) | ||
} else { | ||
(0, self.iter.size_hint().1) | ||
} | ||
} | ||
} | ||
|
||
impl<I, F> FusedIterator for TakeWhileInclusive<'_, I, F> | ||
where | ||
I: Iterator, | ||
F: FnMut(&I::Item) -> bool | ||
{ | ||
} |