Sha256: 61f7ec7fe25663d2c87cffaad19ed27eda032842edb8af731b521025b244f120

Contents?: true

Size: 1.69 KB

Versions: 25

Compression:

Stored size: 1.69 KB

Contents

use core::pin::Pin;
use futures_core::ready;
use futures_core::stream::{FusedStream, Stream};
use futures_core::task::{Context, Poll};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use pin_project_lite::pin_project;

pin_project! {
    /// Stream for the [`skip`](super::StreamExt::skip) method.
    #[derive(Debug)]
    #[must_use = "streams do nothing unless polled"]
    pub struct Skip<St> {
        #[pin]
        stream: St,
        remaining: usize,
    }
}

impl<St: Stream> Skip<St> {
    pub(super) fn new(stream: St, n: usize) -> Self {
        Self { stream, remaining: n }
    }

    delegate_access_inner!(stream, St, ());
}

impl<St: FusedStream> FusedStream for Skip<St> {
    fn is_terminated(&self) -> bool {
        self.stream.is_terminated()
    }
}

impl<St: Stream> Stream for Skip<St> {
    type Item = St::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<St::Item>> {
        let mut this = self.project();

        while *this.remaining > 0 {
            if ready!(this.stream.as_mut().poll_next(cx)).is_some() {
                *this.remaining -= 1;
            } else {
                return Poll::Ready(None);
            }
        }

        this.stream.poll_next(cx)
    }

    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)
    }
}

// Forwarding impl of Sink from the underlying stream
#[cfg(feature = "sink")]
impl<S, Item> Sink<Item> for Skip<S>
where
    S: Stream + Sink<Item>,
{
    type Error = S::Error;

    delegate_sink!(stream, Item);
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
wasmtime-28.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/stream/stream/skip.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/stream/stream/skip.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/stream/stream/skip.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/skip.rs
wasmtime-14.0.4 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/skip.rs
wasmtime-14.0.3 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/skip.rs