What's different between the poll change from futures 0.1 to 0.3

there are two function:

fn poll(&mut self) -> Poll<Self::Item, Self::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;

how to use Context?

You rarely need to (or want to call poll directly). The easiest way to use futures is putting them inside an async function or code block, and then you can use .await. If you provide an example of what you're trying to do with code samples, it would be easier to provide assistance.

i want to impelement a interval for some tasks.

use core::pin::Pin;
use futures::{
    stream::{poll_fn, Stream},
    task::{Context, Poll},
use std::time::Duration;
use tokio::time::{delay_until, Delay, Error, Instant};

/// A stream representing notifications at fixed interval
pub struct Interval {
    /// Future that completes the next time the `Interval` yields a value.
    delay: Delay,

    /// The duration between values yielded by `Interval`.
    duration: Duration,

impl Interval {
    /// Create a new `Interval` that starts at `at` and yields every `duration`
    /// interval after that.
    /// Note that when it starts, it produces item too.
    /// The `duration` argument must be a non-zero duration.
    /// # Panics
    /// This function panics if `duration` is zero.
    pub fn new(at: Instant, duration: Duration) -> Interval {
            duration > Duration::new(0, 0),
            "`duration` must be non-zero."
        Interval::new_with_delay(delay_until(at), duration)

    /// Creates new `Interval` that yields with interval of `duration`.
    /// The function is shortcut for `Interval::new(Instant::now() + duration, duration)`.
    /// The `duration` argument must be a non-zero duration.
    /// # Panics
    /// This function panics if `duration` is zero.
    pub fn new_interval(duration: Duration) -> Interval {
        Interval::new(Instant::now(), duration)

    pub(crate) fn new_with_delay(delay: Delay, duration: Duration) -> Interval {
        Interval { delay, duration }

impl Stream for Interval {
    type Item = Instant;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Wait for the delay to be done
        let _ = ready!(self.delay.poll(self,cx));

        self.delay.reset(Instant::now() + self.duration);

        // Get the `now` by looking at the `delay` deadline
        let now = self.delay.deadline();

        // Return the current instant

but when updating from futures 0.1 to 0.3.
it throws out some error

no method named `poll` found for struct `tokio::time::delay::Delay` in the current scope
expected enum `std::task::Poll`, found enum `std::result::Result`

thank you for help !!

Why don't you use tokio::time::Interval?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.