Rollup merge of #129919 - kevinmehall:waker-getters, r=dtolnay
Stabilize `waker_getters` Tracking issue: #96992 FCP completed on the tracking issue a while ago. It's not clear whether the libs-api team wanted the `RawWaker` methods moved to `Waker` or went back to the current API after further discussion. `@Amanieu` [wrote "This is just waiting for someone to submit a stabilization PR."](https://github.com/rust-lang/rust/issues/96992#issuecomment-2213685218) so I'm doing just that in hopes of nudging this along. Edit: Moved the `data` and `vtable` methods from `RawWaker` to `Waker` and added `Waker::new` per https://github.com/rust-lang/rust/issues/96992#issuecomment-1941998046 ```rs impl Waker { pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self; pub fn data(&self) -> *const (); pub fn vtable(&self) -> &'static RawWakerVTable; } ``` Closes #96992
This commit is contained in:
commit
c7c3ada95a
@ -60,22 +60,6 @@ impl RawWaker {
|
||||
RawWaker { data, vtable }
|
||||
}
|
||||
|
||||
/// Gets the `data` pointer used to create this `RawWaker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "waker_getters", issue = "96992")]
|
||||
pub fn data(&self) -> *const () {
|
||||
self.data
|
||||
}
|
||||
|
||||
/// Gets the `vtable` pointer used to create this `RawWaker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "waker_getters", issue = "96992")]
|
||||
pub fn vtable(&self) -> &'static RawWakerVTable {
|
||||
self.vtable
|
||||
}
|
||||
|
||||
#[unstable(feature = "noop_waker", issue = "98286")]
|
||||
const NOOP: RawWaker = {
|
||||
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|
||||
@ -509,6 +493,37 @@ impl Waker {
|
||||
a_data == b_data && ptr::eq(a_vtable, b_vtable)
|
||||
}
|
||||
|
||||
/// Creates a new `Waker` from the provided `data` pointer and `vtable`.
|
||||
///
|
||||
/// The `data` pointer can be used to store arbitrary data as required
|
||||
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
|
||||
/// that is associated with the task.
|
||||
/// The value of this pointer will get passed to all functions that are part
|
||||
/// of the `vtable` as the first parameter.
|
||||
///
|
||||
/// It is important to consider that the `data` pointer must point to a
|
||||
/// thread safe type such as an `Arc`.
|
||||
///
|
||||
/// The `vtable` customizes the behavior of a `Waker`. For each operation
|
||||
/// on the `Waker`, the associated function in the `vtable` will be called.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The behavior of the returned `Waker` is undefined if the contract defined
|
||||
/// in [`RawWakerVTable`]'s documentation is not upheld.
|
||||
///
|
||||
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
|
||||
/// cost of a required heap allocation.)
|
||||
///
|
||||
/// [`Wake`]: ../../alloc/task/trait.Wake.html
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
|
||||
Waker { waker: RawWaker { data, vtable } }
|
||||
}
|
||||
|
||||
/// Creates a new `Waker` from [`RawWaker`].
|
||||
///
|
||||
/// # Safety
|
||||
@ -565,12 +580,20 @@ impl Waker {
|
||||
WAKER
|
||||
}
|
||||
|
||||
/// Gets a reference to the underlying [`RawWaker`].
|
||||
/// Gets the `data` pointer used to create this `Waker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "waker_getters", issue = "96992")]
|
||||
pub fn as_raw(&self) -> &RawWaker {
|
||||
&self.waker
|
||||
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub fn data(&self) -> *const () {
|
||||
self.waker.data
|
||||
}
|
||||
|
||||
/// Gets the `vtable` pointer used to create this `Waker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub fn vtable(&self) -> &'static RawWakerVTable {
|
||||
self.waker.vtable
|
||||
}
|
||||
}
|
||||
|
||||
@ -778,6 +801,30 @@ impl LocalWaker {
|
||||
a_data == b_data && ptr::eq(a_vtable, b_vtable)
|
||||
}
|
||||
|
||||
/// Creates a new `LocalWaker` from the provided `data` pointer and `vtable`.
|
||||
///
|
||||
/// The `data` pointer can be used to store arbitrary data as required
|
||||
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
|
||||
/// that is associated with the task.
|
||||
/// The value of this pointer will get passed to all functions that are part
|
||||
/// of the `vtable` as the first parameter.
|
||||
///
|
||||
/// The `vtable` customizes the behavior of a `LocalWaker`. For each
|
||||
/// operation on the `LocalWaker`, the associated function in the `vtable`
|
||||
/// will be called.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The behavior of the returned `Waker` is undefined if the contract defined
|
||||
/// in [`RawWakerVTable`]'s documentation is not upheld.
|
||||
///
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "local_waker", issue = "118959")]
|
||||
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
|
||||
LocalWaker { waker: RawWaker { data, vtable } }
|
||||
}
|
||||
|
||||
/// Creates a new `LocalWaker` from [`RawWaker`].
|
||||
///
|
||||
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
|
||||
@ -831,12 +878,20 @@ impl LocalWaker {
|
||||
WAKER
|
||||
}
|
||||
|
||||
/// Gets a reference to the underlying [`RawWaker`].
|
||||
/// Gets the `data` pointer used to create this `LocalWaker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "waker_getters", issue = "96992")]
|
||||
pub fn as_raw(&self) -> &RawWaker {
|
||||
&self.waker
|
||||
#[unstable(feature = "local_waker", issue = "118959")]
|
||||
pub fn data(&self) -> *const () {
|
||||
self.waker.data
|
||||
}
|
||||
|
||||
/// Gets the `vtable` pointer used to create this `LocalWaker`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "local_waker", issue = "118959")]
|
||||
pub fn vtable(&self) -> &'static RawWakerVTable {
|
||||
self.waker.vtable
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "local_waker", issue = "118959")]
|
||||
|
@ -112,7 +112,6 @@
|
||||
#![feature(unsize)]
|
||||
#![feature(unsized_tuple_coercion)]
|
||||
#![feature(unwrap_infallible)]
|
||||
#![feature(waker_getters)]
|
||||
// tidy-alphabetical-end
|
||||
#![allow(internal_features)]
|
||||
#![deny(fuzzy_provenance_casts)]
|
||||
|
@ -4,14 +4,13 @@ use std::task::{RawWaker, RawWakerVTable, Waker};
|
||||
#[test]
|
||||
fn test_waker_getters() {
|
||||
let raw_waker = RawWaker::new(ptr::without_provenance_mut(42usize), &WAKER_VTABLE);
|
||||
assert_eq!(raw_waker.data() as usize, 42);
|
||||
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE));
|
||||
|
||||
let waker = unsafe { Waker::from_raw(raw_waker) };
|
||||
assert_eq!(waker.data() as usize, 42);
|
||||
assert!(ptr::eq(waker.vtable(), &WAKER_VTABLE));
|
||||
|
||||
let waker2 = waker.clone();
|
||||
let raw_waker2 = waker2.as_raw();
|
||||
assert_eq!(raw_waker2.data() as usize, 43);
|
||||
assert!(ptr::eq(raw_waker2.vtable(), &WAKER_VTABLE));
|
||||
assert_eq!(waker2.data() as usize, 43);
|
||||
assert!(ptr::eq(waker2.vtable(), &WAKER_VTABLE));
|
||||
}
|
||||
|
||||
static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
|
||||
|
Loading…
x
Reference in New Issue
Block a user