Skip to content

Commit ccd6513

Browse files
committed
Additional doc links and explanation of Wake.
This is intended to clarify: * That `Wake` exists and can be used instead of `RawWaker`. * How to construct a `Waker` when you are looking at `Wake` (which was previously only documented in the example).
1 parent 4a2fe44 commit ccd6513

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

library/alloc/src/task.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::task::Waker;
1919
/// The implementation of waking a task on an executor.
2020
///
2121
/// This trait can be used to create a [`Waker`]. An executor can define an
22-
/// implementation of this trait, and use that to construct a Waker to pass
22+
/// implementation of this trait, and use that to construct a [`Waker`] to pass
2323
/// to the tasks that are executed on that executor.
2424
///
2525
/// This trait is a memory-safe and ergonomic alternative to constructing a
@@ -28,7 +28,13 @@ use core::task::Waker;
2828
/// those for embedded systems) cannot use this API, which is why [`RawWaker`]
2929
/// exists as an alternative for those systems.
3030
///
31-
/// [arc]: ../../std/sync/struct.Arc.html
31+
/// To construct a [`Waker`] from some type `W` implementing this trait,
32+
/// wrap it in an [`Arc<W>`](Arc) and call [`Waker::from()`][wi].
33+
/// It is also possible to convert to [`RawWaker`] in the same way.
34+
///
35+
/// <!-- This impl is reachable from `alloc` but rustdoc only lists it in `std`
36+
/// because `alloc` doesn't reexport `Waker` -->
37+
/// [wi]: ../../std/task/struct.Waker.html#impl-From<Arc<W,+Global>>-for-Waker
3238
///
3339
/// # Examples
3440
///
@@ -100,7 +106,7 @@ pub trait Wake {
100106
#[cfg(target_has_atomic = "ptr")]
101107
#[stable(feature = "wake_trait", since = "1.51.0")]
102108
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
103-
/// Use a `Wake`-able type as a `Waker`.
109+
/// Use a [`Wake`]-able type as a `Waker`.
104110
///
105111
/// No heap allocations or atomic operations are used for this conversion.
106112
fn from(waker: Arc<W>) -> Waker {

library/core/src/task/wake.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ use crate::ptr;
99
/// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
1010
/// or a [`LocalWaker`] which provides customized wakeup behavior.
1111
///
12-
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
13-
///
1412
/// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
1513
/// that customizes the behavior of the `RawWaker`.
14+
///
15+
/// `RawWaker`s are unsafe to use.
16+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
17+
///
18+
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
19+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
1620
#[derive(PartialEq, Debug)]
1721
#[stable(feature = "futures_api", since = "1.36.0")]
1822
pub struct RawWaker {
@@ -349,8 +353,12 @@ impl<'a> ContextBuilder<'a> {
349353
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
350354
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
351355
///
356+
/// Constructing a `Waker` from a [`RawWaker`] is unsafe.
357+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
358+
///
352359
/// [`Future::poll()`]: core::future::Future::poll
353360
/// [`Poll::Pending`]: core::task::Poll::Pending
361+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
354362
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
355363
#[stable(feature = "futures_api", since = "1.36.0")]
356364
pub struct Waker {
@@ -432,9 +440,15 @@ impl Waker {
432440

433441
/// Creates a new `Waker` from [`RawWaker`].
434442
///
443+
/// # Safety
444+
///
435445
/// The behavior of the returned `Waker` is undefined if the contract defined
436446
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
437-
/// Therefore this method is unsafe.
447+
///
448+
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
449+
/// cost of a required heap allocation.)
450+
///
451+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
438452
#[inline]
439453
#[must_use]
440454
#[stable(feature = "futures_api", since = "1.36.0")]

0 commit comments

Comments
 (0)