pub struct Sender<T> { /* private fields */ }Expand description
Puts new messages onto the channel
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn close(&self)
pub fn close(&self)
Closes the channel for all senders and receivers. No sender will be able to send a message once the channel has been closed. Any senders which have a send in progress but no new send operations will succeed.
Receivers will continue to be able to receive remaining messages. Once they’ve received all messages
Error::Closed will be returned.
§Example
let (tx, mut rx) = channel::<u8>(2)?;
assert!(!rx.is_closed());
assert!(!tx.is_closed());
tx.close();
assert!(rx.is_closed());
assert!(tx.is_closed());Source§impl<T> Sender<T>where
T: Clone,
impl<T> Sender<T>where
T: Clone,
Sourcepub fn send(&self, val: T) -> Result<(), Error>
pub fn send(&self, val: T) -> Result<(), Error>
Put a new value into the channel. This function will almost never block. The underlying implementation means that writes have to be finalised in order meaning that if thread a then b writes to the channel. Thread b will have to wait for thread a to finish as thread a was first to start. Insertion is O(1) and is a very simple operation so hopefully wont be a major issue.
§Errors
Error::ClosedThe channel has been closed by some sender.
§Examples
§Example A
let (tx, mut rx) = channel(2)?;
tx.send(42)?;
let v = rx.try_recv()?;
assert_eq!(v, 42);§Example B
let x = 42;
let (tx, mut rx) = channel(2)?;
tx.send(&x)?;
let v = rx.try_recv()?;
assert_eq!(*v, 42);§Example C (Should not compile)
{
let (tx, mut rx) = channel(2)?;
let x = 42;
tx.send(&x)?;
}Sourcepub fn subscribe(&self) -> Receiver<T>
pub fn subscribe(&self) -> Receiver<T>
Creates a new receiver on the same channel as this sender. The receiver will start at message
id 0 meaning that it’s likely the first call will return Error::Lagged which will cause it
to skip ahead and catch up with the underlying channel.
§Example
let (tx, _) = channel(2)?;
tx.send(42)?;
let mut rx = tx.subscribe();
let v = rx.try_recv()?;
assert_eq!(v, 42);