Trait Stream

Source
pub trait Stream {
    type Item;

    // Required method
    fn next(&mut self) -> impl Future<Output = Option<Self::Item>>;

    // Provided method
    fn size_hint(&self) -> (usize, Option<usize>) { ... }
}
Expand description

A stream of values produced asynchronously in pure async/await.

Required Associated Types§

Source

type Item

Values yielded by the stream.

Required Methods§

Source

fn next(&mut self) -> impl Future<Output = Option<Self::Item>>

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.

Provided Methods§

Source

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream.

Specifically, size_hint() returns a tuple where the first element is the lower bound, and the second element is the upper bound.

The second half of the tuple that is returned is an Option<usize>. A None here means that either there is no known upper bound, or the upper bound is larger than usize.

§Implementation notes

It is not enforced that a stream implementation yields the declared number of elements. A buggy stream may yield less than the lower bound or more than the upper bound of elements.

size_hint() is primarily intended to be used for optimizations such as reserving space for the elements of the stream, but must not be trusted to e.g., omit bounds checks in unsafe code. An incorrect implementation of size_hint() should not lead to memory safety violations.

That said, the implementation should provide a correct estimation, because otherwise it would be a violation of the trait’s protocol.

The default implementation returns (0, None) which is correct for any stream.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

type Item = <S as Stream>::Item

Source§

fn next(&mut self) -> impl Future<Output = Option<Self::Item>>

Implementors§