Skip to content

Commit 9cc4a81

Browse files
authored
sync: add watch::Sender::sender_count (#6836)
This makes it possible to check if other senders exist. For example If you are using a Sender as a subscriber to get a Receiver and might want to know if the real sender is still running.
1 parent 679d765 commit 9cc4a81

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tokio/src/sync/watch.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,29 @@ impl<T> Sender<T> {
13361336
self.shared.ref_count_rx.load(Relaxed)
13371337
}
13381338

1339+
/// Returns the number of senders that currently exist.
1340+
///
1341+
/// # Examples
1342+
///
1343+
/// ```
1344+
/// use tokio::sync::watch;
1345+
///
1346+
/// #[tokio::main]
1347+
/// async fn main() {
1348+
/// let (tx1, rx) = watch::channel("hello");
1349+
///
1350+
/// assert_eq!(1, tx1.sender_count());
1351+
///
1352+
/// let tx2 = tx1.clone();
1353+
///
1354+
/// assert_eq!(2, tx1.sender_count());
1355+
/// assert_eq!(2, tx2.sender_count());
1356+
/// }
1357+
/// ```
1358+
pub fn sender_count(&self) -> usize {
1359+
self.shared.ref_count_tx.load(Relaxed)
1360+
}
1361+
13391362
/// Returns `true` if senders belong to the same channel.
13401363
///
13411364
/// # Examples

0 commit comments

Comments
 (0)