use std::{
fmt::Display,
hash::Hash,
marker::PhantomData,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
pub trait UserEvent: Default {
type ID: ?Sized + Hash + Eq + Send + Sync + Clone + 'static + Display;
type Argument: Sized + Send + 'static;
}
pub trait AutoIncEvent: UserEvent {
fn next(&mut self) -> Self::ID;
}
#[derive(Clone)]
pub struct RPCResponser<Argument>(Arc<AtomicUsize>, PhantomData<Argument>);
impl<Argument> Default for RPCResponser<Argument> {
fn default() -> Self {
Self(Arc::new(AtomicUsize::new(1)), PhantomData)
}
}
impl<Argument> UserEvent for RPCResponser<Argument>
where
Argument: Sized + Send + 'static,
{
type ID = usize;
type Argument = Argument;
}
impl<Argument> AutoIncEvent for RPCResponser<Argument>
where
Argument: Sized + Send + 'static,
{
fn next(&mut self) -> Self::ID {
self.0.fetch_add(1, Ordering::SeqCst)
}
}