Struct Sender
struct Sender<T> { ... }
Sends values to the associated Receiver.
Instances are created by the channel function.
Implementations
impl<T> Sender<T>
fn new(init: T) -> SelfCreates the sending-half of the
watchchannel.See documentation of
watch::channelfor errors when calling this function. Beware that attempting to send a value when there are no receivers will return an error.Examples
let sender = new; assert!; let _rec = sender.subscribe; assert!;fn send(self: &Self, value: T) -> Result<(), error::SendError<T>>Sends a new value via the channel, notifying all receivers.
This method fails if the channel is closed, which is the case when every receiver has been dropped. It is possible to reopen the channel using the
subscribemethod. However, whensendfails, the value isn't made available for future receivers (but returned with theSendError).To always make a new value available for future receivers, even if no receiver currently exists, one of the other send methods (
send_if_modified,send_modify, orsend_replace) can be used instead.fn send_modify<F>(self: &Self, modify: F) where F: FnOnce(&mut T)Modifies the watched value unconditionally in-place, notifying all receivers.
This can be useful for modifying the watched value, without having to allocate a new instance. Additionally, this method permits sending values even when there are no receivers.
Prefer to use the more versatile function [
Self::send_if_modified()] if the value is only modified conditionally during the mutable borrow to prevent unneeded change notifications for unmodified values.Panics
This function panics when the invocation of the
modifyclosure panics. No receivers are notified when panicking. All changes of the watched value applied by the closure before panicking will be visible in subsequent calls toborrow.Examples
use watch; let = channel; state_tx.send_modify; assert_eq!;fn send_if_modified<F>(self: &Self, modify: F) -> bool where F: FnOnce(&mut T) -> boolModifies the watched value conditionally in-place, notifying all receivers only if modified.
This can be useful for modifying the watched value, without having to allocate a new instance. Additionally, this method permits sending values even when there are no receivers.
The
modifyclosure must returntrueif the value has actually been modified during the mutable borrow. It should only returnfalseif the value is guaranteed to be unmodified despite the mutable borrow.Receivers are only notified if the closure returned
true. If the closure has modified the value but returnedfalsethis results in a silent modification, i.e. the modified value will be visible in subsequent calls toborrow, but receivers will not receive a change notification.Returns the result of the closure, i.e.
trueif the value has been modified andfalseotherwise.Panics
This function panics when the invocation of the
modifyclosure panics. No receivers are notified when panicking. All changes of the watched value applied by the closure before panicking will be visible in subsequent calls toborrow.Examples
use watch; let = channel; let inc_counter_if_odd = ; assert_eq!; assert!; assert!; assert!; assert_eq!; assert!; assert!; assert!; assert_eq!;fn send_replace(self: &Self, value: T) -> TSends a new value via the channel, notifying all receivers and returning the previous value in the channel.
This can be useful for reusing the buffers inside a watched value. Additionally, this method permits sending values even when there are no receivers.
Examples
use watch; let = channel; assert_eq!; assert_eq!;fn borrow(self: &Self) -> Ref<'_, T>Returns a reference to the most recently sent value
Outstanding borrows hold a read lock on the inner value. This means that long-lived borrows could cause the producer half to block. It is recommended to keep the borrow as short-lived as possible. Additionally, if you are running in an environment that allows
!Sendfutures, you must ensure that the returnedReftype is never held alive across an.awaitpoint, otherwise, it can lead to a deadlock.Examples
use watch; let = channel; assert_eq!;fn is_closed(self: &Self) -> boolChecks if the channel has been closed. This happens when all receivers have dropped.
Examples
let = channel; assert!; drop; assert!;async fn closed(self: &Self)Completes when all receivers have dropped.
This allows the producer to get notified when interest in the produced values is canceled and immediately stop doing work. Once a channel is closed, the only way to reopen it is to call
Sender::subscribeto get a new receiver.If the channel becomes closed for a brief amount of time (e.g., the last receiver is dropped and then
subscribeis called), then this call toclosedmight return, but it is also possible that it does not "notice" that the channel was closed for a brief amount of time.Cancel safety
This method is cancel safe.
Examples
use watch; asyncfn subscribe(self: &Self) -> Receiver<T>Creates a new
Receiverconnected to thisSender.All messages sent before this call to
subscribeare initially marked as seen by the newReceiver.This method can be called even if there are no other receivers. In this case, the channel is reopened.
Examples
The new channel will receive messages sent on this
Sender.use watch; asyncThe most recent message is considered seen by the channel, so this test is guaranteed to pass.
use watch; use Duration; asyncfn receiver_count(self: &Self) -> usizeReturns the number of receivers that currently exist.
Examples
use watch; asyncfn sender_count(self: &Self) -> usizeReturns the number of senders that currently exist.
Examples
use watch; asyncfn same_channel(self: &Self, other: &Self) -> boolReturns
trueif senders belong to the same channel.Examples
let = channel; let tx2 = tx.clone; assert!; let = channel; assert!;
impl<T> Any for Sender<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Sender<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Sender<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Clone for Sender<T>
fn clone(self: &Self) -> Self
impl<T> CloneToUninit for Sender<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> Drop for Sender<T>
fn drop(self: &mut Self)
impl<T> Freeze for Sender<T>
impl<T> From for Sender<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Sender<T>
impl<T> Send for Sender<T>
impl<T> Sync for Sender<T>
impl<T> ToOwned for Sender<T>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> Unpin for Sender<T>
impl<T> UnwindSafe for Sender<T>
impl<T, U> Into for Sender<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Sender<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Sender<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::fmt::Debug> Debug for Sender<T>
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
impl<T: Default> Default for Sender<T>
fn default() -> Self