introspectable/
lib.rs

1use info::{ScalarType, TypeInfo};
2
3/// Export introspectable_derive
4#[cfg(feature="derive")]
5pub use introspectable_derive::Introspectable;
6
7/// This module provides type info structures and enums
8pub mod info;
9
10/// This module implements Introspectable for several core standard library collections
11#[cfg(feature="specialized_std")]
12pub mod std_impl;
13
14
15/// The main trait of this library which allows introspection on both structs and enums
16pub trait Introspectable {
17    /// Returns type info for the implemented type.
18    fn introspect() -> info::TypeInfo;
19
20    /// Returns type info for an instance of a type
21    fn introspect_instance(&self) -> info::TypeInfo {
22        Self::introspect()
23    }
24}
25
26// Implementation of Introspectable for tuple sizes up to 16
27// A macro is used for this
28
29#[macro_export]
30macro_rules! impl_introspectable {
31    ($($i:ident),+) => {
32        impl<$($i: Introspectable),+> Introspectable for ($($i,)+) {
33            fn introspect() -> info::TypeInfo {
34                info::TypeInfo::Compound(
35                    info::CompoundType::Tuple{
36                        fields: vec![$($i::introspect(),)+]
37                    }
38                )
39            }
40        }
41    };
42}
43
44impl_introspectable!(T1);
45impl_introspectable!(T1, T2);
46impl_introspectable!(T1, T2, T3);
47impl_introspectable!(T1, T2, T3, T4);
48impl_introspectable!(T1, T2, T3, T4, T5);
49impl_introspectable!(T1, T2, T3, T4, T5, T6);
50impl_introspectable!(T1, T2, T3, T4, T5, T6, T7);
51impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8);
52impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
53impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
54impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
55impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
56impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
57impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
58impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
59impl_introspectable!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
60
61// Implementation of Introspectable for pointer types
62
63
64impl<T: Introspectable> Introspectable for *const T {
65    fn introspect() -> info::TypeInfo {
66        info::TypeInfo::Pointer(
67            info::PointerType::ConstPointer(Box::new(T::introspect()))
68        )
69    }
70}
71
72impl<T: Introspectable> Introspectable for *mut T {
73    fn introspect() -> info::TypeInfo {
74        info::TypeInfo::Pointer(
75            info::PointerType::MutPointer(Box::new(T::introspect()))
76        )
77    }
78}
79
80
81
82// Implementation of Introspectable for all scalar types
83
84impl Introspectable for bool {
85    fn introspect() -> TypeInfo {
86        TypeInfo::Scalar(ScalarType::Bool)
87    }
88}
89
90impl Introspectable for i8 {
91    fn introspect() -> TypeInfo {
92        TypeInfo::Scalar(ScalarType::I8)
93    }
94}
95
96impl Introspectable for i16 {
97    fn introspect() -> TypeInfo {
98        TypeInfo::Scalar(ScalarType::I16)
99    }
100}
101
102impl Introspectable for i32 {
103    fn introspect() -> TypeInfo {
104        TypeInfo::Scalar(ScalarType::I32)
105    }
106}
107
108impl Introspectable for i64 {
109    fn introspect() -> TypeInfo {
110        TypeInfo::Scalar(ScalarType::I64)
111    }
112}
113
114impl Introspectable for i128 {
115    fn introspect() -> TypeInfo {
116        TypeInfo::Scalar(ScalarType::I128)
117    }
118}
119
120impl Introspectable for u8 {
121    fn introspect() -> TypeInfo {
122        TypeInfo::Scalar(ScalarType::U8)
123    }
124}
125
126impl Introspectable for u16 {
127    fn introspect() -> TypeInfo {
128        TypeInfo::Scalar(ScalarType::U16)
129    }
130}
131
132impl Introspectable for u32 {
133    fn introspect() -> TypeInfo {
134        TypeInfo::Scalar(ScalarType::U32)
135    }
136}
137
138impl Introspectable for u64 {
139    fn introspect() -> TypeInfo {
140        TypeInfo::Scalar(ScalarType::U64)
141    }
142}
143
144impl Introspectable for u128 {
145    fn introspect() -> TypeInfo {
146        TypeInfo::Scalar(ScalarType::U128)
147    }
148}
149
150impl Introspectable for f32 {
151    fn introspect() -> TypeInfo {
152        TypeInfo::Scalar(ScalarType::F32)
153    }
154}
155
156impl Introspectable for f64 {
157    fn introspect() -> TypeInfo {
158        TypeInfo::Scalar(ScalarType::F64)
159    }
160}
161
162impl Introspectable for char {
163    fn introspect() -> TypeInfo {
164        TypeInfo::Scalar(ScalarType::Char)
165    }
166}