From 9d51b76d017cfef12e053760fa31f0845c214e3a Mon Sep 17 00:00:00 2001 From: nicflower <50106721+nicflower@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:01:36 +0200 Subject: [PATCH] sync: add `watch::Sender::new` (#5998) --- tokio/src/sync/watch.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 2b4fd561e96..9e7cd1a7966 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -876,6 +876,27 @@ impl Drop for Receiver { } impl Sender { + /// Creates the sending-half of the [`watch`] channel. + /// + /// See documentation of [`watch::channel`] for errors when calling this function. + /// Beware that attempting to send a value when there are no receivers will + /// return an error. + /// + /// [`watch`]: crate::sync::watch + /// [`watch::channel`]: crate::sync::watch + /// + /// # Examples + /// ``` + /// let sender = tokio::sync::watch::Sender::new(0u8); + /// assert!(sender.send(3).is_err()); + /// let _rec = sender.subscribe(); + /// assert!(sender.send(4).is_ok()); + /// ``` + pub fn new(init: T) -> Self { + let (tx, _) = channel(init); + tx + } + /// Sends a new value via the channel, notifying all receivers. /// /// This method fails if the channel is closed, which is the case when