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

Function to get the current waker in an async fn #8

Closed
jjl opened this issue Aug 5, 2020 · 6 comments
Closed

Function to get the current waker in an async fn #8

jjl opened this issue Aug 5, 2020 · 6 comments

Comments

@jjl
Copy link
Contributor

jjl commented Aug 5, 2020

Not sure about the name, but I just found a use for this and it's incredibly small. Maybe you have a better idea for a name?

pub async fn waker() -> Waker {
    future::poll_fn(|c| Poll::Ready(c.waker().clone())).await
}
@jjl
Copy link
Contributor Author

jjl commented Aug 5, 2020

slightly more contentious bonus:

pub async fn delay() {
    let mut done = false;
    future::poll_fn(|_| {
        if done {
            Poll::Ready(())
        } else {
            done = true;
            Poll::Pending
        }
    }).await
}

@ghost
Copy link

ghost commented Aug 5, 2020

I've wanted waker() before, more than once :) I think it's useful! The name sounds good to me. Some other ideas: get_waker(), current_waker(). Not sure if its possible, but perhaps it'd be worth changing the signature to return a &Waker rather than Waker.

Regarding delay(), I think that's same as futures_lite::future::yield_now().

@jjl
Copy link
Contributor Author

jjl commented Aug 5, 2020

AIUI, yield_now() reschedules the current future? Delay is for when you don't want to do that (because you're messing with wakers)

@jjl
Copy link
Contributor Author

jjl commented Aug 6, 2020

I went and looked, and yes, that's the essential difference - with delay, we don't wake ourselves.

@jjl
Copy link
Contributor Author

jjl commented Aug 8, 2020

These have now made it into futures-micro as 'waker' and 'sleep'. You might also like to steal PollState. Or you might find it offensive and decide not to :)

@notgull
Copy link
Member

notgull commented Dec 1, 2022

See this comment. I don't think we should add this; it's a bit niche.

@notgull notgull closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants