Skip to content

Commit f9f97c4

Browse files
committed
adds stream::skip_while combinator
1 parent 55ea367 commit f9f97c4

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/stream/stream/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod min_by;
3333
mod next;
3434
mod nth;
3535
mod scan;
36+
mod skip_while;
3637
mod take;
3738
mod zip;
3839

@@ -51,6 +52,7 @@ use fold::FoldFuture;
5152
use min_by::MinByFuture;
5253
use next::NextFuture;
5354
use nth::NthFuture;
55+
use skip_while::SkipWhile;
5456

5557
use std::cmp::Ordering;
5658
use std::marker::PhantomData;
@@ -661,6 +663,38 @@ pub trait Stream {
661663
Scan::new(self, initial_state, f)
662664
}
663665

666+
/// Combinator that `skip`s elements based on a predicate.
667+
///
668+
/// Takes a closure argument. It will call this closure on every element in
669+
/// the stream and ignore elements until it returns `false`.
670+
///
671+
/// After `false` is returned, `SkipWhile`'s job is over and all further
672+
/// elements in the strem are yeilded.
673+
///
674+
/// ## Examples
675+
/// ```
676+
/// # fn main() { async_std::task::block_on(async {
677+
/// #
678+
/// use std::collections::VecDeque;
679+
/// use async_std::stream::Stream;
680+
///
681+
/// let a: VecDeque<_> = vec![-1i32, 0, 1].into_iter().collect();
682+
/// let mut s = a.skip_while(|x| x.is_negative());
683+
///
684+
/// assert_eq!(s.next().await, Some(0));
685+
/// assert_eq!(s.next().await, Some(1));
686+
/// assert_eq!(s.next().await, None);
687+
/// #
688+
/// # }) }
689+
/// ```
690+
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P, Self::Item>
691+
where
692+
Self: Sized,
693+
P: FnMut(&Self::Item) -> bool,
694+
{
695+
SkipWhile::new(self, predicate)
696+
}
697+
664698
/// 'Zips up' two streams into a single stream of pairs.
665699
///
666700
/// `zip()` returns a new stream that will iterate over two other streams, returning a tuple

src/stream/stream/skip_while.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::marker::PhantomData;
2+
use std::pin::Pin;
3+
4+
use crate::stream::Stream;
5+
use crate::task::{Context, Poll};
6+
7+
#[doc(hidden)]
8+
#[allow(missing_debug_implementations)]
9+
pub struct SkipWhile<S, P, T> {
10+
stream: S,
11+
predicate: Option<P>,
12+
__t: PhantomData<T>,
13+
}
14+
15+
impl<S, P, T> SkipWhile<S, P, T> {
16+
pin_utils::unsafe_pinned!(stream: S);
17+
pin_utils::unsafe_unpinned!(predicate: Option<P>);
18+
19+
pub(crate) fn new(stream: S, predicate: P) -> Self {
20+
SkipWhile {
21+
stream,
22+
predicate: Some(predicate),
23+
__t: PhantomData,
24+
}
25+
}
26+
}
27+
28+
impl<S, P> Stream for SkipWhile<S, P, S::Item>
29+
where
30+
S: Stream,
31+
P: FnMut(&S::Item) -> bool,
32+
{
33+
type Item = S::Item;
34+
35+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
36+
loop {
37+
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
38+
39+
match next {
40+
Some(v) => match self.as_mut().predicate() {
41+
Some(p) => match p(&v) {
42+
true => (),
43+
false => {
44+
*self.as_mut().predicate() = None;
45+
return Poll::Ready(Some(v));
46+
}
47+
},
48+
None => return Poll::Ready(Some(v)),
49+
},
50+
None => return Poll::Ready(None),
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)