pub struct ChannelBroker<'a, K: Eq + Hash + Clone> { /* private fields */ }
Expand description
Channel ChannelBroker storing channels of arbitrary types under a key K
Internally, it stores the channel in a hashmap as a Trait object which implements an internal trait and the supplied type of the channel.
Implementations§
Source§impl<'a, K: Eq + Hash + Clone> ChannelBroker<'a, K>
impl<'a, K: Eq + Hash + Clone> ChannelBroker<'a, K>
Sourcepub fn add_channel<T: 'a>(&mut self, id: K)
pub fn add_channel<T: 'a>(&mut self, id: K)
Insert a new channel of type T
into the ChannelBroker with the ID K
This will create a Sender<T>
and Receiver<T>
pair and keep note of the type T
§Examples
use navvy::ChannelBroker;
fn main() {
// Create a broker that uses `usize` as the key
let mut broker: ChannelBroker<usize> = ChannelBroker::new();
// Add a channel of type `String` under the ID `1`
broker.add_channel::<String>(1);
// Add another channel of type `f32` under the ID `32`
broker.add_channel::<f32>(32);
}
Sourcepub fn close_channel(&mut self, id: K)
pub fn close_channel(&mut self, id: K)
“Close” a channel by removing it from the broker.
If there are any senders associated with a removed channel, these will return a
SendError<T>
when calling Sender::send
§Examples
use navvy::ChannelBroker;
fn main() {
let mut broker: ChannelBroker<usize> = ChannelBroker::new();
broker.add_channel::<String>(1);
// Do some work elsewhere
broker.close_channel(1);
}
Sourcepub fn sender<T>(&self, id: K) -> Result<Sender<T>, NavvyError>
pub fn sender<T>(&self, id: K) -> Result<Sender<T>, NavvyError>
Clones a Sender<T>
from the Channel under ID K
§Errors
NavvyError::TypeMismatch
if the suppliedT
does not match what is stored for the channelNavvyError::MissingChannel
if there is no channel under the given ID
§Example
use navvy::ChannelBroker;
fn main() {
let mut broker: ChannelBroker<usize> = ChannelBroker::new();
broker.add_channel::<String>(1);
let sender = broker.sender::<String>(1).unwrap();
std::thread::spawn(move || {
sender.send("Hi there".to_string()).unwrap();
});
}
Sourcepub fn consume_sender<T>(&mut self, id: K) -> Result<Sender<T>, NavvyError>
pub fn consume_sender<T>(&mut self, id: K) -> Result<Sender<T>, NavvyError>
Consumes the Sender<T>
from a Channel meaning that no more senders can be created
from this channel under ID K
This still returns the Sender<T>
but this is considered the “last” sender for the channel which
can be handled in whatever way suits the need
§Errors
NavvyError::TypeMismatch
if the suppliedT
does not match what is stored for the channelNavvyError::SendChannelClosed
if the sender has already been consumedNavvyError::MissingChannel
if there is no channel for the given ID
§Example
use navvy::ChannelBroker;
Sourcepub fn receiver<T>(&self, id: K) -> Result<&Receiver<T>, NavvyError>
pub fn receiver<T>(&self, id: K) -> Result<&Receiver<T>, NavvyError>
Retrieves the Receiver<T>
for the channel under the ID K
If the receiver is called using the Receiver::recv
method then this will endlessly
block if the Sender<T>
has not already been consumed as the ChannelBroker will still hold the
original sender. In these cases, it is best to use the Receiver::try_recv
method instead.
§Errors
NavvyError::TypeMismatch
if the suppliedT
does not match what is stored for the channelNavvyError::MissingChannel
if there is no channel for the given ID
§Example
use navvy::ChannelBroker;
fn main() {
let mut broker: ChannelBroker<usize> = ChannelBroker::new();
broker.add_channel::<String>(1);
let sender = broker.sender::<String>(1).unwrap();
let hdl = std::thread::spawn(move || {
/* Some work here */
sender.send("results".to_string()).unwrap();
});
/* Some more work */
hdl.join().unwrap();
let receiver = broker.receiver::<String>(1).unwrap();
match receiver.try_recv() {
Ok(res) => println!("Result: {res}"),
Err(e) => eprintln!("something went wrong: {e}")
}
}
Sourcepub fn consume_and_fetch_receiver<T>(
&mut self,
id: K,
) -> Result<&Receiver<T>, NavvyError>
pub fn consume_and_fetch_receiver<T>( &mut self, id: K, ) -> Result<&Receiver<T>, NavvyError>
Retrieves the Receiver<T>
for a channel under the ID K
and consumes the Sender<T>
in the process.
This will allow the receiver to call Receiver::recv
without endlessly blocking as the
sender will be consumed from the ChannelBroker.
§Errors
NavvyError::TypeMismatch
if theT
supplied does not match what is stored for the channelNavvyError::MissingChannel
if there is no channel for the given ID
§Example
use navvy::ChannelBroker;
fn main() {
let mut broker: ChannelBroker<usize> = ChannelBroker::new();
broker.add_channel::<u32>(42);
for _ in 0..10 {
let sender = broker.sender::<u32>(42).unwrap();
std::thread::spawn(move || {
/* Do some work */
for i in 0..1000 {
sender.send(i as u32).unwrap();
}
});
}
let receiver = broker.consume_and_fetch_receiver::<u32>(42).unwrap();
while let Ok(value) = receiver.recv() {
println!("Value: {value}");
}
}