use std::future::Future; /// Handler trait for asynchronous event callbacks. /// /// This trait is automatically implemented for any closure matching /// `Fn(E) -> Future + Send + Sync + 'static`. /// /// By using `Fn(E) -> >::Fut` as a supertrait, /// Rust propagates the event type `E` directly into the closure's parameter, /// enabling seamless turbofish syntax with clean field access: /// /// ```rust,no_run /// # use event_bus::EventBus; /// # #[derive(Clone)] struct MessageUpdatedEvent { id: u64 } /// # let bus = EventBus::new(); /// bus.on_async::(|event| async move { /// println!("Updated: {:?}", event.id); /// }); /// ``` pub trait AsyncHandler: Fn(E) -> >::Fut + Send + Sync + 'static { type Fut: Future + Send + 'static; } impl AsyncHandler for F where F: Fn(E) -> Fut + Send + Sync + 'static, Fut: Future + Send + 'static, { type Fut = Fut; } /// Handler trait for asynchronous event callbacks with an injected context. /// /// Automatically implemented for closures matching /// `Fn(C, E) -> Future + Send + Sync + 'static`. pub trait AsyncHandlerWith: Fn(C, E) -> >::Fut + Send + Sync + 'static { type Fut: Future + Send + 'static; } impl AsyncHandlerWith for F where C: Clone + Send + Sync + 'static, F: Fn(C, E) -> Fut + Send + Sync + 'static, Fut: Future + Send + 'static, { type Fut = Fut; }