Struct hey_listen::rc::dispatcher::Dispatcher[][src]

pub struct Dispatcher<T> where
    T: PartialEq + Eq + Hash + Clone + 'static, 
{ /* fields omitted */ }

In charge of parallel dispatching to all listeners.

Implementations

impl<T> Dispatcher<T> where
    T: PartialEq + Eq + Hash + Clone + Sized + 'static, 
[src]

#[must_use]
pub fn new() -> Self
[src]

Create a new blocking dispatcher.

pub fn add_listener<D: Listener<T> + Sized + 'static>(
    &mut self,
    event_key: T,
    listener: D
)
[src]

Adds a Listener to listen for an event_key.

Note: If your Enum owns fields you need to consider implementing the Hash- and PartialEq-trait if you want to ignore fields, see second example for an implementation-suggestion.

Examples

Adding a Listener to the dispatcher:

use hey_listen::rc::{
    Listener, Dispatcher, DispatcherRequest,
};

#[derive(Clone, Eq, Hash, PartialEq)]
enum Event {
    EventType,
}

struct ListenerStruct;

impl Listener<Event> for ListenerStruct {
    fn on_event(&self, event: &Event) -> Option<DispatcherRequest> { None }
}

let listener = ListenerStruct;
let mut dispatcher: Dispatcher<Event> = Dispatcher::new();

dispatcher.add_listener(Event::EventType, listener);

Declaring your own Hash- and PartialEq-trait to bypass hashing on fields:

use std::hash::{Hash, Hasher};
use std::mem::discriminant;

#[derive(Clone)]
enum Event {
    TestVariant(i32),
}

impl Hash for Event {
    fn hash<H: Hasher>(&self, _state: &mut H) {}
}

impl PartialEq for Event {
    fn eq(&self, other: &Event) -> bool {
        discriminant(self) == discriminant(other)
    }
}

impl Eq for Event {}

pub fn dispatch_event(&mut self, event_identifier: &T)[src]

All Listeners listening to a passed event_identifier will be called via their implemented on_event-method. Listeners returning an Option wrapping DispatcherRequest with DispatcherRequest::StopListening will cause them to be removed from the event-dispatcher.

Auto Trait Implementations

impl<T> !RefUnwindSafe for Dispatcher<T>

impl<T> !Send for Dispatcher<T>

impl<T> !Sync for Dispatcher<T>

impl<T> Unpin for Dispatcher<T> where
    T: Unpin

impl<T> !UnwindSafe for Dispatcher<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> Pointable for T

type Init = T

The type for initializers.

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.