pub struct ParallelEventDispatcher<T>where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,{ /* private fields */ }
Expand description
Implementations§
source§impl<T> ParallelEventDispatcher<T>where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
impl<T> ParallelEventDispatcher<T>where
T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static,
sourcepub fn add_listener<D: ParallelListener<T> + Send + Sync + 'static>(
&mut self,
event_identifier: T,
listener: &Arc<Mutex<D>>
)
pub fn add_listener<D: ParallelListener<T> + Send + Sync + 'static>(
&mut self,
event_identifier: T,
listener: &Arc<Mutex<D>>
)
Adds a ParallelListener
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 ParallelListener
to the dispatcher:
extern crate hey_listen;
use std::sync::Arc;
use hey_listen::{Mutex, ParallelListener, ParallelEventDispatcher, ParallelDispatcherRequest};
#[derive(Clone, Eq, Hash, PartialEq)]
enum Event {
EventType,
}
struct ListenerStruct {}
impl ParallelListener<Event> for ListenerStruct {
fn on_event(&mut self, event: &Event) -> Option<ParallelDispatcherRequest> { None }
}
fn main() {
let listener = Arc::new(Mutex::new(ListenerStruct {}));
let mut dispatcher: ParallelEventDispatcher<Event> = ParallelEventDispatcher::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 {}
sourcepub fn add_fn(
&mut self,
event_identifier: T,
function: Box<dyn Fn(&T) -> Option<ParallelDispatcherRequest> + Send + Sync>
)
pub fn add_fn(
&mut self,
event_identifier: T,
function: Box<dyn Fn(&T) -> Option<ParallelDispatcherRequest> + Send + Sync>
)
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 these.
Examples
Adding a Fn
to the dispatcher:
extern crate hey_listen;
extern crate failure;
#[macro_use]
extern crate failure_derive;
use hey_listen::{Mutex, ParallelEventDispatcher, ParallelDispatcherRequest};
use std::sync::Arc;
#[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: ParallelEventDispatcher<Event> = ParallelEventDispatcher::default();
let weak_listener_ref = Arc::downgrade(&Arc::clone(&listener));
let closure = Box::new(move |event: &Event| -> Option<ParallelDispatcherRequest> {
if let Some(listener) = weak_listener_ref.upgrade() {
listener.lock().test_method(&event);
None
} else {
Some(ParallelDispatcherRequest::StopListening)
}
});
dispatcher.add_fn(Event::EventType, closure);
}
sourcepub fn num_threads(&mut self, num: usize) -> Result<(), BuildError>
pub fn num_threads(&mut self, num: usize) -> Result<(), BuildError>
Immediately after calling this method,
the dispatcher will attempt to build a thread-pool with
num
amount of threads.
If internals fail to build, BuildError
is returned.
Note: Failing to build the thread-pool will result in keeping the prior thread-pool, if one has been built before. If none has been built, none will be used; being default.
sourcepub fn dispatch_event(&mut self, event_identifier: &T)
pub fn dispatch_event(&mut self, event_identifier: &T)
All ParallelListener
s listening to a passed event_identifier
will be called via their implemented on_event
-method.
Fn
s returning an Option
wrapping ParallelDispatcherRequest
with ParallelDispatcherRequest::StopListening
will cause them
to be removed from the event-dispatcher.