Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pin and suffering #308

Closed
gekh opened this issue Jul 18, 2024 · 1 comment
Closed

Pin and suffering #308

gekh opened this issue Jul 18, 2024 · 1 comment

Comments

@gekh
Copy link

gekh commented Jul 18, 2024

I found something wrong on this page:

https://fasterthanli.me/articles/pin-and-suffering#position=31.0

Here's what it is:

this code doesn't work

use tokio::time::Sleep;

struct MyFuture {
    sleep: Sleep,
}

impl MyFuture {
    fn new() -> Self {
        Self {
            sleep: tokio::time::sleep(Duration::from_secs(1)),
        }
    }
}

impl Future for MyFuture {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        println!("MyFuture::poll()");
        self.sleep.poll(cx)
    }
}

Here you can find docs how it should be implemented:
https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::time::Sleep;

struct HasSleep {
    sleep: Pin<Box<Sleep>>,
}

impl Future for HasSleep {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.sleep.as_mut().poll(cx)
    }
}
@gekh
Copy link
Author

gekh commented Jul 18, 2024

Oh, sorry. I just should've read it further. Close it please.

@gekh gekh closed this as completed Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant