use std::any::TypeId;
use std::marker::PhantomData;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeKey {
type_id: TypeId,
qualifier: Option<&'static str>,
}
impl TypeKey {
pub fn of<T: ?Sized + 'static>() -> Self {
Self {
type_id: TypeId::of::<T>(),
qualifier: None,
}
}
pub fn keyed<T: ?Sized + 'static>(qualifier: &'static str) -> Self {
Self {
type_id: TypeId::of::<T>(),
qualifier: Some(qualifier),
}
}
pub fn describe(&self) -> String {
match self.qualifier {
Some(q) => format!("{}#{q}", self.type_id_debug()),
None => self.type_id_debug(),
}
}
fn type_id_debug(&self) -> String {
format!("{:?}", self.type_id)
}
}
pub type ServiceKey = TypeKey;
pub struct Key<T: ?Sized + 'static> {
name: &'static str,
_marker: PhantomData<fn() -> T>,
}
impl<T: ?Sized + 'static> Key<T> {
pub const fn new(name: &'static str) -> Self {
Self {
name,
_marker: PhantomData,
}
}
}
impl<T: ?Sized + 'static> From<Key<T>> for TypeKey {
fn from(k: Key<T>) -> Self {
TypeKey::keyed::<T>(k.name)
}
}
pub(crate) type ScopeId = std::sync::Arc<str>;