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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Runtime information about a type

use crate::info::*;
use crate::reflect::*;
use crate::utils::StaticTypeMap;
use crate::{Error, Value};

use core::fmt;
use core::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::lazy::SyncOnceCell;
use std::sync::RwLock;

/// Implement CommonTypeInfo for a given struct
macro impl_common($ty:ty) {
    impl CommonTypeInfo for $ty {
        fn name(&self) -> String {
            (self.vtable.name)()
        }

        fn assoc_fns(&self) -> Vec<AssocFn> {
            (self.vtable.assoc_fns)()
        }

        fn assoc_consts(&self) -> Vec<AssocConst> {
            (self.vtable.assoc_consts)()
        }

        fn as_ref<'a>(&self, val: &'a Value) -> Result<Value<'a>, Error> {
            (self.vtable.as_ref)(val)
        }

        fn as_mut<'a>(&self, val: &'a mut Value) -> Result<Value<'a>, Error> {
            (self.vtable.as_mut)(val)
        }
    }
}

// SAFETY: *do not touch these if you don't know what you're doing*
static REFLECTED_TYS: SyncOnceCell<RwLock<HashMap<String, Type>>> = SyncOnceCell::new();

/// Common information / operations between all types
pub trait CommonTypeInfo {
    /// Get this type's name
    fn name(&self) -> String;
    /// Get the known associated functions of this type
    fn assoc_fns(&self) -> Vec<AssocFn>;
    /// Get the known associated constants of this type
    fn assoc_consts(&self) -> Vec<AssocConst>;
    // fn impled_traits(&self) -> Vec<TraitInfo>;

    /// Convert a Value of this type to a reference to that value, if it's not already a reference
    fn as_ref<'a>(&self, val: &'a Value) -> Result<Value<'a>, Error>;
    /// Convert a Value of this type to a mutable reference to that value, if it's not already a
    /// reference
    fn as_mut<'a>(&self, val: &'a mut Value) -> Result<Value<'a>, Error>;
}

/// Common VTable used by all types
#[derive(Copy, Clone)]
struct TypeVTable {
    name: fn() -> String,
    assoc_fns: fn() -> Vec<AssocFn>,
    assoc_consts: fn() -> Vec<AssocConst>,

    as_ref: for<'a> fn(&'a Value) -> Result<Value<'a>, Error>,
    as_mut: for<'a> fn(&'a mut Value) -> Result<Value<'a>, Error>,
}

impl fmt::Debug for TypeVTable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "TypeVTable {{ name: {:?}, assoc_fns: {:?}, assoc_consts: {:?}, as_ref: {:p} }}",
            self.name, self.assoc_fns, self.assoc_consts, self.as_ref as *const ()
        )
    }
}

impl TypeVTable {
    fn new<T: ?Sized + Reflected>() -> TypeVTable {
        TypeVTable {
            name: T::name,
            assoc_fns: T::assoc_fns,
            assoc_consts: T::assoc_consts,

            as_ref: <T as Ref>::ref_val,
            as_mut: <T as Ref>::mut_val,
        }
    }
}

/// An enum that represents information about a reflected type. This supports basically any possible
/// type in Rust, including primitives, arrays, and references. Generally, the only requirement is
/// that the type implement the [`Reflected`] trait, though most types are also expected to
/// implement another trait related to information they possess not shared by other type kinds.
///
/// This is not, and cannot, be backed by [`core::any::TypeId`], because that is only valid on
/// `'static` types, while this works with dynamic lifetimes.
#[derive(Debug, Copy, Clone)]
pub enum Type {
    /// A primitive simple type, such as `u8` or `str`
    Primitive(PrimitiveInfo),
    /// A tuple type, `(T0, .., Tn)`
    Tuple(TupleInfo),
    /// An array type, `[T; N]`
    Array(ArrayInfo),
    /// A slice type, `[T]`
    Slice(SliceInfo),
    /// A pointer type, either `*const T` or `*mut T`
    Pointer(PointerInfo),
    /// A reference type, either `&T` or `&mut T`
    Reference(ReferenceInfo),
    /// A function pointer type, `fn(T1..Tn) -> T0`
    Function(FunctionInfo),

    /// A struct type, with named fields
    Struct(StructInfo),
    /// A struct type, with unnamed fields
    TupleStruct(TupleStructInfo),
    /// A struct type, with no fields
    UnitStruct(UnitStructInfo),
    /// An enum type, with variants
    Enum(EnumInfo),
    /// A union type, with fields
    Union(UnionInfo),
}

/// Generate unwrap functions for each of the variants of ty
macro_rules! ty_unwraps {
    ($($var:ident),+) => {
        paste::paste! {
            $(
                #[doc = "Get this Type as a [`" $var "Info`], panicking on failure."]
                #[track_caller]
                pub fn [<unwrap_ $var:snake>](&self) -> & [<$var Info>] {
                    if let Type::$var(info) = self {
                        info
                    } else {
                        panic!(concat!("Attempted to unwrap non-", stringify!($var:lower), " Type as ", stringify!($var:lower)))
                    }
                }
            )*
        }
    }
}

impl Type {
    fn add_ty(ty: Type) {
        let mut map = REFLECTED_TYS
            .get_or_init(|| RwLock::new(HashMap::new()))
            .write()
            .expect("REFLECTED_TYS not initialized correctly");

        let name = ty.name();

        if map.contains_key(&name) {
            panic!("Type {} already registered", name);
        }

        map.insert(name, ty);
    }

    /// Internal function used by generated code to initialize a Type for primitives
    #[doc(hidden)]
    pub unsafe fn new_prim<T: ?Sized + Reflected>() {
        let ty = Type::Primitive(PrimitiveInfo {
            vtable: TypeVTable::new::<T>(),
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for tuples
    #[doc(hidden)]
    pub unsafe fn new_tuple<T: ?Sized + ReflectedTuple>() {
        let ty = Type::Tuple(TupleInfo {
            vtable: TypeVTable::new::<T>(),
            fields: T::fields,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for arrays
    #[doc(hidden)]
    pub unsafe fn new_array<T: ?Sized + ReflectedArray>() {
        let ty = Type::Array(ArrayInfo {
            vtable: TypeVTable::new::<T>(),
            element: T::element,
            length: T::length(),
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for slices
    #[doc(hidden)]
    pub unsafe fn new_slice<T: ?Sized + ReflectedSlice>() {
        let ty = Type::Slice(SliceInfo {
            vtable: TypeVTable::new::<T>(),
            element: T::element,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for pointers
    #[doc(hidden)]
    pub unsafe fn new_ptr<T: ReflectedPointer>() {
        let ty = Type::Pointer(PointerInfo {
            vtable: TypeVTable::new::<T>(),
            element: T::element,
            mutability: T::mutability(),
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for references
    #[doc(hidden)]
    pub unsafe fn new_ref<T: ReflectedReference>() {
        let ty = Type::Reference(ReferenceInfo {
            vtable: TypeVTable::new::<T>(),
            element: T::element,
            mutability: T::mutability(),
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for function pointers
    #[doc(hidden)]
    pub unsafe fn new_fn<T: ReflectedFunction>() {
        let ty = Type::Function(FunctionInfo {
            vtable: TypeVTable::new::<T>(),
            args: T::args,
            ret: T::ret,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for structs
    ///
    /// # Safety
    ///
    /// Should only be called inside a [`Reflected`] type's `init` impl
    pub unsafe fn new_struct<T: ?Sized + ReflectedStruct>() {
        let ty = Type::Struct(StructInfo {
            vtable: TypeVTable::new::<T>(),
            fields: T::fields,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for tuple structs
    ///
    /// # Safety
    ///
    /// Should only be called inside a [`Reflected`] type's `init` impl
    pub unsafe fn new_tuple_struct<T: ReflectedTupleStruct>() {
        let ty = Type::TupleStruct(TupleStructInfo {
            vtable: TypeVTable::new::<T>(),
            fields: T::fields,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for unit structs
    ///
    /// # Safety
    ///
    /// Should only be called inside a [`Reflected`] type's `init` impl
    pub unsafe fn new_unit_struct<T: ReflectedUnitStruct>() {
        let ty = Type::UnitStruct(UnitStructInfo {
            vtable: TypeVTable::new::<T>(),
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for enums
    ///
    /// # Safety
    ///
    /// Should only be called inside a [`Reflected`] type's `init` impl
    pub unsafe fn new_enum<T: ReflectedEnum>() {
        let ty = Type::Enum(EnumInfo {
            vtable: TypeVTable::new::<T>(),
            variants: T::variants,
        });

        Type::add_ty(ty);
    }

    /// Internal function used by generated code to initialize a Type for unions
    ///
    /// # Safety
    ///
    /// Should only be called inside a [`Reflected`] type's `init` impl
    pub unsafe fn new_union<T: ReflectedUnion>() {
        let ty = Type::Union(UnionInfo {
            vtable: TypeVTable::new::<T>(),
            fields: T::fields,
        });

        Type::add_ty(ty);
    }

    /// Get a Type instance by name, assuming it has been instantiated beforehand.
    /// The name provided is expected to be of a certain normalized form, which may not
    /// be fully stable between versions. Prefer [`Type::from`] if possible.
    ///
    /// Current Requirements:
    /// - All struct names should be fully qualified, so for example the Type for Type would be
    ///   `rebound::ty::Type`
    /// - Any commas will be followed by spaces, and there will be no trailing commas except in the
    ///   case of 1-element tuples
    /// - References will have no lifetime
    /// - Possibly other things
    ///
    /// # Safety
    ///
    /// This function is in no way memory unsafe, however, the format used for type names is an
    /// implementation detail, and thus may change even across patch versions.
    pub unsafe fn from_name(name: &str) -> Option<Type> {
        REFLECTED_TYS
            .get_or_init(|| RwLock::new(HashMap::new()))
            .read()
            .expect("Couldn't get read lock on Reflection mapping")
            .get(name)
            .copied()
    }

    /// Get a Type instance from any reflected type, instantiating it if necessary.
    pub fn from<T: ?Sized + Reflected>() -> Type {
        static INIT: SyncOnceCell<StaticTypeMap<()>> = SyncOnceCell::new();
        INIT.get_or_init(StaticTypeMap::new).call_once::<T, _>(|| {
            unsafe { T::init() };
        });

        unsafe { Type::from_name(&T::name()).expect("Type not initialized") }
    }

    fn as_inner(&self) -> &dyn CommonTypeInfo {
        match self {
            Type::Primitive(i) => i,
            Type::Tuple(i) => i,
            Type::Slice(i) => i,
            Type::Array(i) => i,
            Type::Pointer(i) => i,
            Type::Reference(i) => i,
            Type::Function(i) => i,

            Type::Struct(i) => i,
            Type::TupleStruct(i) => i,
            Type::UnitStruct(i) => i,
            Type::Enum(i) => i,
            Type::Union(i) => i,
        }
    }

    ty_unwraps!(
        Primitive,
        Tuple,
        Array,
        Slice,
        Pointer,
        Reference,
        Function,
        Struct,
        TupleStruct,
        UnitStruct,
        Enum,
        Union
    );
}

impl PartialEq for Type {
    fn eq(&self, other: &Type) -> bool {
        // This is safe because Type creation is based on the name, overlaps will cause warnings
        self.name() == other.name()
    }
}

impl Eq for Type {}

impl Hash for Type {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name().hash(state);
    }
}

impl CommonTypeInfo for Type {
    fn name(&self) -> String {
        self.as_inner().name()
    }

    fn assoc_fns(&self) -> Vec<AssocFn> {
        self.as_inner().assoc_fns()
    }

    fn assoc_consts(&self) -> Vec<AssocConst> {
        self.as_inner().assoc_consts()
    }

    fn as_ref<'a>(&self, val: &'a Value) -> Result<Value<'a>, Error> {
        self.as_inner().as_ref(val)
    }

    fn as_mut<'a>(&self, val: &'a mut Value) -> Result<Value<'a>, Error> {
        self.as_inner().as_mut(val)
    }
}

/// Type information about a primitive reflected type
#[derive(Debug, Copy, Clone)]
pub struct PrimitiveInfo {
    vtable: TypeVTable,
}

impl_common!(PrimitiveInfo);

/// Type information about a reflected tuple
#[derive(Debug, Copy, Clone)]
pub struct TupleInfo {
    vtable: TypeVTable,
    fields: fn() -> Vec<Field>,
}

impl TupleInfo {
    /// Get all the [`Fields`](Field) of this Tuple
    pub fn fields(&self) -> Vec<Field> {
        (self.fields)()
    }
}

impl_common!(TupleInfo);

/// Type information about a reflected array
#[derive(Debug, Copy, Clone)]
pub struct ArrayInfo {
    vtable: TypeVTable,
    element: fn() -> Type,
    length: usize,
}

impl ArrayInfo {
    /// Get the element [`Type`] of this Array
    pub fn element(&self) -> Type {
        (self.element)()
    }

    /// Get the length of this Array
    pub fn length(&self) -> usize {
        self.length
    }
}

impl_common!(ArrayInfo);

/// Type information about a reflected slice
#[derive(Debug, Copy, Clone)]
pub struct SliceInfo {
    vtable: TypeVTable,
    element: fn() -> Type,
}

impl SliceInfo {
    /// Get the element [`Type`] of this Slice
    pub fn element(&self) -> Type {
        (self.element)()
    }
}

impl_common!(SliceInfo);

/// Type information about a reflected pointer
#[derive(Debug, Copy, Clone)]
pub struct PointerInfo {
    vtable: TypeVTable,
    element: fn() -> Type,
    mutability: bool,
}

impl PointerInfo {
    /// Get the element [`Type`] of this Pointer
    pub fn element(&self) -> Type {
        (self.element)()
    }

    /// Check whether this pointer is mutable
    pub fn mutability(&self) -> bool {
        self.mutability
    }
}

impl_common!(PointerInfo);

/// Type information about a reflected reference
#[derive(Debug, Copy, Clone)]
pub struct ReferenceInfo {
    vtable: TypeVTable,
    element: fn() -> Type,
    mutability: bool,
}

impl ReferenceInfo {
    /// Get the element [`Type`] of this Reference
    pub fn element(&self) -> Type {
        (self.element)()
    }

    /// Check whether this reference is mutable
    pub fn mutability(&self) -> bool {
        self.mutability
    }
}

impl_common!(ReferenceInfo);

/// Type information about a reflected function pointer
#[derive(Debug, Copy, Clone)]
pub struct FunctionInfo {
    vtable: TypeVTable,
    args: fn() -> Vec<Type>,
    ret: fn() -> Type,
}

impl FunctionInfo {
    /// Get the argument [`Types`](Type) of this Function
    pub fn arg_tys(&self) -> Vec<Type> {
        (self.args)()
    }

    /// Get the return [`Type`] of this Function
    pub fn ret_ty(&self) -> Type {
        (self.ret)()
    }
}

impl_common!(FunctionInfo);

/// Type information about a reflected struct
#[derive(Debug, Copy, Clone)]
pub struct StructInfo {
    vtable: TypeVTable,
    fields: fn() -> Vec<Field>,
}

impl StructInfo {
    /// Get all the [`Fields`](Field) of this Struct
    pub fn fields(&self) -> Vec<Field> {
        (self.fields)()
    }
}

impl_common!(StructInfo);

/// Type information about a reflected tuple-struct
#[derive(Debug, Copy, Clone)]
pub struct TupleStructInfo {
    vtable: TypeVTable,
    fields: fn() -> Vec<Field>,
}

impl TupleStructInfo {
    /// Get all the [`Fields`](Field) of this Tuple Struct
    pub fn fields(&self) -> Vec<Field> {
        (self.fields)()
    }
}

impl_common!(TupleStructInfo);

/// Type information about a reflected unit struct
#[derive(Debug, Copy, Clone)]
pub struct UnitStructInfo {
    vtable: TypeVTable,
}

impl_common!(UnitStructInfo);

/// Type information about a reflected enum
#[derive(Debug, Copy, Clone)]
pub struct EnumInfo {
    vtable: TypeVTable,
    variants: fn() -> Vec<Variant>,
}

impl EnumInfo {
    /// Get all the [`Variants`](Variant) of this Enum
    pub fn variants(&self) -> Vec<Variant> {
        (self.variants)()
    }

    pub fn variant_of(&self, val: &Value) -> Result<Variant, Error> {
        for i in self.variants() {
            if i.is_variant(val)? {
                return Ok(i);
            }
        }
        unreachable!("An instance of an enum should always be one of its variants")
    }

    /// Check whether a [`Value`] is of a specific [`Variant`], if it's of this type
    pub fn is_variant(&self, val: &Value, var: &Variant) -> Result<bool, Error> {
        if var.assoc_ty() == Type::Enum(*self) {
            var.is_variant(val)
        } else {
            Err(Error::wrong_type(var.assoc_ty(), Type::Enum(*self)))
        }
    }
}

impl_common!(EnumInfo);

/// Type information about a reflected union
#[derive(Debug, Copy, Clone)]
pub struct UnionInfo {
    vtable: TypeVTable,
    fields: fn() -> Vec<UnionField>,
}

impl UnionInfo {
    /// Get all the [`Fields`](Field) of this Union
    pub fn fields(&self) -> Vec<UnionField> {
        (self.fields)()
    }
}

impl_common!(UnionInfo);