Struct hey_listen::EventDispatcher
[−]
[src]
pub struct EventDispatcher<T> where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static, { /* fields omitted */ }
Methods
impl<T> EventDispatcher<T> where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
[src]
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
pub fn add_listener<D: Listener<T> + Send + Sync + 'static>(
&mut self,
event_identifier: T,
listener: &Arc<Mutex<D>>
)
[src]
&mut self,
event_identifier: T,
listener: &Arc<Mutex<D>>
)
Adds a Listener
to listen for an event_identifier
.
If event_identifier
is a new HashMap
-key, it will be added.
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:
extern crate hey_listen; extern crate parking_lot; use std::sync::Arc; use hey_listen::{Listener, EventDispatcher, SyncDispatcherRequest}; #[derive(Clone, Eq, Hash, PartialEq)] enum Event { EventType, } struct ListenerStruct {} impl Listener<Event> for ListenerStruct { fn on_event(&mut self, event: &Event) -> Option<SyncDispatcherRequest> { None } } fn main() { let listener = Arc::new(parking_lot::Mutex::new(ListenerStruct {})); let mut dispatcher: EventDispatcher<Event> = EventDispatcher::default(); 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 add_fn(
&mut self,
event_identifier: T,
function: Box<Fn(&T) -> Option<SyncDispatcherRequest> + Send + Sync + 'static>
)
[src]
&mut self,
event_identifier: T,
function: Box<Fn(&T) -> Option<SyncDispatcherRequest> + Send + Sync + 'static>
)
Adds a Fn
to listen for an event_identifier
.
If event_identifier
is a new HashMap
-key, it will be added.
Note: If your Enum
owns fields you need to consider implementing
the Hash
- and PartialEq
-trait if you want to ignore fields.
Examples
Adding a Fn
to the dispatcher:
extern crate hey_listen; extern crate parking_lot; use hey_listen::EventDispatcher; use hey_listen::SyncDispatcherRequest; use std::sync::Arc; use parking_lot::Mutex; #[derive(Clone, Eq, Hash, PartialEq)] enum Event { EventType, } struct EventListener { used_method: bool, } impl EventListener { fn test_method(&mut self, _event: &Event) { self.used_method = true; } } fn main() { let listener = Arc::new(Mutex::new(EventListener { used_method: false })); let mut dispatcher: EventDispatcher<Event> = EventDispatcher::default(); let weak_listener_ref = Arc::downgrade(&Arc::clone(&listener)); let closure = Box::new(move |event: &Event| -> Option<SyncDispatcherRequest> { if let Some(listener) = weak_listener_ref.upgrade() { listener.lock().test_method(&event); None } else { Some(SyncDispatcherRequest::StopListening) } }); dispatcher.add_fn(Event::EventType, closure); }
pub fn dispatch_event(&mut self, event_identifier: &T)
[src]
Trait Implementations
impl<T> Default for EventDispatcher<T> where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
[src]
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
fn default() -> EventDispatcher<T>
[src]
Returns the "default value" for a type. Read more