mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
parent
ab24a655ad
commit
1a5de2c79d
@ -53,6 +53,9 @@ pub use pending::{pending, Pending};
|
||||
mod stream_map;
|
||||
pub use stream_map::StreamMap;
|
||||
|
||||
mod skip;
|
||||
use skip::Skip;
|
||||
|
||||
mod try_next;
|
||||
use try_next::TryNext;
|
||||
|
||||
@ -451,6 +454,31 @@ pub trait StreamExt: Stream {
|
||||
TakeWhile::new(self, f)
|
||||
}
|
||||
|
||||
/// Creates a new stream that will skip the `n` first items of the
|
||||
/// underlying stream.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # #[tokio::main]
|
||||
/// # async fn main() {
|
||||
/// use tokio::stream::{self, StreamExt};
|
||||
///
|
||||
/// let mut stream = stream::iter(1..=10).skip(7);
|
||||
///
|
||||
/// assert_eq!(Some(8), stream.next().await);
|
||||
/// assert_eq!(Some(9), stream.next().await);
|
||||
/// assert_eq!(Some(10), stream.next().await);
|
||||
/// assert_eq!(None, stream.next().await);
|
||||
/// # }
|
||||
/// ```
|
||||
fn skip(self, n: usize) -> Skip<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Skip::new(self, n)
|
||||
}
|
||||
|
||||
/// Tests if every element of the stream matches a predicate.
|
||||
///
|
||||
/// `all()` takes a closure that returns `true` or `false`. It applies
|
||||
|
63
tokio/src/stream/skip.rs
Normal file
63
tokio/src/stream/skip.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use crate::stream::Stream;
|
||||
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`skip`](super::StreamExt::skip) method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct Skip<St> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
remaining: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> fmt::Debug for Skip<St>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Skip")
|
||||
.field("stream", &self.stream)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> Skip<St> {
|
||||
pub(super) fn new(stream: St, remaining: usize) -> Self {
|
||||
Self { stream, remaining }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> Stream for Skip<St>
|
||||
where
|
||||
St: Stream,
|
||||
{
|
||||
type Item = St::Item;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
loop {
|
||||
match ready!(self.as_mut().project().stream.poll_next(cx)) {
|
||||
Some(e) => {
|
||||
if self.remaining == 0 {
|
||||
return Poll::Ready(Some(e));
|
||||
}
|
||||
*self.as_mut().project().remaining -= 1;
|
||||
}
|
||||
None => return Poll::Ready(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let (lower, upper) = self.stream.size_hint();
|
||||
|
||||
let lower = lower.saturating_sub(self.remaining);
|
||||
let upper = upper.map(|x| x.saturating_sub(self.remaining));
|
||||
|
||||
(lower, upper)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user