Struct riddle_common::eventpub::EventPub [−][src]
Event publisher which can have multiple subscribers.
Example
#[derive(Clone)] enum Message { Test(u32) } fn main() { let publisher: EventPub<Message> = EventPub::new(); // Attach any subscribers // ... publisher.dispatch(Message::Test(42)); publisher.dispatch(Message::Test(13)); }
Implementations
impl<T: Clone> EventPub<T>
[src]
pub fn new() -> Self
[src]
Create a new event publisher.
The new publisher will have no subscribers, so any message dispatched to it in this state will be silently dropped.
pub fn attach(&self, sub: &EventSub<T>)
[src]
Attach a subscriber to the publisher.
Any events dispatched after this call will be registered with the subscriber. The subscriber won’t receive any events dispatched before it has been attached.
The subscriber is detached by dropping the attached EventSub
.
Example
let publisher: EventPub<Message> = EventPub::new(); let subscriber: EventSub<Message> = EventSub::new(); publisher.attach(&subscriber); assert_eq!(1, publisher.subscription_count()); drop(subscriber); assert_eq!(0, publisher.subscription_count());
pub fn dispatch(&self, event: T)
[src]
Send an event to all currently registered subscribers.
Example
let sub_a: EventSub<Message> = EventSub::new(); let sub_b: EventSub<Message> = EventSub::new(); let publisher: EventPub<Message> = EventPub::new(); publisher.attach(&sub_a); publisher.attach(&sub_b); publisher.dispatch(Message::Test(0)); assert_eq!(vec![Message::Test(0)], sub_a.collect()); assert_eq!(vec![Message::Test(0)], sub_b.collect());
pub fn subscription_count(&self) -> u32
[src]
The current count of attached subscribers.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for EventPub<T>
impl<T> Send for EventPub<T> where
T: Send,
T: Send,
impl<T> Sync for EventPub<T> where
T: Send,
T: Send,
impl<T> Unpin for EventPub<T>
impl<T> UnwindSafe for EventPub<T>
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,
pub 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.
pub 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>,