Implementing Stream
The Stream
trait is very similar to the Future
trait.
use std::pin::Pin;
use std::task::{Context, Poll};
pub trait Stream {
type Item;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Self::Item>>;
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
The Stream::poll_next()
function is much like Future::poll
, except it can be called repeatedly to receive many values from the stream. Just as we saw in Async in depth, when a stream is not ready to return a value, Poll::Pending
is returned instead. The task’s waker is registered. Once the stream should be polled again, the waker is notified.
The size_hint()
method is used the same way as it is with iterators.
Usually, when manually implementing a Stream
, it is done by composing futures and other streams. As an example, let’s build off of the Delay
future we implemented in Async in depth. We will convert it to a stream that yields ()
three times at 10 ms intervals
use tokio_stream::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
struct Interval {
rem: usize,
delay: Delay,
}
impl Stream for Interval {
type Item = ();
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<Option<()>>
{
if self.rem == 0 {
// No more delays
return Poll::Ready(None);
}
match Pin::new(&mut self.delay).poll(cx) {
Poll::Ready(_) => {
let when = self.delay.when + Duration::from_millis(10);
self.delay = Delay { when };
self.rem -= 1;
Poll::Ready(Some(()))
}
Poll::Pending => Poll::Pending,
}
}
}
async-stream
Manually implementing streams using the Stream
trait can be tedious. Unfortunately, the Rust programming language does not yet support async/await
syntax for defining streams. This is in the works, but not yet ready.
The async-stream
crate is available as a temporary solution. This crate provides an async_stream!
macro that transforms the input into a stream. Using this crate, the above interval can be implemented like this:
use async_stream::stream;
use std::time::{Duration, Instant};
stream! {
let mut when = Instant::now();
for _ in 0..3 {
let delay = Delay { when };
delay.await;
yield ();
when += Duration::from_millis(10);
}
}