Skip to content

Commit 3f91f60

Browse files
authored
Unrolled build for rust-lang#116387
Rollup merge of rust-lang#116387 - kpreid:wake-doc, r=cuviper 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).
2 parents 81b757c + a6c91f0 commit 3f91f60

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

library/alloc/src/task.rs

+10-3
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,14 @@ 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()` on that.
33+
/// It is also possible to convert to [`RawWaker`] in the same way.
34+
///
35+
/// <!-- Ideally we'd link to the `From` impl, but rustdoc doesn't generate any page for it within
36+
/// `alloc` because `alloc` neither defines nor re-exports `From` or `Waker`, and we can't
37+
/// link ../../std/task/struct.Waker.html#impl-From%3CArc%3CW,+Global%3E%3E-for-Waker
38+
/// without getting a link-checking error in CI. -->
3239
///
3340
/// # Examples
3441
///
@@ -100,7 +107,7 @@ pub trait Wake {
100107
#[cfg(target_has_atomic = "ptr")]
101108
#[stable(feature = "wake_trait", since = "1.51.0")]
102109
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
103-
/// Use a `Wake`-able type as a `Waker`.
110+
/// Use a [`Wake`]-able type as a `Waker`.
104111
///
105112
/// No heap allocations or atomic operations are used for this conversion.
106113
fn from(waker: Arc<W>) -> Waker {

library/core/src/task/wake.rs

+17-3
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 {
@@ -355,8 +359,12 @@ impl<'a> ContextBuilder<'a> {
355359
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
356360
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
357361
///
362+
/// Constructing a `Waker` from a [`RawWaker`] is unsafe.
363+
/// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
364+
///
358365
/// [`Future::poll()`]: core::future::Future::poll
359366
/// [`Poll::Pending`]: core::task::Poll::Pending
367+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
360368
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
361369
#[stable(feature = "futures_api", since = "1.36.0")]
362370
pub struct Waker {
@@ -438,9 +446,15 @@ impl Waker {
438446

439447
/// Creates a new `Waker` from [`RawWaker`].
440448
///
449+
/// # Safety
450+
///
441451
/// The behavior of the returned `Waker` is undefined if the contract defined
442452
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
443-
/// Therefore this method is unsafe.
453+
///
454+
/// (Authors wishing to avoid unsafe code may implement the [`Wake`] trait instead, at the
455+
/// cost of a required heap allocation.)
456+
///
457+
/// [`Wake`]: ../../alloc/task/trait.Wake.html
444458
#[inline]
445459
#[must_use]
446460
#[stable(feature = "futures_api", since = "1.36.0")]

0 commit comments

Comments
 (0)