@@ -9,10 +9,14 @@ use crate::ptr;
9
9
/// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
10
10
/// or a [`LocalWaker`] which provides customized wakeup behavior.
11
11
///
12
- /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
13
- ///
14
12
/// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
15
13
/// 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
16
20
#[ derive( PartialEq , Debug ) ]
17
21
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
18
22
pub struct RawWaker {
@@ -355,8 +359,12 @@ impl<'a> ContextBuilder<'a> {
355
359
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
356
360
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
357
361
///
362
+ /// Constructing a `Waker` from a [`RawWaker`] is unsafe.
363
+ /// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
364
+ ///
358
365
/// [`Future::poll()`]: core::future::Future::poll
359
366
/// [`Poll::Pending`]: core::task::Poll::Pending
367
+ /// [`Wake`]: ../../alloc/task/trait.Wake.html
360
368
#[ cfg_attr( not( doc) , repr( transparent) ) ] // work around https://github.com/rust-lang/rust/issues/66401
361
369
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
362
370
pub struct Waker {
@@ -438,9 +446,15 @@ impl Waker {
438
446
439
447
/// Creates a new `Waker` from [`RawWaker`].
440
448
///
449
+ /// # Safety
450
+ ///
441
451
/// The behavior of the returned `Waker` is undefined if the contract defined
442
452
/// 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
444
458
#[ inline]
445
459
#[ must_use]
446
460
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
0 commit comments