@@ -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 {
@@ -349,8 +353,12 @@ impl<'a> ContextBuilder<'a> {
349
353
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
350
354
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
351
355
///
356
+ /// Constructing a `Waker` from a [`RawWaker`] is unsafe.
357
+ /// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
358
+ ///
352
359
/// [`Future::poll()`]: core::future::Future::poll
353
360
/// [`Poll::Pending`]: core::task::Poll::Pending
361
+ /// [`Wake`]: ../../alloc/task/trait.Wake.html
354
362
#[ cfg_attr( not( doc) , repr( transparent) ) ] // work around https://github.com/rust-lang/rust/issues/66401
355
363
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
356
364
pub struct Waker {
@@ -432,9 +440,15 @@ impl Waker {
432
440
433
441
/// Creates a new `Waker` from [`RawWaker`].
434
442
///
443
+ /// # Safety
444
+ ///
435
445
/// The behavior of the returned `Waker` is undefined if the contract defined
436
446
/// 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
438
452
#[ inline]
439
453
#[ must_use]
440
454
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
0 commit comments