[−][src]Struct crossbeam_channel::Select
Selects from a set of channel operations.
Select
allows you to define a set of channel operations, wait until any one of them becomes
ready, and finally execute it. If multiple operations are ready at the same time, a random one
among them is selected.
An operation is considered to be ready if it doesn't have to block. Note that it is ready even when it will simply return an error because the channel is disconnected.
The select!
macro is a convenience wrapper around Select
. However, it cannot select over a
dynamically created list of channel operations.
Once a list of operations has been built with Select
, there are two different ways of
proceeding:
-
Select an operation with
try_select
,select
, orselect_timeout
. If successful, the returned selected operation has already begun and must be completed. If we don't complete it, a panic will occur. -
Wait for an operation to become ready with
try_ready
,ready
, orready_timeout
. If successful, we may attempt to execute the operation, but are not obliged to. In fact, it's possible for another thread to make the operation not ready just before we try executing it, so it's wise to use a retry loop. However, note that these methods might return with success spuriously, so it's a good idea to always double check if the operation is really ready.
Examples
Use select
to receive a message from a list of receivers:
use crossbeam_channel::{Receiver, RecvError, Select}; fn recv_multiple<T>(rs: &[Receiver<T>]) -> Result<T, RecvError> { // Build a list of operations. let mut sel = Select::new(); for r in rs { sel.recv(r); } // Complete the selected operation. let oper = sel.select(); let index = oper.index(); oper.recv(&rs[index]) }
Use ready
to receive a message from a list of receivers:
use crossbeam_channel::{Receiver, RecvError, Select}; fn recv_multiple<T>(rs: &[Receiver<T>]) -> Result<T, RecvError> { // Build a list of operations. let mut sel = Select::new(); for r in rs { sel.recv(r); } loop { // Wait until a receive operation becomes ready and try executing it. let index = sel.ready(); let res = rs[index].try_recv(); // If the operation turns out not to be ready, retry. if let Err(e) = res { if e.is_empty() { continue; } } // Success! return res.map_err(|_| RecvError); } }
Methods
impl<'a> Select<'a>
[src][−]
pub fn new() -> Select<'a>
[src][+]
pub fn send<T>(&mut self, s: &'a Sender<T>) -> usize
[src][+]
pub fn recv<T>(&mut self, r: &'a Receiver<T>) -> usize
[src][+]
pub fn try_select(&mut self) -> Result<SelectedOperation<'a>, TrySelectError>
[src][+]
pub fn select(&mut self) -> SelectedOperation<'a>
[src][+]
pub fn select_timeout(
&mut self,
timeout: Duration
) -> Result<SelectedOperation<'a>, SelectTimeoutError>
[src][+]
&mut self,
timeout: Duration
) -> Result<SelectedOperation<'a>, SelectTimeoutError>
pub fn try_ready(&mut self) -> Result<usize, TryReadyError>
[src][+]
pub fn ready(&mut self) -> usize
[src][+]
pub fn ready_timeout(
&mut self,
timeout: Duration
) -> Result<usize, ReadyTimeoutError>
[src][+]
&mut self,
timeout: Duration
) -> Result<usize, ReadyTimeoutError>
Trait Implementations
impl<'a> Sync for Select<'a>
[src]
impl<'a> Clone for Select<'a>
[src][+]
impl<'a> Send for Select<'a>
[src]
impl<'a> Default for Select<'a>
[src][+]
impl<'a> Debug for Select<'a>
[src][+]
Blanket Implementations
impl<T> From for T
[src][+]
impl<T, U> Into for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src][+]
T: Clone,
impl<T, U> TryFrom for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T> Borrow for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,