Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/futures-util/src/stream/stream/   (Firefox Browser Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 1 kB image not shown  

Quelle  take.rs

  Sprache: Rust
 

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(superfn 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);
}

Messung V0.5 in Prozent
C=98 H=100 G=98

¤ Dauer der Verarbeitung: 0.15 Sekunden  (vorverarbeitet am  2026-06-18) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.