Trait futures::Stream [] [src]

pub trait Stream {
    type Item;
    type Error;
    fn poll_next(
        &mut self,
        cx: &mut Context
    ) -> Result<Async<Option<Self::Item>>, Self::Error>; }

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.


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.

Associated Types

Values yielded by the stream.

Errors yielded by the stream.

Required Methods

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.


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

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


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


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


impl<A, E, F> Stream for Recover<A, E, F> where
    A: Stream,
    F: FnMut(<A as Stream>::Error) -> Option<<A as Stream>::Item>,