Sha256: 57d381b482c3d584c4c26b0e15941bc2ea58e3f39a2e5c74391a2ee7b825cc8c

Contents?: true

Size: 1.94 KB

Versions: 25

Compression:

Stored size: 1.94 KB

Contents

use core::cmp;
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 [`take`](super::StreamExt::take) method.
    #[derive(Debug)]
    #[must_use = "streams do nothing unless polled"]
    pub struct Take<St> {
        #[pin]
        stream: St,
        remaining: usize,
    }
}

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

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

impl<St> Stream for Take<St>
where
    St: Stream,
{
    type Item = St::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<St::Item>> {
        if self.remaining == 0 {
            Poll::Ready(None)
        } else {
            let this = self.project();
            let next = ready!(this.stream.poll_next(cx));
            if next.is_some() {
                *this.remaining -= 1;
            } else {
                *this.remaining = 0;
            }
            Poll::Ready(next)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.remaining == 0 {
            return (0, Some(0));
        }

        let (lower, upper) = self.stream.size_hint();

        let lower = cmp::min(lower, self.remaining);

        let upper = match upper {
            Some(x) if x < self.remaining => Some(x),
            _ => Some(self.remaining),
        };

        (lower, upper)
    }
}

impl<St> FusedStream for Take<St>
where
    St: FusedStream,
{
    fn is_terminated(&self) -> bool {
        self.remaining == 0 || self.stream.is_terminated()
    }
}

// Forwarding impl of Sink from the underlying stream
#[cfg(feature = "sink")]
impl<S, Item> Sink<Item> for Take<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/take.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/stream/stream/take.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/stream/stream/take.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/take.rs
wasmtime-14.0.4 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/take.rs
wasmtime-14.0.3 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/take.rs