pub struct Multiplexer<T>{
pub channels: Arc<Mutex<HashMap<Id, Arc<Sender<T>>>>>,
/* private fields */
}
Expand description
A simple MPMC (one to many) channel Multiplexer that broadcasts to
multiple registered receivers. Multiplexer<T>
itself can be
cloned and used to broadcast using Multiplexer::broadcast()
or Multiplexer::try_broadcast()
. To create a receiving channel,
you can call MultiplexerChannel<T>::from()
and supply the
desired Multiplexer instance, or simply call Multiplexer::channel()
to create a new MultiplexerChannel
instance. The receiving channel
gets unregistered when MultiplexerChannel
is dropped or the
underlying Receiver
is closed.
Fields§
§channels: Arc<Mutex<HashMap<Id, Arc<Sender<T>>>>>
Implementations§
Source§impl<T> Multiplexer<T>
impl<T> Multiplexer<T>
Sourcepub fn new() -> Multiplexer<T>
pub fn new() -> Multiplexer<T>
Create a new Multiplexer instance
Sourcepub fn channel(&self) -> MultiplexerChannel<T>
pub fn channel(&self) -> MultiplexerChannel<T>
Create a new multiplexer receiving channel
Sourcepub async fn broadcast(&self, event: T) -> Result<(), ChannelError<T>>
pub async fn broadcast(&self, event: T) -> Result<(), ChannelError<T>>
Async Multiplexer::broadcast
function that calls Sender::send()
on all registered MultiplexerChannel
instances.
Sourcepub fn try_broadcast(&self, event: T) -> Result<(), ChannelError<T>>
pub fn try_broadcast(&self, event: T) -> Result<(), ChannelError<T>>
A synchronous Multiplexer::try_broadcast
function that calls Sender::try_send()
on all registered MultiplexerChannel
instances.
This function holds a mutex for the duration of the broadcast.
Trait Implementations§
Source§impl<T> Clone for Multiplexer<T>
impl<T> Clone for Multiplexer<T>
Source§fn clone(&self) -> Multiplexer<T>
fn clone(&self) -> Multiplexer<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> Default for Multiplexer<T>
impl<T> Default for Multiplexer<T>
Source§fn default() -> Multiplexer<T>
fn default() -> Multiplexer<T>
Source§impl<T> From<&Multiplexer<T>> for MultiplexerChannel<T>
Create a MultiplexerChannel
from Multiplexer
by reference.
impl<T> From<&Multiplexer<T>> for MultiplexerChannel<T>
Create a MultiplexerChannel
from Multiplexer
by reference.