pub struct Funnel<T> { /* private fields */ }
Expand description
Funnel
maintains a collection of
Receivers
which it collects messages from to output to a single source.
Implementations§
Source§impl<T> Funnel<T>
impl<T> Funnel<T>
Sourcepub fn new() -> Funnel<T>
pub fn new() -> Funnel<T>
Creates a new Funnel
that received messages of type T
with no input sources.
Sourcepub fn push(&mut self, receiver: Receiver<T>)
pub fn push(&mut self, receiver: Receiver<T>)
Adds a new input source to the Funnel
, which it takes ownership over.
Sourcepub fn remove(&mut self, index: usize) -> Receiver<T>
pub fn remove(&mut self, index: usize) -> Receiver<T>
Removes a specific input source at a given index
.
This method is useful in situations where the owner of the Funnel
is
keeping track of the indexes of Receiver
s. It is also useful for
removing Receiver
s that can no longer be read from, indicated by any
errors returned by a call to recv()
.
Sourcepub fn add_receiver(&mut self) -> Sender<T>
pub fn add_receiver(&mut self) -> Sender<T>
Creates a new channel, whose Receiver
will be managed by the funnel.
Sourcepub fn recv(&mut self) -> (Option<T>, Vec<(FunnelError, usize)>)
pub fn recv(&mut self) -> (Option<T>, Vec<(FunnelError, usize)>)
Attempts to wait for a value on the oldest Receiver
not already received from.
Successive calls to recv()
result in calls to the recv()
method of successive
Receiver
s managed by the funnel. In doing so, channels are read from in an even
distribution. As soon as a value is successfully received, it will be returned as
the first element of the tuple returned. recv()
will accumulate a
Vec containing any errors that
may have occurred trying to read from Receiver
s, as well as the index of those
Receiver
s, allowing users to remove()
them if desired. Note that the returned
error type is FunnelError
, which may take on the NoSourcesError
variant. In such
cases, the index accompanying the error will be 0, however this error is returned to
signify that the length of the sources container is 0 and thus remove()
ing index 0
would cause an error.
§Examples
use self::funnel::Funnel;
use std::thread;
let mut fun = Funnel::new();
let writer1 = fun.add_receiver();
let writer2 = fun.add_receiver();
thread::spawn(move || {
let _ = writer1.send(32).unwrap();
});
thread::spawn(move || {
let _ = writer2.send(64).unwrap();
});
assert!(match fun.recv() {
(Some(read_value), errors) => read_value == 32 && errors.len() == 0,
_ => false,
});
assert!(match fun.recv() {
(Some(read_value), errors) => read_value == 64 && errors.len() == 0,
_ => false,
});