24 lines
696 B
Rust
24 lines
696 B
Rust
#![cfg(any(feature = "mocks", test))]
|
|
|
|
use std::future::ready;
|
|
use std::pin::Pin;
|
|
use std::rc::Rc;
|
|
|
|
pub struct AsyncMonitor<E: 'static>(Rc<dyn Fn(E) -> Pin<Box<dyn Future<Output = ()>>>>);
|
|
impl<E: 'static> AsyncMonitor<E> {
|
|
pub fn new<F: AsyncFn(E) -> () + 'static>(f: F) -> Self {
|
|
let f_rc = Rc::new(f);
|
|
AsyncMonitor(Rc::new(move |e| {
|
|
let f_rc = f_rc.clone();
|
|
Box::pin(async move { f_rc(e).await })
|
|
}))
|
|
}
|
|
pub async fn notify(&self, e: E) -> () { (self.0)(e).await }
|
|
}
|
|
impl<E: 'static> Default for AsyncMonitor<E> {
|
|
fn default() -> Self { Self(Rc::new(|_| Box::pin(ready(())))) }
|
|
}
|
|
impl<E: 'static> Clone for AsyncMonitor<E> {
|
|
fn clone(&self) -> Self { Self(self.0.clone()) }
|
|
}
|