@@ -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 {
@@ -231,8 +235,12 @@ impl fmt::Debug for Context<'_> {
231
235
/// this might be done to wake a future when a blocking function call completes on another
232
236
/// thread.
233
237
///
238
+ /// Constructing a `Waker` from a [`RawWaker`] is unsafe.
239
+ /// Implementing the [`Wake`] trait is a safe alternative that requires memory allocation.
240
+ ///
234
241
/// [`Future::poll()`]: core::future::Future::poll
235
242
/// [`Poll::Pending`]: core::task::Poll::Pending
243
+ /// [`Wake`]: ../../alloc/task/trait.Wake.html
236
244
#[ cfg_attr( not( doc) , repr( transparent) ) ] // work around https://github.com/rust-lang/rust/issues/66401
237
245
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
238
246
pub struct Waker {
@@ -312,9 +320,15 @@ impl Waker {
312
320
313
321
/// Creates a new `Waker` from [`RawWaker`].
314
322
///
323
+ /// # Safety
324
+ ///
315
325
/// The behavior of the returned `Waker` is undefined if the contract defined
316
326
/// 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
318
332
#[ inline]
319
333
#[ must_use]
320
334
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
0 commit comments