Struct hey_listen::PriorityEventDispatcher [] [src]

pub struct PriorityEventDispatcher<P, T> where
    P: Ord,
    T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static, 
{ /* fields omitted */ }

Owns a map of all listened event-variants, Weak-references to their listeners and Fns. 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> PriorityEventDispatcher<P, T> where
    P: Ord + Clone,
    T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static, 
[src]

[src]

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:

extern crate hey_listen;
extern crate parking_lot;
use std::sync::Arc;

use hey_listen::{Listener, PriorityEventDispatcher, 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: PriorityEventDispatcher<u32, Event> = PriorityEventDispatcher::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 {}

[src]

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:

extern crate hey_listen;
extern crate parking_lot;

use hey_listen::{PriorityEventDispatcher, 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: PriorityEventDispatcher<u32, Event> = PriorityEventDispatcher::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, 1);
}

[src]

All Listeners listening to a passed event_identifier will be called via their implemented on_event-method. Fns returning Result with Ok(()) will be retained and Err(SyncDispatcherRequest::StopListening) will cause them to be removed from the event-dispatcher.

Notice: Listeners will called ordered by their priority-level.

Trait Implementations

impl<P, T> Default for PriorityEventDispatcher<P, T> where
    P: Ord + Clone,
    T: PartialEq + Eq + Hash + Clone + Send + Sync + 'static, 
[src]

[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations

impl<P, T> Send for PriorityEventDispatcher<P, T> where
    P: Send

impl<P, T> Sync for PriorityEventDispatcher<P, T> where
    P: Sync