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]
T: PartialEq + Eq + Hash + Clone + Send + Sized + Sync + 'static,
#[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]
&mut self,
event_key: T,
listener: D
)
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 AsyncListener
s listening to a passed event_identifier
will be called via their implemented on_event
-method.
AsyncListener
s 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]
T: PartialEq + Eq + Hash + Clone + Send + Sized + Sync + 'static,
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,
T: Unpin,
impl<T> !UnwindSafe for AsyncDispatcher<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
pub const ALIGN: usize
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
pub unsafe fn drop(ptr: usize)
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,