pub trait Stream {
type Item;
type Error;
// Required method
fn poll_next(
&mut self,
cx: &mut Context<'_>,
) -> Result<Async<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§
Required Methods§
Sourcefn poll_next(
&mut self,
cx: &mut Context<'_>,
) -> Result<Async<Option<Self::Item>>, Self::Error>
fn poll_next( &mut self, cx: &mut Context<'_>, ) -> Result<Async<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 subsequentpoll_next
calls. -
Ok(Ready(None))
means that the stream has terminated, andpoll_next
should not be invoked again. -
Err(err)
means that the stream encountered an error while trying topoll_next
. Subsequent calls topoll_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.