Trait futures_core::stream::Stream [−][src]
pub trait Stream { type Item; type Error; fn poll_next(
&mut self,
cx: &mut Context
) -> Poll<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.
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
.
Associated Types
Required Methods
fn poll_next(
&mut self,
cx: &mut Context
) -> Poll<Option<Self::Item>, Self::Error>
&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 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.
Implementations on Foreign Types
impl<A, B> Stream for Either<A, B> where
A: Stream,
B: Stream<Item = A::Item, Error = A::Error>,
[src]
impl<A, B> Stream for Either<A, B> where
A: Stream,
B: Stream<Item = A::Item, Error = A::Error>,
impl<'a, S: ?Sized + Stream> Stream for &'a mut S
[src]
impl<'a, S: ?Sized + Stream> Stream for &'a mut S
impl<S: ?Sized + Stream> Stream for Box<S>
[src]
impl<S: ?Sized + Stream> Stream for Box<S>
impl<S: Stream> Stream for AssertUnwindSafe<S>
[src]
impl<S: Stream> Stream for AssertUnwindSafe<S>
impl<T> Stream for VecDeque<T>
[src]
impl<T> Stream for VecDeque<T>