Skip to content

Commit a1c4cf6

Browse files
committed
Change RawWaker constructor to const fn
1 parent 8e7ef03 commit a1c4cf6

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/libcore/task/wake.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,29 @@ pub struct RawWaker {
1919
/// that is associated with the task.
2020
/// The value of this field gets passed to all functions that are part of
2121
/// the vtable as the first parameter.
22-
pub data: *const (),
22+
data: *const (),
2323
/// Virtual function pointer table that customizes the behavior of this waker.
24-
pub vtable: &'static RawWakerVTable,
24+
vtable: &'static RawWakerVTable,
25+
}
26+
27+
impl RawWaker {
28+
/// Creates a new `RawWaker` from the provided `data` pointer and `vtable`.
29+
///
30+
/// The `data` pointer can be used to store arbitrary data as required
31+
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
32+
/// that is associated with the task.
33+
/// The value of this poiner will get passed to all functions that are part
34+
/// of the `vtable` as the first parameter.
35+
///
36+
/// The `vtable` customizes the behavior of a `Waker` which gets created
37+
/// from a `RawWaker`. For each operation on the `Waker`, the associated
38+
/// function in the `vtable` of the underlying `RawWaker` will be called.
39+
pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
40+
RawWaker {
41+
data,
42+
vtable,
43+
}
44+
}
2545
}
2646

2747
/// A virtual function pointer table (vtable) that specifies the behavior
@@ -102,8 +122,8 @@ impl Waker {
102122
/// Creates a new `Waker` from [`RawWaker`].
103123
///
104124
/// The behavior of the returned `Waker` is undefined if the contract defined
105-
/// in [RawWaker]'s documentation is not upheld. Therefore this method is
106-
/// unsafe.
125+
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
126+
/// Therefore this method is unsafe.
107127
pub unsafe fn new_unchecked(waker: RawWaker) -> Waker {
108128
Waker {
109129
waker,

src/test/run-pass/auxiliary/arc_wake.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ pub trait ArcWake {
2525
let ptr = Arc::into_raw(wake) as *const();
2626

2727
unsafe {
28-
Waker::new_unchecked(RawWaker{
29-
data: ptr,
30-
vtable: waker_vtable!(Self),
31-
})
28+
Waker::new_unchecked(RawWaker::new(ptr, waker_vtable!(Self)))
3229
}
3330
}
3431
}
@@ -44,10 +41,7 @@ unsafe fn increase_refcount<T: ArcWake>(data: *const()) {
4441

4542
unsafe fn clone_arc_raw<T: ArcWake>(data: *const()) -> RawWaker {
4643
increase_refcount::<T>(data);
47-
RawWaker {
48-
data: data,
49-
vtable: waker_vtable!(T),
50-
}
44+
RawWaker::new(data, waker_vtable!(T))
5145
}
5246

5347
unsafe fn drop_arc_raw<T: ArcWake>(data: *const()) {

0 commit comments

Comments
 (0)