1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use info::{ScalarType, TypeInfo};


/// This module provides type info structures and enums
pub mod info;

/// This module implements Introspectable for several core standard library collections
#[cfg(features="specialized_std")]
pub mod std_impl;


/// The main trait of this library which allows introspection on both structs and enums
pub trait Introspectable {
    /// Returns type info for the implemented type.
    fn introspect() -> info::TypeInfo;

    /// Returns type info for an instance of a type
    fn introspect_instance(&self) -> info::TypeInfo {
        Self::introspect()
    }
}

// Implementation of Introspectable for tuple sizes up to 16
// A macro is used for this

#[macro_export]
macro_rules! impl_introspectable {
    ($($i:ident),+) => {
        impl<$($i: Introspectable),+> Introspectable for ($($i,)+) {
            fn introspect() -> info::TypeInfo {
                info::TypeInfo::Compound(
                    info::CompoundType::Tuple{
                        fields: vec![$($i::introspect(),)+]
                    }
                )
            }
        }
    };
}

impl_introspectable!(T1);
impl_introspectable!(T1, T2);
impl_introspectable!(T1, T2, T3);
impl_introspectable!(T1, T2, T3, T4);
impl_introspectable!(T1, T2, T3, T4, T5);
impl_introspectable!(T1, T2, T3, T4, T5, T6);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);

// Implementation of Introspectable for pointer types


impl<T: Introspectable> Introspectable for *const T {
    fn introspect() -> info::TypeInfo {
        info::TypeInfo::Pointer(
            info::PointerType::ConstPointer(Box::new(T::introspect()))
        )
    }
}

impl<T: Introspectable> Introspectable for *mut T {
    fn introspect() -> info::TypeInfo {
        info::TypeInfo::Pointer(
            info::PointerType::MutPointer(Box::new(T::introspect()))
        )
    }
}



// Implementation of Introspectable for all scalar types

impl Introspectable for bool {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::Bool)
    }
}

impl Introspectable for i8 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::I8)
    }
}

impl Introspectable for i16 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::I16)
    }
}

impl Introspectable for i32 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::I32)
    }
}

impl Introspectable for i64 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::I64)
    }
}

impl Introspectable for i128 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::I128)
    }
}

impl Introspectable for u8 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::U8)
    }
}

impl Introspectable for u16 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::U16)
    }
}

impl Introspectable for u32 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::U32)
    }
}

impl Introspectable for u64 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::U64)
    }
}

impl Introspectable for u128 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::U128)
    }
}

impl Introspectable for f32 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::F32)
    }
}

impl Introspectable for f64 {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::F64)
    }
}

impl Introspectable for char {
    fn introspect() -> TypeInfo {
        TypeInfo::Scalar(ScalarType::Char)
    }
}