Rollup merge of #146057 - Ddystopia:waker-fn, r=dtolnay

feat: add `from_fn_ptr` to `Waker` and `LocalWaker`

Closes: https://github.com/rust-lang/rust/issues/146055
This commit is contained in:
Stuart Cook 2025-11-04 13:44:49 +11:00 committed by GitHub
commit 149eb1a36d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -584,6 +584,28 @@ impl Waker {
pub fn vtable(&self) -> &'static RawWakerVTable {
self.waker.vtable
}
/// Constructs a `Waker` from a function pointer.
#[inline]
#[must_use]
#[unstable(feature = "waker_from_fn_ptr", issue = "148457")]
pub const fn from_fn_ptr(f: fn()) -> Self {
// SAFETY: Unsafe is used for transmutes, pointer came from `fn()` so it
// is sound to transmute it back to `fn()`.
static VTABLE: RawWakerVTable = unsafe {
RawWakerVTable::new(
|this| RawWaker::new(this, &VTABLE),
|this| transmute::<*const (), fn()>(this)(),
|this| transmute::<*const (), fn()>(this)(),
|_| {},
)
};
let raw = RawWaker::new(f as *const (), &VTABLE);
// SAFETY: `clone` is just a copy, `drop` is a no-op while `wake` and
// `wake_by_ref` just call the function pointer.
unsafe { Self::from_raw(raw) }
}
}
#[stable(feature = "futures_api", since = "1.36.0")]
@ -879,6 +901,28 @@ impl LocalWaker {
pub fn vtable(&self) -> &'static RawWakerVTable {
self.waker.vtable
}
/// Constructs a `LocalWaker` from a function pointer.
#[inline]
#[must_use]
#[unstable(feature = "waker_from_fn_ptr", issue = "148457")]
pub const fn from_fn_ptr(f: fn()) -> Self {
// SAFETY: Unsafe is used for transmutes, pointer came from `fn()` so it
// is sound to transmute it back to `fn()`.
static VTABLE: RawWakerVTable = unsafe {
RawWakerVTable::new(
|this| RawWaker::new(this, &VTABLE),
|this| transmute::<*const (), fn()>(this)(),
|this| transmute::<*const (), fn()>(this)(),
|_| {},
)
};
let raw = RawWaker::new(f as *const (), &VTABLE);
// SAFETY: `clone` is just a copy, `drop` is a no-op while `wake` and
// `wake_by_ref` just call the function pointer.
unsafe { Self::from_raw(raw) }
}
}
#[unstable(feature = "local_waker", issue = "118959")]
impl Clone for LocalWaker {