Skip to content

Commit b385528

Browse files
authored
Unrolled build for rust-lang#129919
Rollup merge of rust-lang#129919 - kevinmehall:waker-getters, r=dtolnay Stabilize `waker_getters` Tracking issue: rust-lang#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."](rust-lang#96992 (comment)) 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 rust-lang#96992 (comment) ```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 rust-lang#96992
2 parents 009e738 + 22bd319 commit b385528

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

library/core/src/task/wake.rs

+79-24
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ impl RawWaker {
6060
RawWaker { data, vtable }
6161
}
6262

63-
/// Gets the `data` pointer used to create this `RawWaker`.
64-
#[inline]
65-
#[must_use]
66-
#[unstable(feature = "waker_getters", issue = "96992")]
67-
pub fn data(&self) -> *const () {
68-
self.data
69-
}
70-
71-
/// Gets the `vtable` pointer used to create this `RawWaker`.
72-
#[inline]
73-
#[must_use]
74-
#[unstable(feature = "waker_getters", issue = "96992")]
75-
pub fn vtable(&self) -> &'static RawWakerVTable {
76-
self.vtable
77-
}
78-
7963
#[unstable(feature = "noop_waker", issue = "98286")]
8064
const NOOP: RawWaker = {
8165
const VTABLE: RawWakerVTable = RawWakerVTable::new(
@@ -509,6 +493,37 @@ impl Waker {
509493
a_data == b_data && ptr::eq(a_vtable, b_vtable)
510494
}
511495

496+
/// Creates a new `Waker` from the provided `data` pointer and `vtable`.
497+
///
498+
/// The `data` pointer can be used to store arbitrary data as required
499+
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
500+
/// that is associated with the task.
501+
/// The value of this pointer will get passed to all functions that are part
502+
/// of the `vtable` as the first parameter.
503+
///
504+
/// It is important to consider that the `data` pointer must point to a
505+
/// thread safe type such as an `Arc`.
506+
///
507+
/// The `vtable` customizes the behavior of a `Waker`. For each operation
508+
/// on the `Waker`, the associated function in the `vtable` will be called.
509+
///
510+
/// # Safety
511+
///
512+
/// The behavior of the returned `Waker` is undefined if the contract defined
513+
/// in [`RawWakerVTable`]'s documentation is not upheld.
514+
///
515+
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
516+
/// cost of a required heap allocation.)
517+
///
518+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
519+
#[inline]
520+
#[must_use]
521+
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
522+
#[rustc_const_stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
523+
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
524+
Waker { waker: RawWaker { data, vtable } }
525+
}
526+
512527
/// Creates a new `Waker` from [`RawWaker`].
513528
///
514529
/// # Safety
@@ -565,12 +580,20 @@ impl Waker {
565580
WAKER
566581
}
567582

568-
/// Gets a reference to the underlying [`RawWaker`].
583+
/// Gets the `data` pointer used to create this `Waker`.
569584
#[inline]
570585
#[must_use]
571-
#[unstable(feature = "waker_getters", issue = "96992")]
572-
pub fn as_raw(&self) -> &RawWaker {
573-
&self.waker
586+
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
587+
pub fn data(&self) -> *const () {
588+
self.waker.data
589+
}
590+
591+
/// Gets the `vtable` pointer used to create this `Waker`.
592+
#[inline]
593+
#[must_use]
594+
#[stable(feature = "waker_getters", since = "CURRENT_RUSTC_VERSION")]
595+
pub fn vtable(&self) -> &'static RawWakerVTable {
596+
self.waker.vtable
574597
}
575598
}
576599

@@ -778,6 +801,30 @@ impl LocalWaker {
778801
a_data == b_data && ptr::eq(a_vtable, b_vtable)
779802
}
780803

804+
/// Creates a new `LocalWaker` from the provided `data` pointer and `vtable`.
805+
///
806+
/// The `data` pointer can be used to store arbitrary data as required
807+
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
808+
/// that is associated with the task.
809+
/// The value of this pointer will get passed to all functions that are part
810+
/// of the `vtable` as the first parameter.
811+
///
812+
/// The `vtable` customizes the behavior of a `LocalWaker`. For each
813+
/// operation on the `LocalWaker`, the associated function in the `vtable`
814+
/// will be called.
815+
///
816+
/// # Safety
817+
///
818+
/// The behavior of the returned `Waker` is undefined if the contract defined
819+
/// in [`RawWakerVTable`]'s documentation is not upheld.
820+
///
821+
#[inline]
822+
#[must_use]
823+
#[unstable(feature = "local_waker", issue = "118959")]
824+
pub const unsafe fn new(data: *const (), vtable: &'static RawWakerVTable) -> Self {
825+
LocalWaker { waker: RawWaker { data, vtable } }
826+
}
827+
781828
/// Creates a new `LocalWaker` from [`RawWaker`].
782829
///
783830
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
@@ -831,12 +878,20 @@ impl LocalWaker {
831878
WAKER
832879
}
833880

834-
/// Gets a reference to the underlying [`RawWaker`].
881+
/// Gets the `data` pointer used to create this `LocalWaker`.
835882
#[inline]
836883
#[must_use]
837-
#[unstable(feature = "waker_getters", issue = "96992")]
838-
pub fn as_raw(&self) -> &RawWaker {
839-
&self.waker
884+
#[unstable(feature = "local_waker", issue = "118959")]
885+
pub fn data(&self) -> *const () {
886+
self.waker.data
887+
}
888+
889+
/// Gets the `vtable` pointer used to create this `LocalWaker`.
890+
#[inline]
891+
#[must_use]
892+
#[unstable(feature = "local_waker", issue = "118959")]
893+
pub fn vtable(&self) -> &'static RawWakerVTable {
894+
self.waker.vtable
840895
}
841896
}
842897
#[unstable(feature = "local_waker", issue = "118959")]

library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
#![feature(unsize)]
113113
#![feature(unsized_tuple_coercion)]
114114
#![feature(unwrap_infallible)]
115-
#![feature(waker_getters)]
116115
// tidy-alphabetical-end
117116
#![allow(internal_features)]
118117
#![deny(fuzzy_provenance_casts)]

library/core/tests/waker.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ use std::task::{RawWaker, RawWakerVTable, Waker};
44
#[test]
55
fn test_waker_getters() {
66
let raw_waker = RawWaker::new(ptr::without_provenance_mut(42usize), &WAKER_VTABLE);
7-
assert_eq!(raw_waker.data() as usize, 42);
8-
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE));
9-
107
let waker = unsafe { Waker::from_raw(raw_waker) };
8+
assert_eq!(waker.data() as usize, 42);
9+
assert!(ptr::eq(waker.vtable(), &WAKER_VTABLE));
10+
1111
let waker2 = waker.clone();
12-
let raw_waker2 = waker2.as_raw();
13-
assert_eq!(raw_waker2.data() as usize, 43);
14-
assert!(ptr::eq(raw_waker2.vtable(), &WAKER_VTABLE));
12+
assert_eq!(waker2.data() as usize, 43);
13+
assert!(ptr::eq(waker2.vtable(), &WAKER_VTABLE));
1514
}
1615

1716
static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(

0 commit comments

Comments
 (0)