futures_core::stream

Trait Stream

Source
pub trait Stream {
    type Item;
    type Error;

    // Required method
    fn poll_next(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Self::Item>, Self::Error>;
}
Expand description

A stream of values produced asynchronously.

If Future is an asynchronous version of Result, then Stream is an asynchronous version of Iterator. A stream represents a sequence of value-producing events that occur asynchronously to the caller.

The trait is modeled after Future, but allows poll_next to be called even after a value has been produced, yielding None once the stream has been fully exhausted.

§Errors

Streams, like futures, also bake in errors through an associated Error type. An error on a stream does not terminate the stream. That is, after one error is received, another value may be received from the same stream (it’s valid to keep polling). Thus a stream is somewhat like an Iterator<Item = Result<T, E>>, and is always terminated by returning None.

Required Associated Types§

Source

type Item

Values yielded by the stream.

Source

type Error

Errors yielded by the stream.

Required Methods§

Source

fn poll_next( &mut self, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>, Self::Error>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted.

§Return value

There are several possible return values, each indicating a distinct stream state:

  • Ok(Pending) means that this stream’s next value is not ready yet. Implementations will ensure that the current task will be notified when the next value may be ready.

  • Ok(Ready(Some(val))) means that the stream has successfully produced a value, val, and may produce further values on subsequent poll_next calls.

  • Ok(Ready(None)) means that the stream has terminated, and poll_next should not be invoked again.

  • Err(err) means that the stream encountered an error while trying to poll_next. Subsequent calls to poll_next are allowed, and may return further values or errors.

§Panics

Once a stream is finished, i.e. Ready(None) has been returned, further calls to poll_next may result in a panic or other “bad behavior”. If this is difficult to guard against then the fuse adapter can be used to ensure that poll_next always returns Ready(None) in subsequent calls.

Implementations on Foreign Types§

Source§

impl<'a, S: ?Sized + Stream> Stream for &'a mut S

Source§

type Item = <S as Stream>::Item

Source§

type Error = <S as Stream>::Error

Source§

fn poll_next( &mut self, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>, Self::Error>

Source§

impl<A, B> Stream for Either<A, B>
where A: Stream, B: Stream<Item = A::Item, Error = A::Error>,

Source§

type Item = <A as Stream>::Item

Source§

type Error = <A as Stream>::Error

Source§

fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<A::Item>, A::Error>

Source§

impl<S: Stream> Stream for AssertUnwindSafe<S>

Source§

type Item = <S as Stream>::Item

Source§

type Error = <S as Stream>::Error

Source§

fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<S::Item>, S::Error>

Source§

impl<S: ?Sized + Stream> Stream for Box<S>

Source§

type Item = <S as Stream>::Item

Source§

type Error = <S as Stream>::Error

Source§

fn poll_next( &mut self, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>, Self::Error>

Source§

impl<T> Stream for VecDeque<T>

Source§

type Item = T

Source§

type Error = Never

Source§

fn poll_next( &mut self, _cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>, Self::Error>

Implementors§