From a1c4cf6889f69dc335a3f0b42b29e4d9a081005d Mon Sep 17 00:00:00 2001 From: Matthias Einwag Date: Wed, 6 Feb 2019 22:56:33 -0800 Subject: [PATCH] Change RawWaker constructor to const fn --- src/libcore/task/wake.rs | 28 +++++++++++++++++++++---- src/test/run-pass/auxiliary/arc_wake.rs | 10 ++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/libcore/task/wake.rs b/src/libcore/task/wake.rs index a877e033bc6..21f0a8cea41 100644 --- a/src/libcore/task/wake.rs +++ b/src/libcore/task/wake.rs @@ -19,9 +19,29 @@ pub struct RawWaker { /// that is associated with the task. /// The value of this field gets passed to all functions that are part of /// the vtable as the first parameter. - pub data: *const (), + data: *const (), /// Virtual function pointer table that customizes the behavior of this waker. - pub vtable: &'static RawWakerVTable, + vtable: &'static RawWakerVTable, +} + +impl RawWaker { + /// Creates a new `RawWaker` 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 poiner will get passed to all functions that are part + /// of the `vtable` as the first parameter. + /// + /// The `vtable` customizes the behavior of a `Waker` which gets created + /// from a `RawWaker`. For each operation on the `Waker`, the associated + /// function in the `vtable` of the underlying `RawWaker` will be called. + pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker { + RawWaker { + data, + vtable, + } + } } /// A virtual function pointer table (vtable) that specifies the behavior @@ -102,8 +122,8 @@ pub fn will_wake(&self, other: &Waker) -> bool { /// Creates a new `Waker` from [`RawWaker`]. /// /// The behavior of the returned `Waker` is undefined if the contract defined - /// in [RawWaker]'s documentation is not upheld. Therefore this method is - /// unsafe. + /// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld. + /// Therefore this method is unsafe. pub unsafe fn new_unchecked(waker: RawWaker) -> Waker { Waker { waker, diff --git a/src/test/run-pass/auxiliary/arc_wake.rs b/src/test/run-pass/auxiliary/arc_wake.rs index 0baaa378046..034e378af7f 100644 --- a/src/test/run-pass/auxiliary/arc_wake.rs +++ b/src/test/run-pass/auxiliary/arc_wake.rs @@ -25,10 +25,7 @@ fn into_waker(wake: Arc) -> Waker where Self: Sized let ptr = Arc::into_raw(wake) as *const(); unsafe { - Waker::new_unchecked(RawWaker{ - data: ptr, - vtable: waker_vtable!(Self), - }) + Waker::new_unchecked(RawWaker::new(ptr, waker_vtable!(Self))) } } } @@ -44,10 +41,7 @@ unsafe fn increase_refcount(data: *const()) { unsafe fn clone_arc_raw(data: *const()) -> RawWaker { increase_refcount::(data); - RawWaker { - data: data, - vtable: waker_vtable!(T), - } + RawWaker::new(data, waker_vtable!(T)) } unsafe fn drop_arc_raw(data: *const()) {