Struct AsyncEvent

Source
pub struct AsyncEvent<'a, TEventArgs> { /* private fields */ }
Expand description

An asynchronous event that can have multiple handlers attached to it.

This is similar to C#’s event keyword but designed for async/await patterns. Handlers are stored in a slab storage for efficient access by index.

§Examples

use async_event_rs::AsyncEvent;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct EventArgs<'a> {
    id: u32,
    message: &'a str,
}

let mut event = AsyncEvent::<EventArgs>::new();
event.add(|args| async move {
    println!("Event invoked with args: {:?}", args);
    assert_eq!(args, EventArgs {id: 0, message: ""});
});

let arg = EventArgs {id: 0, message: ""};
event.invoke_async(arg).await;

Implementations§

Source§

impl<'a, TEventArgs> AsyncEvent<'a, TEventArgs>

Source

pub fn new() -> Self

Creates a new, empty AsyncEvent

§Examples
use async_event_rs::AsyncEvent;

let mut event: AsyncEvent<()> = AsyncEvent::new();
Source

pub fn add<F, Fut>(&mut self, handler: F) -> usize
where F: Fn(TEventArgs) -> Fut + 'a, Fut: Future<Output = ()> + 'a,

Adds an event handler to the event.

The handler should be a closure that accepts a reference to the event arguments and returns a future. The future will be executed when the event is invoked.

Returns a handle that can be used to remove the handler later.

§Examples
use async_event_rs::AsyncEvent;

let mut event = AsyncEvent::<()>::new();
let handle = event.add(|args| async move {
    println!("Event invoked");
});
Source

pub fn remove(&mut self, handle: usize) -> bool

Removes an event handler using its handle.

Returns true if the handler was found and removed, false otherwise.

§Examples
use async_event_rs::AsyncEvent;

let mut event = AsyncEvent::<()>::new();
let handle = event.add(|args| async move {
    println!("Event invoked");
});

assert!(event.remove(handle));
assert!(!event.remove(handle)); // Already removed
Source

pub fn clear(&mut self)

Removes all event handlers.

§Examples
use async_event_rs::AsyncEvent;

let mut event = AsyncEvent::<()>::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });

event.clear(); // Remove all handlers
Source

pub async fn invoke_async(&self, arg: TEventArgs)
where TEventArgs: Clone,

Invokes all event handlers sequentially (one after another).

Each handler is awaited before the next one is executed.

§Examples
use async_event_rs::AsyncEvent;

let mut event = AsyncEvent::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });

event.invoke_async(()).await; // Execute all handlers in order
Source

pub fn invoke_parallel_async( &self, arg: TEventArgs, ) -> JoinAll<impl Future<Output = ()> + 'a>
where TEventArgs: Clone,

Invokes all event handlers in parallel.

All handlers are spawned concurrently and executed simultaneously.

§Examples
use async_event_rs::AsyncEvent;

let mut event = AsyncEvent::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });

event.invoke_parallel_async(()).await; // Execute all handlers in parallel

Trait Implementations§

Source§

impl<'a, TEventArgs> Default for AsyncEvent<'a, TEventArgs>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, TEventArgs> Freeze for AsyncEvent<'a, TEventArgs>

§

impl<'a, TEventArgs> !RefUnwindSafe for AsyncEvent<'a, TEventArgs>

§

impl<'a, TEventArgs> !Send for AsyncEvent<'a, TEventArgs>

§

impl<'a, TEventArgs> !Sync for AsyncEvent<'a, TEventArgs>

§

impl<'a, TEventArgs> Unpin for AsyncEvent<'a, TEventArgs>

§

impl<'a, TEventArgs> !UnwindSafe for AsyncEvent<'a, TEventArgs>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.