Skip to content

It seems Stream is not impl for Interval in tokio 1.0 ? #3341

Answered by Darksonn
valbendan asked this question in Q&A
Discussion options

You must be logged in to vote

The tokio-stream crate cannot add an impl of Stream to tokio::time::Interval because neither the trait nor the type is defined in tokio-stream. Instead you can use the async-stream crate to turn it into a stream.

let stream = async_stream::stream! {
    loop {
        yield interval.tick().await;
    }
};

You can also implement Stream on your wrapper type like this:

use tokio::time::{Interval, Instant};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_stream::Stream;

struct MyInterval {
    int: Interval,
}

impl Stream for MyInterval {
    type Item = Instant;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> {
        self.int.poll_tick

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Darksonn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants