scsys_traits/
dtype.rs

1/*
2    Appellation: dtype <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use core::any::{Any, TypeId};
6
7/// Compare two types
8fn type_of<U, V>() -> bool
9where
10    U: core::any::Any + ?Sized,
11    V: core::any::Any + ?Sized,
12{
13    core::any::TypeId::of::<U>() == core::any::TypeId::of::<V>()
14}
15
16pub trait TypeOf {
17    fn of<T: 'static>() -> bool;
18}
19
20pub trait IsType {
21    fn is<T>(&self) -> bool
22    where
23        T: 'static,
24        Self: 'static,
25    {
26        type_of::<Self, T>()
27    }
28}
29
30/// [`DType`] is a trait designed to provide additional information regarding the type of a
31/// particular value.
32pub trait DType: Any {
33    private!();
34
35    fn type_id(&self) -> TypeId {
36        Any::type_id(self)
37    }
38
39    fn type_name(&self) -> &'static str {
40        core::any::type_name::<Self>()
41    }
42}
43
44/*
45 ************* Implementations *************
46*/
47impl<T> TypeOf for T
48where
49    T: Any,
50{
51    fn of<U: 'static>() -> bool {
52        type_of::<T, U>()
53    }
54}
55
56impl<T: 'static> IsType for T {}
57
58impl dyn DType {}