Skip to content

Commit 3dd36d3

Browse files
committed
review: reference is_terminated() in documentation
NB: this commit means that this branch (tokio-rs#7153) is dependent upon interfaces proposed in (tokio-rs#7152). pr tokio-rs#7152 proposes a `is_terminated()` method for the oneshot channel's receiver. that is the proper method to use to check whether or not it is safe to poll a oneshot receiver as a future. this commit adds a note cross-referencing this method to the documentation of `is_empty()`, to help mitigate misuse. this method only reports whether a value is currently waiting in the channel; a channel that has already received and yielded a value to its receiver is also empty, but would panic when polled. Signed-off-by: katelyn martin <[email protected]>
1 parent b7a0070 commit 3dd36d3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tokio/src/sync/oneshot.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,14 @@ impl<T> Receiver<T> {
996996
///
997997
/// This method returns `true` if the channel has no messages.
998998
///
999+
/// It is not necessarily safe to poll an empty receiver, which may have
1000+
/// already yielded a value. Use [`is_terminated()`][Self::is_terminated]
1001+
/// to check whether or not a receiver can be safely polled, instead.
1002+
///
9991003
/// # Examples
10001004
///
1005+
/// Sending a value.
1006+
///
10011007
/// ```
10021008
/// use tokio::sync::oneshot;
10031009
///
@@ -1013,6 +1019,25 @@ impl<T> Receiver<T> {
10131019
/// assert!(rx.is_empty());
10141020
/// }
10151021
/// ```
1022+
///
1023+
/// Dropping the sender.
1024+
///
1025+
/// ```
1026+
/// use tokio::sync::oneshot;
1027+
///
1028+
/// #[tokio::main]
1029+
/// async fn main() {
1030+
/// let (tx, mut rx) = oneshot::channel::<()>();
1031+
///
1032+
/// // A channel is empty if the sender is dropped.
1033+
/// drop(tx);
1034+
/// assert!(rx.is_empty());
1035+
///
1036+
/// // A closed channel still yields an error, however.
1037+
/// (&mut rx).await.expect_err("should yield an error");
1038+
/// assert!(rx.is_empty());
1039+
/// }
1040+
/// ```
10161041
pub fn is_empty(&self) -> bool {
10171042
let Some(inner) = self.inner.as_ref() else {
10181043
// The channel has already terminated.

0 commit comments

Comments
 (0)