Implementing Stream

The Stream trait is very similar to the Future trait.

  1. use std::pin::Pin;
  2. use std::task::{Context, Poll};
  3. pub trait Stream {
  4. type Item;
  5. fn poll_next(
  6. self: Pin<&mut Self>,
  7. cx: &mut Context<'_>
  8. ) -> Poll<Option<Self::Item>>;
  9. fn size_hint(&self) -> (usize, Option<usize>) {
  10. (0, None)
  11. }
  12. }

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

  1. use tokio_stream::Stream;
  2. use std::pin::Pin;
  3. use std::task::{Context, Poll};
  4. use std::time::Duration;
  5. struct Interval {
  6. rem: usize,
  7. delay: Delay,
  8. }
  9. impl Stream for Interval {
  10. type Item = ();
  11. fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
  12. -> Poll<Option<()>>
  13. {
  14. if self.rem == 0 {
  15. // No more delays
  16. return Poll::Ready(None);
  17. }
  18. match Pin::new(&mut self.delay).poll(cx) {
  19. Poll::Ready(_) => {
  20. let when = self.delay.when + Duration::from_millis(10);
  21. self.delay = Delay { when };
  22. self.rem -= 1;
  23. Poll::Ready(Some(()))
  24. }
  25. Poll::Pending => Poll::Pending,
  26. }
  27. }
  28. }

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:

  1. use async_stream::stream;
  2. use std::time::{Duration, Instant};
  3. stream! {
  4. let mut when = Instant::now();
  5. for _ in 0..3 {
  6. let delay = Delay { when };
  7. delay.await;
  8. yield ();
  9. when += Duration::from_millis(10);
  10. }
  11. }