pub struct Sender<T>(/* private fields */);
Expand description
The sending half of a channel returned by channel
.
The sender can be cloned to send messages from multiple threads or processes.
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn send(&self, message: T)
pub fn send(&self, message: T)
Sends a message to the associated Receiver<T>
.
Does not return a result, so messages can be silently dropped if the receiver has closed or failed. To detect such conditions, include another sender in the message you send so that the receiving thread can use it to send a response.
let (send, mut recv) = channel();
let (response_send, mut response_recv) = channel::<bool>();
send.send((3, response_send));
let (val, response_send) = recv.recv().await.unwrap();
response_send.send(val == 3);
assert_eq!(response_recv.recv().await.unwrap(), true);
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns whether the receiving side of the channel is known to be closed (or failed).
This is useful to determine if there is any point in sending more data
via this port. Note that even if this returns false
messages may still
fail to reach the destination, for example if the receiver is closed
after this method is called but before the message is consumed.
Source§impl<T> Sender<T>where
T: MeshField,
impl<T> Sender<T>where
T: MeshField,
Sourcepub fn bridge(self, receiver: Receiver<T>)
pub fn bridge(self, receiver: Receiver<T>)
Bridges this and recv
together, consuming both self
and recv
. This
makes it so that anything sent to recv
will be directly sent to this
channel’s peer receiver, without a separate relay step. This includes
any data that was previously sent but not yet consumed.
let (outer_send, inner_recv) = channel::<u32>();
let (inner_send, mut outer_recv) = channel::<u32>();
outer_send.send(2);
inner_send.send(1);
inner_send.bridge(inner_recv);
assert_eq!(outer_recv.try_recv().unwrap(), 1);
assert_eq!(outer_recv.try_recv().unwrap(), 2);