use std::{
any::{Any, TypeId},
collections::{HashMap, hash_map},
fmt::Debug,
hash::{BuildHasherDefault, Hasher},
task::{Context, Poll, ready},
};
use crate::Interface;
pub mod alive;
pub mod local_endpoint;
pub mod route;
pub trait Component: Any + Debug + Send + Sync {
fn poll_shutdown(&self, cx: &mut Context<'_>) -> Poll<()>;
fn reinit(&self, iface: &Interface);
}
#[derive(Default)]
pub(super) struct IdHasher(u64);
impl Hasher for IdHasher {
fn write(&mut self, _: &[u8]) {
unreachable!("TypeId calls write_u64");
}
#[inline]
fn write_u64(&mut self, id: u64) {
self.0 = id;
}
#[inline]
fn finish(&self) -> u64 {
self.0
}
}
#[derive(Default)]
pub struct Components {
pub(super) map: HashMap<TypeId, Box<dyn Component>, BuildHasherDefault<IdHasher>>,
}
impl Components {
pub fn new() -> Self {
Self::default()
}
pub fn get<C: Component>(&self) -> Option<&C> {
self.map
.get(&TypeId::of::<C>())
.and_then(|c| (c.as_ref() as &dyn Any).downcast_ref())
}
pub fn exist<C: Component>(&self) -> bool {
self.map.contains_key(&TypeId::of::<C>())
}
pub fn with<C: Component, T>(&self, f: impl FnOnce(&C) -> T) -> Option<T> {
self.get::<C>().map(f)
}
pub fn init_with<C: Component>(&mut self, init: impl FnOnce() -> C) -> &mut C {
let ref_mut = self
.map
.entry(TypeId::of::<C>())
.or_insert_with(|| Box::new(init()));
(ref_mut.as_mut() as &mut dyn Any).downcast_mut().unwrap()
}
pub fn try_init_with<C: Component, E>(
&mut self,
init: impl FnOnce() -> Result<C, E>,
) -> Result<&mut C, E> {
let entry = self.map.entry(TypeId::of::<C>());
let ref_mut = match entry {
hash_map::Entry::Occupied(entry) => entry.into_mut(),
hash_map::Entry::Vacant(entry) => entry.insert(Box::new(init()?)),
};
Ok((ref_mut.as_mut() as &mut dyn Any).downcast_mut().unwrap())
}
pub fn poll_remove<C>(&mut self, cx: &mut Context<'_>) -> Poll<()>
where
C: Component,
{
let hash_map::Entry::Occupied(entry) = self.map.entry(TypeId::of::<C>()) else {
return Poll::Ready(());
};
ready!(entry.get().poll_shutdown(cx));
entry.remove();
Poll::Ready(())
}
}