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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
use std::alloc::Layout;
use std::any::{type_name, TypeId};
use std::borrow::Cow;
use std::fmt::Debug;
/// Trait implemented by types that support runtime reflection on them.
///
/// # Safety
/// Implementors of this trait must provide accurate reflection information in the
/// `TYPE_REFLECTION` associated constant and the `type_reflection` and
/// `get_type_reflection` methods.
pub unsafe trait Reflection: 'static
{
const TYPE_REFLECTION: &Type;
fn type_reflection() -> &'static Type
where
Self: Sized,
{
Self::TYPE_REFLECTION
}
fn get_type_reflection(&self) -> &'static Type
{
Self::TYPE_REFLECTION
}
}
/// Trait implemented by enums that support runtime reflection on them.
///
/// # Safety
/// Implementors of this trait must provide accurate reflection information in the
/// `get_variant_reflection` method.
pub unsafe trait EnumReflectionExt: Reflection
{
fn get_variant_reflection(&self) -> &'static EnumVariant;
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Type
{
Struct(Struct),
Enum(Enum),
Array(Array),
Slice(Slice),
Literal(Literal),
}
impl Type
{
pub const fn as_struct(&self) -> Option<&Struct>
{
match self {
Self::Struct(struct_reflection) => Some(struct_reflection),
_ => None,
}
}
pub const fn as_enum(&self) -> Option<&Enum>
{
match self {
Self::Enum(enum_reflection) => Some(enum_reflection),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Struct
{
pub fields: &'static [Field],
}
#[derive(Debug, Clone)]
pub struct Enum
{
/// Enum variants in the same order as in the enum definition.
pub variants: &'static [EnumVariant],
/// The enum only contains unit variants.
pub is_unit_only: bool,
}
#[derive(Debug, Clone)]
pub struct EnumVariant
{
pub name: &'static str,
/// The fields of this variant. If `None`, this variant is a unit variant.
pub fields: Option<EnumVariantFields>,
}
#[derive(Debug, Clone)]
pub enum EnumVariantFields
{
Named
{
fields: &'static [Field]
},
Unnamed
{
fields: &'static [Field]
},
}
#[derive(Debug, Clone)]
pub struct Field
{
pub name: Option<&'static str>,
pub index: usize,
pub layout: Layout,
pub byte_offset: Option<usize>,
pub type_id: TypeId,
pub type_name: &'static str,
pub get_type: FnWithDebug<Option<&'static Type>>,
pub visibility: Visibility,
}
impl Field
{
pub fn type_reflection(&self) -> Option<&'static Type>
{
self.get_type.get()
}
}
#[derive(Debug, Clone)]
pub struct Array
{
pub item_reflection: &'static Type,
pub length: usize,
}
#[derive(Debug, Clone)]
pub struct Slice
{
pub item_reflection: &'static Type,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Literal
{
pub layout: Layout,
pub type_id: TypeId,
pub ty: LiteralType,
pub type_name: fn() -> &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum LiteralType
{
U8,
I8,
U16,
I16,
U32,
I32,
U64,
I64,
U128,
I128,
F32,
F64,
Usize,
Isize,
Bool,
Str,
}
#[derive(Debug, Clone)]
pub enum Visibility
{
Pub,
PubScoped(VisibilityScope),
Private,
}
#[derive(Debug, Clone)]
pub enum VisibilityScope
{
Crate,
Super,
SelfModule,
In(Cow<'static, str>),
}
macro_rules! impl_reflection_for_literals {
($(($literal_type: ident, $literal: ty)),*) => {
$(
unsafe impl Reflection for $literal
{
const TYPE_REFLECTION: &Type = &Type::Literal(Literal {
layout: Layout::new::<$literal>(),
type_id: TypeId::of::<$literal>(),
ty: LiteralType::$literal_type,
type_name: || type_name::<$literal>()
});
}
)*
};
}
impl_reflection_for_literals!(
(U8, u8),
(I8, i8),
(U16, u16),
(I16, i16),
(U32, u32),
(I32, i32),
(U64, u64),
(I64, i64),
(U128, u128),
(I128, i128),
(F32, f32),
(F64, f64),
(Usize, usize),
(Isize, isize),
(Bool, bool),
(Str, &'static str)
);
unsafe impl<T: Reflection, const LEN: usize> Reflection for [T; LEN]
{
const TYPE_REFLECTION: &Type = &Type::Array(Array {
item_reflection: T::TYPE_REFLECTION,
length: LEN,
});
}
unsafe impl<T: Reflection> Reflection for &'static [T]
{
const TYPE_REFLECTION: &Type =
&Type::Slice(Slice { item_reflection: T::TYPE_REFLECTION });
}
#[derive(Clone)]
pub struct FnWithDebug<Value>
{
func: fn() -> Value,
}
impl<Value> FnWithDebug<Value>
{
pub const fn new(func: fn() -> Value) -> Self
{
Self { func }
}
pub fn get(&self) -> Value
{
(self.func)()
}
}
impl<Value: Debug> Debug for FnWithDebug<Value>
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
formatter
.debug_tuple("FnWithDebug")
.field(&self.get())
.finish()
}
}
|