use std::{
any::{type_name, Any, TypeId},
fmt::Display,
};
use crate::Tag;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Key {
pub(crate) id: Id,
pub(crate) type_name: String,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Id {
TypeId(TypeId),
Tag(&'static str),
}
impl Key {
pub(crate) fn from_type_id<T: Any + ?Sized>() -> Self {
Self {
id: Id::TypeId(TypeId::of::<T>()),
type_name: type_name::<T>().to_string(),
}
}
pub(crate) fn from_tag<T: Any + ?Sized>(tag: &Tag<T>) -> Self {
Self {
id: Id::Tag(tag.tag),
type_name: type_name::<T>().to_string(),
}
}
}
impl Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.id {
Id::TypeId(_) => write!(f, "{}", self.type_name),
Id::Tag(tag) => write!(f, "Tag({tag})"),
}
}
}