use crate::{ServiceProvider, Ref};
use std::any::type_name;
use std::collections::hash_map::DefaultHasher;
use std::fmt::{Display, Formatter, Result as FormatResult};
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, Eq)]
pub struct Type {
id: u64,
name: String,
key: Option<String>,
}
impl Type {
pub fn of<T: ?Sized>() -> Self {
Type::new(type_name::<T>().to_string(), None)
}
pub fn keyed<TKey, TType: ?Sized>() -> Self {
Type::new(
type_name::<TType>().to_string(),
Some(type_name::<TKey>().to_string()),
)
}
pub fn factory_of<TSvc: ?Sized>() -> Self {
Type::new(
type_name::<dyn Fn(&ServiceProvider) -> Ref<TSvc>>().to_string(),
None,
)
}
pub fn unknown() -> Self {
Self::of::<()>()
}
pub fn with_key(&self, key: &Self) -> Self {
Type::new(self.name.clone(), Some(key.name.clone()))
}
fn new(name: String, key: Option<String>) -> Self {
let mut hasher = DefaultHasher::new();
name.hash(&mut hasher);
if let Some(ref val) = key {
val.hash(&mut hasher);
}
Self {
id: hasher.finish(),
name,
key,
}
}
pub fn id(&self) -> u64 {
self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn deconstruct(t: &Type) -> (&str, Option<&str>) {
(&t.name, t.key.as_deref())
}
}
impl PartialEq<Type> for Type {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl PartialEq<Type> for &Type {
fn eq(&self, other: &Type) -> bool {
self.id == other.id
}
}
impl Hash for Type {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl Display for Type {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult {
formatter.write_str(&self.name)
}
}