Skip to content

Commit 54b2973

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 7a892ab commit 54b2973

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
@@ -14,7 +14,7 @@ use crate::sync::Arc;
1414
/// The implementation of waking a task on an executor.
1515
///
1616
/// This trait can be used to create a [`Waker`]. An executor can define an
17-
/// implementation of this trait, and use that to construct a Waker to pass
17+
/// implementation of this trait, and use that to construct a [`Waker`] to pass
1818
/// to the tasks that are executed on that executor.
1919
///
2020
/// This trait is a memory-safe and ergonomic alternative to constructing a
@@ -23,7 +23,13 @@ use crate::sync::Arc;
2323
/// those for embedded systems) cannot use this API, which is why [`RawWaker`]
2424
/// exists as an alternative for those systems.
2525
///
26-
/// [arc]: ../../std/sync/struct.Arc.html
26+
/// To construct a [`Waker`] from some type `W` implementing this trait,
27+
/// wrap it in an [`Arc<W>`](Arc) and call [`Waker::from()`][wi].
28+
/// It is also possible to convert to [`RawWaker`] in the same way.
29+
///
30+
/// <!-- This impl is reachable from `alloc` but rustdoc only lists it in `std`
31+
/// because `alloc` doesn't reexport `Waker` -->
32+
/// [wi]: ../../std/task/struct.Waker.html#impl-From<Arc<W,+Global>>-for-Waker
2733
///
2834
/// # Examples
2935
///
@@ -94,7 +100,7 @@ pub trait Wake {
94100

95101
#[stable(feature = "wake_trait", since = "1.51.0")]
96102
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
97-
/// Use a `Wake`-able type as a `Waker`.
103+
/// Use a [`Wake`]-able type as a `Waker`.
98104
///
99105
/// No heap allocations or atomic operations are used for this conversion.
100106
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
@@ -7,10 +7,14 @@ use crate::ptr;
77
/// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
88
/// which provides customized wakeup behavior.
99
///
10-
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
11-
///
1210
/// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
1311
/// that customizes the behavior of the `RawWaker`.
12+
///
13+
/// `RawWaker`s are unsafe to use.
14+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
15+
///
16+
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
17+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
1418
#[derive(PartialEq, Debug)]
1519
#[stable(feature = "futures_api", since = "1.36.0")]
1620
pub struct RawWaker {
@@ -235,8 +239,12 @@ impl fmt::Debug for Context<'_> {
235239
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
236240
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
237241
///
242+
/// Constructing a `Waker` from a [`RawWaker`] is unsafe.
243+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
244+
///
238245
/// [`Future::poll()`]: core::future::Future::poll
239246
/// [`Poll::Pending`]: core::task::Poll::Pending
247+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
240248
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
241249
#[stable(feature = "futures_api", since = "1.36.0")]
242250
pub struct Waker {
@@ -318,9 +326,15 @@ impl Waker {
318326

319327
/// Creates a new `Waker` from [`RawWaker`].
320328
///
329+
/// # Safety
330+
///
321331
/// The behavior of the returned `Waker` is undefined if the contract defined
322332
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
323-
/// Therefore this method is unsafe.
333+
///
334+
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
335+
/// cost of a required heap allocation.)
336+
///
337+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
324338
#[inline]
325339
#[must_use]
326340
#[stable(feature = "futures_api", since = "1.36.0")]

0 commit comments

Comments
 (0)