[−][src]Struct bottles::dispatcher::Dispatcher
Dispatches messages among subscribers
A subscriber is a closure which takes exactly one argument: Rc<T>
where T
is the type of
message to receive.
Dispatcher
is type safe by having a mapping of the subscriber's expected type TypeId's and
the closures handling them.
Methods
impl Dispatcher
[src]
pub fn new() -> Self
[src]
Creates a new Dispatcher
.
pub fn register<T: 'static>(&mut self)
[src]
Registers the type to be available for subscribing
It is a design choice that subscribing for a message of type that has not yet been registered will result in a panic (error in the future).
Safety
This function uses unsafe internally, because of the need of mapping a Rc<T>
into a
*const ()
for homogeneous storage.
The transmutation fo *const ()
is done in a closure that knows the target type Rc<T>
and
the usage is safe because the Dispatcher type holds a mapping between a TypeId
and
the corresponding closure which deals with the type itself.
pub fn subscribe<F, T>(&mut self, f: F) where
F: FnMut(Rc<T>) + 'static,
T: 'static,
[src]
F: FnMut(Rc<T>) + 'static,
T: 'static,
Adds a subscriber for a message of type T
.
Whenever a value of type Rc<T>
will be dispatched the provided callback will be executed.
Example
use bottles::dispatcher::Dispatcher; struct Message {} let mut dispatcher = Dispatcher::new(); dispatcher.register::<Message>(); dispatcher.subscribe(|message: Rc<Message>| { // react to the message });
pub fn dispatch<T: 'static>(&mut self, msg: Rc<T>)
[src]
Dispatches the message of value T
to all of the subscribers
Safety
This method casts untyped *const () message received internally into the expected type. It is safe to be called because of the mapping that ensures the correct messages are distributed to subscribers expecting them.
Auto Trait Implementations
impl !RefUnwindSafe for Dispatcher
impl !Send for Dispatcher
impl !Sync for Dispatcher
impl Unpin for Dispatcher
impl !UnwindSafe for Dispatcher
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,