Sha256: ed7e3d15e7b1adec5ad5789b0d3186b5995a3353cc974fb7f41a72f6d8ad4cbb

Contents?: true

Size: 1.57 KB

Versions: 22

Compression:

Stored size: 1.57 KB

Contents

use core::pin::Pin;
use core::usize;
use futures_core::ready;
use futures_core::stream::{FusedStream, Stream};
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;

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

impl<St> Cycle<St>
where
    St: Clone + Stream,
{
    pub(super) fn new(stream: St) -> Self {
        Self { orig: stream.clone(), stream }
    }
}

impl<St> Stream for Cycle<St>
where
    St: Clone + Stream,
{
    type Item = St::Item;

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

        match ready!(this.stream.as_mut().poll_next(cx)) {
            None => {
                this.stream.set(this.orig.clone());
                this.stream.poll_next(cx)
            }
            item => Poll::Ready(item),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // the cycle stream is either empty or infinite
        match self.orig.size_hint() {
            size @ (0, Some(0)) => size,
            (0, _) => (0, None),
            _ => (usize::max_value(), None),
        }
    }
}

impl<St> FusedStream for Cycle<St>
where
    St: Clone + Stream,
{
    fn is_terminated(&self) -> bool {
        // the cycle stream is either empty or infinite
        if let (0, Some(0)) = self.size_hint() {
            true
        } else {
            false
        }
    }
}

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/stream/stream/cycle.rs
wasmtime-14.0.4 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/cycle.rs
wasmtime-14.0.3 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/cycle.rs
wasmtime-14.0.1 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/cycle.rs
wasmtime-14.0.0 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/cycle.rs
wasmtime-13.0.0 ./ext/cargo-vendor/futures-util-0.3.28/src/stream/stream/cycle.rs