Skip to content

Commit b554680

Browse files
committed
Waker and LocalWaker docs: Warn against a particular, likely, abuse
In pers. comm with colleagues, it became clear that even fairly experienced Rust programmers can be confused about the behaviour of Futures. No-op wakers seem like a superficially attractive answer for calling a poll method when no waker is available. So, at least add some docs warning of the problems. This may even save someone using Stable, since they may see the warning and choose a better approach, rather than simply writing their own no-op waker.
1 parent c404406 commit b554680

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/core/src/task/wake.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@ impl Waker {
541541
///
542542
/// If an owned `Waker` is needed, `clone()` this one.
543543
///
544+
/// # Warning
545+
///
546+
/// Do not use this function as a way to be able to call `poll` methods,
547+
/// in production code,
548+
/// from within a non-`async`, non-`poll`, function.
549+
///
550+
/// Using a no-op waker for that purpose would cause wakeups to be lost:
551+
/// the future would store only the no-op waker,
552+
/// replacing the proper waker for the waiting task.
553+
///
544554
/// # Examples
545555
///
546556
/// ```
@@ -798,6 +808,16 @@ impl LocalWaker {
798808
///
799809
/// If an owned `LocalWaker` is needed, `clone()` this one.
800810
///
811+
/// # Warning
812+
///
813+
/// Do not use this function as a way to be able to call `poll` methods,
814+
/// in production code,
815+
/// from within a non-`async`, non-`poll`, function.
816+
///
817+
/// Using a no-op waker for that purpose would cause wakeups to be lost:
818+
/// the future would store only the no-op waker,
819+
/// replacing the proper waker for the waiting task.
820+
///
801821
/// # Examples
802822
///
803823
/// ```

0 commit comments

Comments
 (0)