Skip to content

Commit 7d42a6a

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 e3c631b commit 7d42a6a

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

library/alloc/src/task.rs

+9-3
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

+17-3
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 {
@@ -231,8 +235,12 @@ impl fmt::Debug for Context<'_> {
231235
/// this might be done to wake a future when a blocking function call completes on another
232236
/// thread.
233237
///
238+
/// Constructing a `Waker` from a [`RawWaker`] is unsafe.
239+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
240+
///
234241
/// [`Future::poll()`]: core::future::Future::poll
235242
/// [`Poll::Pending`]: core::task::Poll::Pending
243+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
236244
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
237245
#[stable(feature = "futures_api", since = "1.36.0")]
238246
pub struct Waker {
@@ -312,9 +320,15 @@ impl Waker {
312320

313321
/// Creates a new `Waker` from [`RawWaker`].
314322
///
323+
/// # Safety
324+
///
315325
/// The behavior of the returned `Waker` is undefined if the contract defined
316326
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
317-
/// Therefore this method is unsafe.
327+
///
328+
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
329+
/// cost of a required heap allocation.)
330+
///
331+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
318332
#[inline]
319333
#[must_use]
320334
#[stable(feature = "futures_api", since = "1.36.0")]

0 commit comments

Comments
 (0)