Struct ChannelBroker

Source
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>

Source

pub fn new() -> Self

Create a new ChannelBroker which holds Channels under the ID of type K

Source

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);
}
Source

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);
}
Source

pub fn sender<T>(&self, id: K) -> Result<Sender<T>, NavvyError>

Clones a Sender<T> from the Channel under ID K

§Errors
§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();
    });
}
Source

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
§Example
use navvy::ChannelBroker;
Source

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
§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}")
    }
}
Source

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
§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}");
    }
}

Auto Trait Implementations§

§

impl<'a, K> Freeze for ChannelBroker<'a, K>

§

impl<'a, K> !RefUnwindSafe for ChannelBroker<'a, K>

§

impl<'a, K> !Send for ChannelBroker<'a, K>

§

impl<'a, K> !Sync for ChannelBroker<'a, K>

§

impl<'a, K> Unpin for ChannelBroker<'a, K>
where K: Unpin,

§

impl<'a, K> !UnwindSafe for ChannelBroker<'a, K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.