Add conversion from Arc<Fn> to Waker #70764
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an addition to work done in #68700. This patch adds a conversion from
Arc<Fn>
toWaker
making it significantly easier to construct wakers for instrumentation purposes.The conversion of a closure into a waker has seen prior adoption in the ecosystem in the form of the
wakeful
crate by @sagebind. And was later adopted by @stjepang inasync-task
as well.Usage
Alternatives
Originally there was talk about adding a
task::waker_fn
instead. But @withoutboats pointed out there had always been an intention to add a safeWake
trait, and conversions from closures could instead utilize that. Now thatWake
is available on nightly, it seemed like a good idea to implement this conversion as well.While drafting this patch I also tried to make a non-arc conversion work, but unfortunately failed because of orphan rules (
Waker
andFn
are defined incore
,Arc
is defined inalloc
):I'm not sure if there is a way around that given that tests wouldn't compile if this was a problem. But if that's not desirable for any reason, we should probably review this conversion is forward-compatible with a future conversion from
From<Fn() + Sync + Send + 'static> for Waker
.Stability
I think this conversion should be separate from the core trait introduced in #68700. Any issues that might arise from this should not hold up the stabilization of the core trait; so I propose we track this (and any future conversions) separately.
References
wakeful::waker_fn
async-task::waker_fn
Wake
trait