[−][src]Struct hey_listen::rc::priority_dispatcher::PriorityDispatcher
In charge of prioritised sync dispatching to all listeners.
Owns a map event-variants and Weak
-references to their
listeners and/or owns Fn
s.
Opposed to EventListener
, this structure utilises one BTreeMap
per
event-type to order listeners by a given priority-level.
Note: Consider implementing your own Ord
-trait, if you
want a different kind of order.
Methods
impl<P, T> PriorityDispatcher<P, T> where
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
[src]
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
pub fn add_listener<D: Listener<T> + 'static>(
&mut self,
event_identifier: T,
listener: &Rc<RwLock<D>>,
priority: P
)
[src]
&mut self,
event_identifier: T,
listener: &Rc<RwLock<D>>,
priority: P
)
Adds a Listener
to listen for an event_identifier
, considering
a given priority
implementing the Ord
-trait, to sort dispatch-order.
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:
use std::rc::Rc; use hey_listen::{ RwLock, rc::{Listener, PriorityDispatcher, 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 = Rc::new(RwLock::new(ListenerStruct {})); let mut dispatcher: PriorityDispatcher<u32, Event> = PriorityDispatcher::default(); dispatcher.add_listener(Event::EventType, &listener, 1); }
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<dyn Fn(&T) -> Option<SyncDispatcherRequest>>,
priority: P
)
[src]
&mut self,
event_identifier: T,
function: Box<dyn Fn(&T) -> Option<SyncDispatcherRequest>>,
priority: P
)
Adds an Fn
to listen for an event_identifier
, considering
a given priority
implementing the Ord
-trait in order to sort dispatch-order.
If event_identifier
is a new HashMap
-key, it will be added.
Examples
Adding an Fn
to the dispatcher:
use hey_listen::{ RwLock, rc::{Listener, PriorityDispatcher, SyncDispatcherRequest}, }; use std::rc::Rc; #[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 = Rc::new(RwLock::new(EventListener { used_method: false })); let mut dispatcher: PriorityDispatcher<u32, Event> = PriorityDispatcher::default(); let weak_listener_ref = Rc::downgrade(&Rc::clone(&listener)); let closure = Box::new(move |event: &Event| -> Option<SyncDispatcherRequest> { if let Some(listener) = weak_listener_ref.upgrade() { listener.write().test_method(&event); None } else { Some(SyncDispatcherRequest::StopListening) } }); dispatcher.add_fn(Event::EventType, closure, 1); }
pub fn dispatch_event(&mut self, event_identifier: &T)
[src]
All Listener
s listening to a passed event_identifier
will be called via their implemented on_event
-method.
Fn
s returning Result
with Ok(())
will be retained
and Err(SyncDispatcherRequest::StopListening)
will cause them to
be removed from the event-dispatcher.
Notice: Listener
s will called ordered by their priority-level.
Trait Implementations
impl<P, T> Default for PriorityDispatcher<P, T> where
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
[src]
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
fn default() -> PriorityDispatcher<P, T>
[src]
Auto Trait Implementations
impl<P, T> !Send for PriorityDispatcher<P, T>
impl<P, T> !Sync for PriorityDispatcher<P, T>
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,