Skip to content

Commit

Permalink
tokio: function new for watch::Sender
Browse files Browse the repository at this point in the history
Added a function that basically calls the watch::channel function and drops the returned receiver
  • Loading branch information
nicflower committed Sep 10, 2023
1 parent 1a9ebf2 commit 3fe3611
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,26 @@ impl<T> Drop for Receiver<T> {
}

impl<T> Sender<T> {
/// 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 no one subscribed to the channel 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
Expand Down

0 comments on commit 3fe3611

Please sign in to comment.