Struct crossbeam_channel::Sender
[−]
[src]
pub struct Sender<T>(_);
The sending half of a channel.
Senders can be cloned and shared among multiple threads.
Examples
use std::thread; use crossbeam_channel::unbounded; let (tx1, rx) = unbounded(); let tx2 = tx1.clone(); thread::spawn(move || { tx1.send(1).unwrap(); }); thread::spawn(move || { tx2.send(2).unwrap(); }); let msg1 = rx.recv().unwrap(); let msg2 = rx.recv().unwrap(); assert_eq!(3, msg1 + msg2);
Methods
impl<T> Sender<T>
[src]
fn try_send(&self, msg: T) -> Result<(), TrySendError<T>>
[src]
Attempts to send a message into the channel without blocking.
This method will either send a message into the channel immediately, or return an error if the channel is full or disconnected. The returned error contains the original message.
If called on a zero-capacity channel, this method will send a message the message only if there happens to be a receive operation on the other side of the channel at the same time.
Examples
use crossbeam_channel::{bounded, TrySendError}; let (tx, rx) = bounded(1); assert_eq!(tx.try_send(1), Ok(())); assert_eq!(tx.try_send(2), Err(TrySendError::Full(2))); drop(rx); assert_eq!(tx.try_send(2), Err(TrySendError::Disconnected(2)));
fn send(&self, msg: T) -> Result<(), SendError<T>>
[src]
Sends a message into the channel, blocking if the channel is full.
If the channel is full (its capacity is fully utilized), this call will block until the send operation can proceed. If the channel is (or becomes) disconnected, this call will wake up and return an error.
If called on a zero-capacity channel, this method will wait for a receive operation to appear on the other side of the channel.
Examples
use std::thread; use std::time::Duration; use crossbeam_channel::{bounded, SendError}; let (tx, rx) = bounded(1); assert_eq!(tx.send(1), Ok(())); thread::spawn(move || { assert_eq!(rx.recv(), Ok(1)); thread::sleep(Duration::from_secs(1)); drop(rx); }); assert_eq!(tx.send(2), Ok(())); assert_eq!(tx.send(3), Err(SendError(3)));
fn send_timeout(
&self,
msg: T,
timeout: Duration
) -> Result<(), SendTimeoutError<T>>
[src]
&self,
msg: T,
timeout: Duration
) -> Result<(), SendTimeoutError<T>>
Sends a message into the channel, blocking if the channel is full for a limited time.
If the channel is full (its capacity is fully utilized), this call will block until the
send operation can proceed. If the channel is (or becomes) disconnected, or if it waits for
longer than timeout
, this call will wake up and return an error.
If called on a zero-capacity channel, this method will wait for a receive operation to appear on the other side of the channel.
Examples
use std::thread; use std::time::Duration; use crossbeam_channel::{unbounded, RecvTimeoutError}; let (tx, rx) = unbounded(); thread::spawn(move || { thread::sleep(Duration::from_secs(1)); tx.send(5).unwrap(); drop(tx); }); assert_eq!(rx.recv_timeout(Duration::from_millis(500)), Err(RecvTimeoutError::Timeout)); assert_eq!(rx.recv_timeout(Duration::from_secs(1)), Ok(5)); assert_eq!(rx.recv_timeout(Duration::from_secs(1)), Err(RecvTimeoutError::Disconnected));
fn is_empty(&self) -> bool
[src]
Returns true
if the channel is empty.
Zero-capacity channels are always empty.
Examples
use crossbeam_channel::unbounded; let (tx, rx) = unbounded(); assert!(tx.is_empty()); tx.send(0).unwrap(); assert!(!tx.is_empty()); // Drop the only receiver, thus disconnecting the channel. drop(rx); // Even a disconnected channel can be non-empty. assert!(!tx.is_empty());
fn len(&self) -> usize
[src]
Returns the number of messages in the channel.
Examples
use crossbeam_channel::unbounded; let (tx, rx) = unbounded(); assert_eq!(tx.len(), 0); tx.send(1).unwrap(); tx.send(2).unwrap(); assert_eq!(tx.len(), 2);
fn capacity(&self) -> Option<usize>
[src]
If the channel is bounded, returns its capacity.
Examples
use crossbeam_channel::{bounded, unbounded}; let (tx, _) = unbounded::<i32>(); assert_eq!(tx.capacity(), None); let (tx, _) = bounded::<i32>(5); assert_eq!(tx.capacity(), Some(5)); let (tx, _) = bounded::<i32>(0); assert_eq!(tx.capacity(), Some(0));
fn is_disconnected(&self) -> bool
[src]
Returns true
if the channel is disconnected (i.e. there are no receivers).
Examples
use crossbeam_channel::unbounded; let (tx, rx) = unbounded::<i32>(); assert!(!tx.is_disconnected()); drop(rx); assert!(tx.is_disconnected());
Trait Implementations
impl<T: Send> Send for Sender<T>
[src]
impl<T: Send> Sync for Sender<T>
[src]
impl<T> Drop for Sender<T>
[src]
impl<T> Clone for Sender<T>
[src]
fn clone(&self) -> Self
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more