sync: add watch::Sender::new (#5998)

This commit is contained in:
nicflower 2023-09-19 16:01:36 +02:00 committed by GitHub
parent 804511822b
commit 9d51b76d01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -876,6 +876,27 @@ 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 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