pub mod async_fn_ptr;
use std::{
any::{type_name, TypeId},
marker::PhantomData,
};
pub use unique_type;
use self::async_fn_ptr::{AsyncFnPtr, HandlerCallableErased, HandlerFn};
#[allow(clippy::module_name_repetitions)]
pub struct EventDef<
Tag: unique_type::Unique,
At,
Rt = (),
> {
pub(crate) name: &'static str,
_tag: PhantomData<*const Tag >,
_at: PhantomData<*const At >,
_rt: PhantomData<*const Rt >,
}
unsafe impl<Tag: unique_type::Unique, At, Rt> Sync for EventDef<Tag, At, Rt> {}
unsafe impl<Tag: unique_type::Unique, At, Rt> Send for EventDef<Tag, At, Rt> {}
impl<Tag: unique_type::Unique, At, Rt> EventDef<Tag, At, Rt> {
#[must_use]
pub const unsafe fn new(name: &'static str) -> Self {
Self {
name,
_tag: PhantomData,
_at: PhantomData,
_rt: PhantomData,
}
}
}
#[allow(clippy::module_name_repetitions)]
pub struct EventRegister<S: ?Sized> {
pub(crate) handlers: Vec<(
TypeId,
Box<dyn HandlerCallableErased + Send + Sync + 'static>,
String,
)>,
_stop_t: PhantomData<S>,
}
impl<S: Sync + Send + 'static> EventRegister<S> {
pub(crate) const fn new() -> Self {
Self {
handlers: vec![],
_stop_t: PhantomData,
}
}
#[must_use]
pub fn handler<Tag, At, Rt, P>(mut self, def: &'static EventDef<Tag, At, Rt>, func: P) -> Self
where
Tag: unique_type::Unique + Send + Sync + 'static,
At: Send + Sync + 'static,
Rt: Send + Sync + 'static,
P: for<'a> AsyncFnPtr<'a, S, At, Rt> + Copy + Send + Sync + 'static,
{
self.handlers.push((
TypeId::of::<Tag>(),
Box::new(HandlerFn::new(func)),
format!(
"handler: {}, name: {}, args: {}, return: {}, type_id: {:?}",
type_name::<S>(),
def.name,
type_name::<At>(),
type_name::<Rt>(),
TypeId::of::<Tag>(),
),
));
let _ = def;
self
}
}