Skip to content

Commit 376049b

Browse files
committed
Merge branch 'master' into fs-stream-step-by
2 parents e74c0ce + 2acc070 commit 376049b

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/stream/stream/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod next;
3535
mod nth;
3636
mod scan;
3737
mod skip;
38+
mod skip_while;
3839
mod step_by;
3940
mod take;
4041
mod zip;
@@ -43,6 +44,7 @@ pub use filter::Filter;
4344
pub use fuse::Fuse;
4445
pub use scan::Scan;
4546
pub use skip::Skip;
47+
pub use skip_while::SkipWhile;
4648
pub use step_by::StepBy;
4749
pub use take::Take;
4850
pub use zip::Zip;
@@ -729,7 +731,13 @@ pub trait Stream {
729731
Scan::new(self, initial_state, f)
730732
}
731733

732-
/// Creates a combinator that skips the first `n` elements.
734+
/// Combinator that `skip`s elements based on a predicate.
735+
///
736+
/// Takes a closure argument. It will call this closure on every element in
737+
/// the stream and ignore elements until it returns `false`.
738+
///
739+
/// After `false` is returned, `SkipWhile`'s job is over and all further
740+
/// elements in the strem are yeilded.
733741
///
734742
/// ## Examples
735743
///
@@ -739,6 +747,27 @@ pub trait Stream {
739747
/// use std::collections::VecDeque;
740748
/// use async_std::stream::Stream;
741749
///
750+
/// let a: VecDeque<_> = vec![-1i32, 0, 1].into_iter().collect();
751+
/// let mut s = a.skip_while(|x| x.is_negative());
752+
///
753+
/// assert_eq!(s.next().await, Some(0));
754+
/// assert_eq!(s.next().await, Some(1));
755+
/// assert_eq!(s.next().await, None);
756+
/// #
757+
/// # }) }
758+
/// ```
759+
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P, Self::Item>
760+
where
761+
Self: Sized,
762+
P: FnMut(&Self::Item) -> bool,
763+
{
764+
SkipWhile::new(self, predicate)
765+
}
766+
767+
/// Creates a combinator that skips the first `n` elements.
768+
///
769+
/// ## Examples
770+
///
742771
/// let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
743772
/// let mut skipped = s.skip(2);
744773
///

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+
/// A stream to skip elements of another stream based on a predicate.
8+
#[derive(Debug)]
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)