Struct riddle_common::eventpub::EventPub[][src]

pub struct EventPub<T> { /* fields omitted */ }

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

impl<T: Clone> Default for EventPub<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for EventPub<T>

impl<T> Send for EventPub<T> where
    T: Send

impl<T> Sync for EventPub<T> where
    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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.