Struct hey_listen::sync::async_dispatcher::AsyncDispatcher[][src]

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

In charge of parallel dispatching to all listeners.

Implementations

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

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

Create a new async dispatcher. Amount of threads must be set via Tokio.

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

Adds a AsyncListener 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 AsyncListener to the dispatcher:

use std::sync::Arc;
use hey_listen::{
   RwLock,
   sync::{AsyncListener, AsyncDispatcher, AsyncDispatchResult},
};
use async_trait::async_trait;

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

struct ListenerStruct {}

#[async_trait]
impl AsyncListener<Event> for Arc<RwLock<ListenerStruct>> {
    async fn on_event(&self, event: &Event) -> Option<AsyncDispatchResult> { None }
}


let listener = Arc::new(RwLock::new(ListenerStruct {}));
let mut dispatcher: AsyncDispatcher<Event> = AsyncDispatcher::new();

dispatcher.add_listener(Event::EventType, Arc::clone(&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 async fn dispatch_event<'a>(&mut self, event_identifier: &T)[src]

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

Trait Implementations

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

Auto Trait Implementations

impl<T> !RefUnwindSafe for AsyncDispatcher<T>

impl<T> Send for AsyncDispatcher<T>

impl<T> Sync for AsyncDispatcher<T>

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

impl<T> !UnwindSafe for AsyncDispatcher<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.