Skip to content

Commit eccb5e7

Browse files
committed
docs: remove recommendations to use LocalWaker in stable API documentation
1 parent c67a446 commit eccb5e7

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

library/core/src/task/wake.rs

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl RawWaker {
3939
/// thread safe type such as an `[Arc]<T: Send + Sync>`
4040
/// when used to construct a [`Waker`]. This restriction is lifted when
4141
/// constructing a [`LocalWaker`], which allows using types that do not implement
42-
/// <code>[Send] + [Sync]</code> like `[Rc]<T: !Send + !Sync>`.
42+
/// <code>[Send] + [Sync]</code> like `[Rc]<T>`.
4343
///
4444
/// The `vtable` customizes the behavior of a `Waker` which gets created
4545
/// from a `RawWaker`. For each operation on the `Waker`, the associated
@@ -240,16 +240,6 @@ impl<'a> Context<'a> {
240240
}
241241

242242
/// Returns a reference to the [`Waker`] for the current task.
243-
///
244-
/// Note that if the waker does not need to be sent across threads, it
245-
/// is preferable to call `local_waker`, which is more portable and
246-
/// potentially more efficient.
247-
///
248-
/// # Panics
249-
/// This function will panic if no `Waker` was set on the context. This happens if
250-
/// the executor does not support working with thread safe wakers. An alternative
251-
/// may be to call [`.local_waker()`](Context::local_waker) instead. For a fallible
252-
/// version of this function see [`.try_waker()`](Context::try_waker).
253243
#[inline]
254244
#[must_use]
255245
#[stable(feature = "futures_api", since = "1.36.0")]
@@ -396,7 +386,7 @@ impl<'a> ContextBuilder<'a> {
396386
impl<'a> From<&mut Context<'a>> for ContextBuilder<'a> {
397387
#[inline]
398388
fn from(value: &mut Context<'a>) -> Self {
399-
let Context { waker, local_waker, .. } = *value;
389+
let Context { waker, local_waker, _marker, _marker2 } = *value;
400390
ContextBuilder { waker, local_waker }
401391
}
402392
}
@@ -415,8 +405,7 @@ impl<'a> From<&mut Context<'a>> for ContextBuilder<'a> {
415405
/// Implements [`Clone`], [`Send`], and [`Sync`]; therefore, a waker may be invoked
416406
/// from any thread, including ones not in any way managed by the executor. For example,
417407
/// this might be done to wake a future when a blocking function call completes on another
418-
/// thread. If the waker does not need to be moved across threads, it is better to use
419-
/// [`LocalWaker`], which the executor may use to skip unnecessary memory synchronization.
408+
/// thread.
420409
///
421410
/// Note that it is preferable to use `waker.clone_from(&new_waker)` instead
422411
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
@@ -656,19 +645,6 @@ pub struct LocalWaker {
656645
impl Unpin for LocalWaker {}
657646

658647
impl LocalWaker {
659-
/// Creates a new `LocalWaker` from [`RawWaker`].
660-
///
661-
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
662-
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
663-
/// Therefore this method is unsafe.
664-
#[inline]
665-
#[must_use]
666-
#[stable(feature = "futures_api", since = "1.36.0")]
667-
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
668-
pub const unsafe fn from_raw(waker: RawWaker) -> LocalWaker {
669-
Self { waker }
670-
}
671-
672648
/// Wake up the task associated with this `LocalWaker`.
673649
///
674650
/// As long as the executor keeps running and the task is not finished, it is
@@ -703,6 +679,37 @@ impl LocalWaker {
703679
unsafe { (wake)(data) };
704680
}
705681

682+
/// Wake up the task associated with this `LocalWaker` without consuming the `LocalWaker`.
683+
///
684+
/// This is similar to [`wake()`](Self::wake), but may be slightly less efficient in
685+
/// the case where an owned `Waker` is available. This method should be preferred to
686+
/// calling `waker.clone().wake()`.
687+
#[inline]
688+
#[stable(feature = "futures_api", since = "1.36.0")]
689+
pub fn wake_by_ref(&self) {
690+
// The actual wakeup call is delegated through a virtual function call
691+
// to the implementation which is defined by the executor.
692+
693+
// SAFETY: see `wake`
694+
unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
695+
}
696+
697+
/// Returns `true` if this `LocalWaker` and another `LocalWaker` would awake the same task.
698+
///
699+
/// This function works on a best-effort basis, and may return false even
700+
/// when the `Waker`s would awaken the same task. However, if this function
701+
/// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
702+
///
703+
/// This function is primarily used for optimization purposes — for example,
704+
/// this type's [`clone_from`](Self::clone_from) implementation uses it to
705+
/// avoid cloning the waker when they would wake the same task anyway.
706+
#[inline]
707+
#[must_use]
708+
#[stable(feature = "futures_api", since = "1.36.0")]
709+
pub fn will_wake(&self, other: &LocalWaker) -> bool {
710+
self.waker == other.waker
711+
}
712+
706713
/// Creates a new `LocalWaker` that does nothing when `wake` is called.
707714
///
708715
/// This is mostly useful for writing tests that need a [`Context`] to poll
@@ -733,43 +740,25 @@ impl LocalWaker {
733740
WAKER
734741
}
735742

736-
/// Get a reference to the underlying [`RawWaker`].
737-
#[inline]
738-
#[must_use]
739-
#[unstable(feature = "waker_getters", issue = "96992")]
740-
pub fn as_raw(&self) -> &RawWaker {
741-
&self.waker
742-
}
743-
744-
/// Returns `true` if this `LocalWaker` and another `LocalWaker` would awake the same task.
745-
///
746-
/// This function works on a best-effort basis, and may return false even
747-
/// when the `Waker`s would awaken the same task. However, if this function
748-
/// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
743+
/// Creates a new `LocalWaker` from [`RawWaker`].
749744
///
750-
/// This function is primarily used for optimization purposes — for example,
751-
/// this type's [`clone_from`](Self::clone_from) implementation uses it to
752-
/// avoid cloning the waker when they would wake the same task anyway.
745+
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
746+
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
747+
/// Therefore this method is unsafe.
753748
#[inline]
754749
#[must_use]
755750
#[stable(feature = "futures_api", since = "1.36.0")]
756-
pub fn will_wake(&self, other: &LocalWaker) -> bool {
757-
self.waker == other.waker
751+
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
752+
pub const unsafe fn from_raw(waker: RawWaker) -> LocalWaker {
753+
Self { waker }
758754
}
759755

760-
/// Wake up the task associated with this `LocalWaker` without consuming the `LocalWaker`.
761-
///
762-
/// This is similar to [`wake()`](Self::wake), but may be slightly less efficient in
763-
/// the case where an owned `Waker` is available. This method should be preferred to
764-
/// calling `waker.clone().wake()`.
756+
/// Get a reference to the underlying [`RawWaker`].
765757
#[inline]
766-
#[stable(feature = "futures_api", since = "1.36.0")]
767-
pub fn wake_by_ref(&self) {
768-
// The actual wakeup call is delegated through a virtual function call
769-
// to the implementation which is defined by the executor.
770-
771-
// SAFETY: see `wake`
772-
unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
758+
#[must_use]
759+
#[unstable(feature = "waker_getters", issue = "96992")]
760+
pub fn as_raw(&self) -> &RawWaker {
761+
&self.waker
773762
}
774763
}
775764
#[unstable(feature = "local_waker", issue = "118959")]

0 commit comments

Comments
 (0)