summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync/mpsc/unbounded.rs
diff options
context:
space:
mode:
authorZahari Dichev <zaharidichev@gmail.com>2020-09-25 18:40:31 +0300
committerGitHub <noreply@github.com>2020-09-25 08:40:31 -0700
commit55d932a21fd4c5fa298ca3cfdcb1388dbbf43dd0 (patch)
tree500d0d68a991277b466eabb5b87379bb1037d093 /tokio/src/sync/mpsc/unbounded.rs
parent444660664b96f758610a0e7201a6a1a31a0f2405 (diff)
sync: add `mpsc::Sender::closed` future (#2840)
Adding closed future, makes it possible to select over closed and some other work, so that the task is woken when the channel is closed and can proactively cancel itself. Added a mpsc::Sender::closed future that will become ready when the receiver is closed.
Diffstat (limited to 'tokio/src/sync/mpsc/unbounded.rs')
-rw-r--r--tokio/src/sync/mpsc/unbounded.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/tokio/src/sync/mpsc/unbounded.rs b/tokio/src/sync/mpsc/unbounded.rs
index 59456375..09f71f21 100644
--- a/tokio/src/sync/mpsc/unbounded.rs
+++ b/tokio/src/sync/mpsc/unbounded.rs
@@ -210,4 +210,39 @@ impl<T> UnboundedSender<T> {
}
}
}
+
+ /// Completes when the receiver has dropped.
+ ///
+ /// This allows the producers to get notified when interest in the produced
+ /// values is canceled and immediately stop doing work.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::mpsc;
+ ///
+ /// #[tokio::main]
+ /// async fn main() {
+ /// let (mut tx1, rx) = mpsc::unbounded_channel::<()>();
+ /// let mut tx2 = tx1.clone();
+ /// let mut tx3 = tx1.clone();
+ /// let mut tx4 = tx1.clone();
+ /// let mut tx5 = tx1.clone();
+ /// tokio::spawn(async move {
+ /// drop(rx);
+ /// });
+ ///
+ /// futures::join!(
+ /// tx1.closed(),
+ /// tx2.closed(),
+ /// tx3.closed(),
+ /// tx4.closed(),
+ /// tx5.closed()
+ /// );
+ //// println!("Receiver dropped");
+ /// }
+ /// ```
+ pub async fn closed(&mut self) {
+ self.chan.closed().await
+ }
}