forked from mirror/async-std
adds stream::skip combinator
This commit is contained in:
parent
55ea367415
commit
570329b176
2 changed files with 68 additions and 0 deletions
|
@ -33,6 +33,7 @@ mod min_by;
|
||||||
mod next;
|
mod next;
|
||||||
mod nth;
|
mod nth;
|
||||||
mod scan;
|
mod scan;
|
||||||
|
mod skip;
|
||||||
mod take;
|
mod take;
|
||||||
mod zip;
|
mod zip;
|
||||||
|
|
||||||
|
@ -51,6 +52,7 @@ use fold::FoldFuture;
|
||||||
use min_by::MinByFuture;
|
use min_by::MinByFuture;
|
||||||
use next::NextFuture;
|
use next::NextFuture;
|
||||||
use nth::NthFuture;
|
use nth::NthFuture;
|
||||||
|
use skip::Skip;
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
@ -661,6 +663,31 @@ pub trait Stream {
|
||||||
Scan::new(self, initial_state, f)
|
Scan::new(self, initial_state, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a combinator that skips the first `n` elements.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
/// use async_std::stream::Stream;
|
||||||
|
///
|
||||||
|
/// let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
|
||||||
|
/// let mut skipped = s.skip(2);
|
||||||
|
///
|
||||||
|
/// assert_eq!(skipped.next().await, Some(3));
|
||||||
|
/// assert_eq!(skipped.next().await, None);
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
fn skip(self, n: usize) -> Skip<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
Skip::new(self, n)
|
||||||
|
}
|
||||||
|
|
||||||
/// 'Zips up' two streams into a single stream of pairs.
|
/// 'Zips up' two streams into a single stream of pairs.
|
||||||
///
|
///
|
||||||
/// `zip()` returns a new stream that will iterate over two other streams, returning a tuple
|
/// `zip()` returns a new stream that will iterate over two other streams, returning a tuple
|
||||||
|
|
41
src/stream/stream/skip.rs
Normal file
41
src/stream/stream/skip.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use crate::stream::Stream;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct Skip<S> {
|
||||||
|
stream: S,
|
||||||
|
n: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Skip<S> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: S);
|
||||||
|
pin_utils::unsafe_unpinned!(n: usize);
|
||||||
|
|
||||||
|
pub(crate) fn new(stream: S, n: usize) -> Self {
|
||||||
|
Skip { stream, n }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Stream for Skip<S>
|
||||||
|
where
|
||||||
|
S: Stream,
|
||||||
|
{
|
||||||
|
type Item = S::Item;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
loop {
|
||||||
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
|
||||||
|
match next {
|
||||||
|
Some(v) => match self.n {
|
||||||
|
0 => return Poll::Ready(Some(v)),
|
||||||
|
_ => *self.as_mut().n() -= 1,
|
||||||
|
},
|
||||||
|
None => return Poll::Ready(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue