@@ -7,10 +7,14 @@ use crate::ptr;
7
7
/// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
8
8
/// which provides customized wakeup behavior.
9
9
///
10
- /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
11
- ///
12
10
/// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
13
11
/// 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
14
18
#[ derive( PartialEq , Debug ) ]
15
19
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
16
20
pub struct RawWaker {
@@ -235,8 +239,12 @@ impl fmt::Debug for Context<'_> {
235
239
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
236
240
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
237
241
///
242
+ /// Constructing a `Waker` from a [`RawWaker`] is unsafe.
243
+ /// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
244
+ ///
238
245
/// [`Future::poll()`]: core::future::Future::poll
239
246
/// [`Poll::Pending`]: core::task::Poll::Pending
247
+ /// [`Wake`]: ../../alloc/task/trait.Wake.html
240
248
#[ cfg_attr( not( doc) , repr( transparent) ) ] // work around https://github.com/rust-lang/rust/issues/66401
241
249
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
242
250
pub struct Waker {
@@ -318,9 +326,15 @@ impl Waker {
318
326
319
327
/// Creates a new `Waker` from [`RawWaker`].
320
328
///
329
+ /// # Safety
330
+ ///
321
331
/// The behavior of the returned `Waker` is undefined if the contract defined
322
332
/// 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
324
338
#[ inline]
325
339
#[ must_use]
326
340
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
0 commit comments