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/// The [`TypeOf`] trait automatically provides a way to check if a type is of a specific type
16/// at compile time. This is useful for generic programming and type checking.
17pub trait TypeOf {
18    private!();
19
20    fn of<T: 'static>() -> bool;
21}
22/// The [`IsType`] trait provides a method to check if the current type is of a specific type
23/// at runtime. This is useful for dynamic type checking in scenarios where the type may not be
24/// known at compile time.
25pub trait IsType {
26    private!();
27
28    fn is<T>(&self) -> bool
29    where
30        T: 'static,
31        Self: 'static,
32    {
33        type_of::<Self, T>()
34    }
35}
36
37/// [`DType`] is a trait designed to provide additional information regarding the type of a
38/// particular value.
39pub trait DType: 'static + IsType + TypeOf {
40    private!();
41
42    fn type_id(&self) -> TypeId {
43        Any::type_id(self)
44    }
45
46    fn type_name(&self) -> &'static str {
47        core::any::type_name::<Self>()
48    }
49}
50
51/*
52 ************* Implementations *************
53*/
54impl<T> DType for T
55where
56    T: 'static,
57{
58    seal!();
59
60    fn type_id(&self) -> TypeId {
61        Any::type_id(self)
62    }
63
64    fn type_name(&self) -> &'static str {
65        core::any::type_name::<Self>()
66    }
67}
68
69impl<T> TypeOf for T
70where
71    T: 'static,
72{
73    seal!();
74
75    fn of<U: 'static>() -> bool {
76        type_of::<T, U>()
77    }
78}
79
80impl<T> IsType for T
81where
82    T: 'static,
83{
84    seal!();
85}