Skip to main content

zinq_reflect/
type_of.rs

1#[macro_export]
2macro_rules! type_of {
3    [&$value:expr] => {
4        crate::TypeOf::to_type(&$value.clone())
5    };
6    [$($type:ty)*] => {
7        $(<$type>::type_of())*
8    };
9    [$value:expr] => {
10        crate::TypeOf::to_type(&$value)
11    };
12}
13
14pub trait TypeOf {
15    fn type_of() -> crate::Type;
16    fn to_type(&self) -> crate::Type {
17        return Self::type_of();
18    }
19}
20
21#[cfg(test)]
22mod test {
23    use crate::TypeOf;
24
25    #[test]
26    pub fn from_expr() {
27        assert!(type_of!(1_i8).is_i8());
28    }
29
30    #[test]
31    pub fn from_expr_ref() {
32        assert!(type_of!(&1_i8).is_i8());
33    }
34
35    #[test]
36    pub fn from_type() {
37        assert!(type_of!(i8).is_i8());
38    }
39}