mirror of
https://github.com/rust-lang/rust.git
synced 2025-12-02 16:28:09 +00:00
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:
commit
149eb1a36d
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user