static-reflect 0.1.9

Static type information, giving a form of compile-time reflection
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! The static type system
use crate::{StaticReflect, FieldReflect};

#[cfg(feature = "num")]
pub use self::num::{PrimNum, PrimValue};
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::fmt::{self, Formatter, Display, Debug};

#[cfg(feature = "gc")]
use zerogc_derive::{unsafe_gc_impl};

#[cfg(feature = "builtins")]
use crate::builtins::{AsmSlice, AsmStr};

/// A type which is never zero, and where optional types
/// are guaranteed to use the null-pointer representation
///
/// If `T: SimpleNonZeroPointer` -> `sizeof(Option<T>) == sizeof(T) && repr(Option<T>) == repr(T)`
pub unsafe trait SimpleNonZeroRepr: StaticReflect {}

/// An integer size, named in the style of C/Java
///
/// Although named after their C equivalents,
/// they are not necessarily compatible.
/// For example, the C standard technically allows 16-bit ints
/// or 32-bit longs.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(u8)]
pub enum IntSize {
    /// A single byte (`u8`)
    Byte = 1,
    /// A two byte short (`u16`)
    Short = 2,
    /// A four byte integer (`u32`)
    ///
    /// This is the default integer type (by convention)
    Int = 4,
    /// An eight byte integer (`u64`)
    Long = 8
}
impl IntSize {
    /// The size of the integer in bytes
    #[inline]
    pub const fn bytes(self) -> usize {
        self as usize
    }
    /// Get an integer size corresponding to the specified number of bytes
    #[inline]
    pub const fn from_bytes(bytes: usize) -> Result<IntSize, InvalidSizeErr> {
        Ok(match bytes {
            1 => IntSize::Byte,
            2 => IntSize::Short,
            4 => IntSize::Int,
            8 => IntSize::Long,
            _ => return Err(InvalidSizeErr { bytes })
        })
    }
}
impl Default for IntSize {
    #[inline]
    fn default() -> IntSize {
        IntSize::Int // `int` is the conventional default
    }
}
/// An error indicating that the size is invalid
#[derive(Debug)]
pub struct InvalidSizeErr {
    /// The size in bytes that is considered invalid
    pub bytes: usize,
}
impl Display for InvalidSizeErr {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Invalid size: {}", self.bytes)
    }
}
impl std::error::Error for InvalidSizeErr {}

/// The size of a floating point number,
/// either single-precision or double-precision
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum FloatSize {
    /// A single-precision floating point number.
    ///
    /// This type is 4 bytes (`f32`)
    Single = 4,
    /// A double-precision floating point number.
    /// 
    /// By convention, this type is the default.
    /// This type is 8 bytes (`f64`).
    Double = 8
}
impl FloatSize {
    /// The number of bytes for a float of this size
    #[inline]
    pub const fn bytes(self) -> usize {
        self as usize
    }
    /// Get a [FloatSize] corresponding to the specified
    /// number of bytes.
    #[inline]
    pub const fn from_bytes(bytes: usize) -> Result<FloatSize, InvalidSizeErr> {
        Ok(match bytes {
            4 => FloatSize::Single,
            8 => FloatSize::Double,
            _ => return Err(InvalidSizeErr { bytes })
        })
    }
}
impl Default for FloatSize {
    #[inline]
    fn default() -> FloatSize {
        FloatSize::Double
    }
}

/// A type whose representation is known via reflection
///
/// These are usually defined statically via [StaticReflect
///
/// However, they can be allocated at runtime,
/// and potentially live for a more limited lifetime.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum TypeInfo<'a> {
    /// The zero-length unit type `()`
    ///
    /// Used for functions that return nothing
    Unit,
    /// An impossible type,
    ///
    /// The mere existence of this type at runtime is undefined behavior.
    /// Functions that have this as their `return` type never actually return.
    #[cfg(feature = "never")]
    Never,
    /// A boolean
    ///
    /// Like a Rust `bool`, values must be either zero or one.
    /// Anything else is undefined behavior.
    Bool,
    /// An integer
    Integer {
        /// The size of the integer
        size: IntSize,
        /// If the integer is signed
        signed: bool
    },
    /// A floating point number
    Float {
        /// The size/precision of the float
        size: FloatSize
    },
    /// A slice of memory, represented as pointer + length
    ///
    /// The element type is needed, since array indexing implicitly
    /// multiples by the size of the memory.
    ///
    /// Representation should match the [AsmSlice] type
    #[cfg(feature = "builtins")]
    Slice {
        /// The type of the inner element
        element_type: &'a TypeInfo<'a>,
    },
    /// A pointer to a UTF8 encoded string and length,
    /// just like Rust's 'str' type
    ///
    /// Internally represented by the [AsmStr] structure
    #[cfg(feature = "builtins")]
    Str,
    /// A very simple optional, represented as an [AsmOption](crate::builtins::AsmOption)
    ///
    /// This **never** uses the null pointer optimization
    #[cfg(feature = "builtins")]
    Optional(&'a TypeInfo<'a>),
    /// An untyped pointer
    ///
    /// This may be null.
    ///
    /// Untyped pointers simplify the type system significantly.
    /// They also avoid cycles when defining structures
    /// in case a structure contains a pointer to itself.
    Pointer,
    /// A structure
    Structure(&'a StructureDef<'a>),
    /// An untagged union
    Union(&'a UnionDef<'a>),
    /// A named, transparent, extern type
    Extern {
        /// The name of the type
        ///
        /// Since this is all we have, it's what used
        /// to disambiguate between them.
        name: &'static str
    },
    /// A 'magic' type, with a user-defined meaning
    ///
    /// This allows extensions to the type system
    Magic {
        /// The id of the magic type,
        /// giving more information about how its implemented
        /// and what it actually means.
        id: &'static &'static str,
        /// Extra information (if any)
        extra: Option<&'a TypeInfo<'a>>,
    }
}
/*
 * HACK: Implement AsmType as `NullTrace`
 *
 * Unfortunately this means the type cannot use
 * garbage collected references.
 */
#[cfg(feature = "gc")]
unsafe_gc_impl! {
    target => TypeInfo<'a>,
    params => ['a],
    bounds => {
        Trace => always,
        TraceImmutable => always,
        GcSafe => always,
        GcRebrand => { where 'a: 'new_gc },
        GcErase => { where 'a: 'min }
    },
    null_trace => always,
    branded_type => Self,
    erased_type => Self,
    NEEDS_TRACE => false,
    NEEDS_DROP => ::std::mem::needs_drop::<Self>(),
    visit => |self, visitor| { Ok(()) /* nop */ }
}
impl TypeInfo<'static> {
    /// A 32-bit, single-precision float
    pub const F32: Self = TypeInfo::Float { size: FloatSize::Single };
    /// A 64-bit, double-precision float
    pub const F64: Self = TypeInfo::Float { size: FloatSize::Double };

    /// An integer with the specified size and signed-ness
    ///
    /// Panics if the size is invalid
    #[inline]
    pub const fn integer(size: usize, signed: bool) -> Self {
        let size = match IntSize::from_bytes(size) {
            Ok(s) => s,
            Err(_) => panic!("Invalid size")
        };
        TypeInfo::Integer { size, signed }
    }
}
impl<'tp> TypeInfo<'tp> {
    /// The size of the type, in bytes
    pub const fn size(&self) -> usize {
        use std::mem::size_of;
        use self::TypeInfo::*;
        match *self {
            Unit => 0,
            #[cfg(feature = "never")]
            Never => size_of::<!>(),
            Bool => size_of::<bool>(),
            Integer { size, signed: _ } => size.bytes(),
            Float { size } => size.bytes(),
            #[cfg(feature = "builtins")]
            Slice { .. } => std::mem::size_of::<AsmSlice<()>>(),
            #[cfg(feature = "builtins")]
            Optional(ref _inner) => unimplemented!(),
            Pointer => size_of::<*const ()>(),
            #[cfg(feature = "builtins")]
            Str => size_of::<AsmStr>(),
            Structure(ref def) => def.size,
            Union(ref def) => def.size,
            // Provide a dummy value
            TypeInfo::Magic { .. } | TypeInfo::Extern { .. } => 0xFFFF_FFFF
        }
    }
    /// The alignment of the type, matching `std::mem::align_of`
    pub const fn alignment(&self) -> usize {
        use std::mem::align_of;
        match *self {
            TypeInfo::Unit => align_of::<()>(),
            #[cfg(feature = "never")]
            TypeInfo::Never => align_of::<!>(),
            TypeInfo::Magic { .. } | TypeInfo::Extern { .. } => 0,
            TypeInfo::Bool => align_of::<bool>(),
            TypeInfo::Integer { size: IntSize::Byte, signed: false } => align_of::<u8>(),
            TypeInfo::Integer { size: IntSize::Short, signed: false } => align_of::<u16>(),
            TypeInfo::Integer { size: IntSize::Int, signed: false } => align_of::<u32>(),
            TypeInfo::Integer { size: IntSize::Long, signed: false } => align_of::<u64>(),
            TypeInfo::Integer { size: IntSize::Byte, signed: true } => align_of::<i8>(),
            TypeInfo::Integer { size: IntSize::Short, signed: true } => align_of::<i16>(),
            TypeInfo::Integer { size: IntSize::Int, signed: true } => align_of::<i32>(),
            TypeInfo::Integer { size: IntSize::Long, signed: true } => align_of::<i64>(),
            TypeInfo::Float { size: FloatSize::Single } => align_of::<f32>(),
            TypeInfo::Float { size: FloatSize::Double } => align_of::<f64>(),
            #[cfg(feature = "builtins")]
            TypeInfo::Slice { .. } | TypeInfo::Optional(_) => unimplemented!(),
            TypeInfo::Pointer => align_of::<*const ()>(),
            #[cfg(feature = "builtins")]
            TypeInfo::Str => align_of::<AsmStr>(),
            TypeInfo::Structure(ref def) => def.alignment,
            TypeInfo::Union(ref def) => def.alignment,
        }
    }
}
impl<'a> Display for TypeInfo<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            TypeInfo::Unit => f.write_str("()"),
            TypeInfo::Never => f.write_str("!"),
            TypeInfo::Bool => f.write_str("bool"),
            TypeInfo::Integer { size, signed: true } => write!(f, "i{}", size.bytes() * 8),
            TypeInfo::Integer { size, signed: false } => write!(f, "u{}", size.bytes() * 8),
            TypeInfo::Float { size } => write!(f, "f{}", size.bytes() * 8),
            TypeInfo::Slice { element_type } => write!(f, "[{}]", element_type),
            TypeInfo::Str => f.write_str("str"),
            TypeInfo::Optional(inner_type) => write!(f, "Option<{}>", inner_type),
            TypeInfo::Pointer => f.write_str("*mut void"),
            TypeInfo::Structure(ref def) => f.write_str(def.name),
            TypeInfo::Union(ref def) => f.write_str(def.name),
            TypeInfo::Extern { name } => write!(f, "extern {}", name),
            TypeInfo::Magic { id, extra: None } => write!(f, "magic::{}", id),
            TypeInfo::Magic { id, extra: Some(extra) } => write!(f, "magic::{}<{}>", id, extra)
        }
    }
}
/// Static information on the definition of a structure
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct StructureDef<'a> {
    /// The name of the structure
    pub name: &'a str,
    /// All of the fields defined in the structure
    pub fields: &'a [FieldDef<'a>],
    /// The total size of the structure (including padding)
    pub size: usize,
    /// The required alignment of the structure
    pub alignment: usize,
}
impl<T: StaticReflect> Copy for FieldDef<'_, T> {}
impl<T: StaticReflect> Clone for FieldDef<'_, T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}
/// The definition of a field
#[derive(Debug, Eq, PartialEq, Hash)]
pub struct FieldDef<'tp, T: StaticReflect = ()> {
    /// The name of the field
    pub name: &'tp str,
    /// The type of the field
    pub value_type: TypeId<'tp, T>,
    /// The offset of the field in bytes
    pub offset: usize,
    /// The numeric index of the field
    ///
    /// Should correspond to the order of declaration
    pub index: usize
}
impl<'a, T: StaticReflect> FieldDef<'a, T> {
    /// Erase the static type information from this field definition
    #[inline]
    pub const fn erase(&self) -> FieldDef<'a> {
        FieldDef {
            name: self.name,
            value_type: self.value_type.erase(),
            offset: self.offset,
            index: self.index
        }
    }
    /// The offset of the field, in bytes
    #[inline]
    pub const fn offset(&self) -> usize {
        self.offset
    }
}
/// A `UnionDef` which is known at compile-time
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct UnionDef<'a> {
    /// The name of the union
    pub name: &'a str,
    /// The fields of the union
    pub fields: &'a [UnionFieldDef<'a>],
    /// The size of the union, in bytes
    ///
    /// Should equal the size of its largest member
    pub size: usize,
    /// The required alignment of the union, in bytes
    ///
    /// I believe this should equal the maximum
    /// of the alignments required alignment by its members
    pub alignment: usize,
}

/// A field of a union which is known at compile-time
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct UnionFieldDef<'tp, T: StaticReflect = ()> {
    /// The name of the field
    pub name: &'tp str,
    /// The type of the field
    pub value_type: TypeId<'tp, T>,
    /// The numeric index of the field
    ///
    /// This has no effect on generated code, but it should probably correspond
    /// to the order of declaration in the source code
    pub index: usize
}
impl<'a, T: StaticReflect> UnionFieldDef<'a, T> {
    /// Erase the generic type of this field
    pub const fn erase(&self) -> UnionFieldDef<'a> {
        UnionFieldDef {
            name: self.name,
            value_type: self.value_type.erase(),
            index: self.index
        }
    }
    /// Offset of the field in the union
    ///
    /// The fields of unions never have any offset,
    /// so this is always zero.
    #[inline]
    pub const fn offset(&self) -> usize {
        0
    }
}

#[cfg(feature = "num")]
pub mod num {
    use num_traits::Num;
    use crate::NativeRepr;
    use std::fmt::Debug;

    pub trait PrimValue: NativeRepr + PartialEq + Copy + Debug {}
    impl PrimValue for bool {}
    impl PrimValue for () {}
    impl PrimValue for ! {}
    impl<T> PrimValue for *mut T {}
    impl<T: PrimNum> PrimValue for T {}

    pub trait PrimNum: NativeRepr + Num + Debug + Copy {}
    macro_rules! prim_num {
        ($($target:ty),*) => {$(
            impl PrimNum for $target {}
        )*};
    }
    prim_num!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize, f32, f64);
    pub trait PrimFloat: PrimNum {}
    impl PrimFloat for f32 {}
    impl PrimFloat for f64 {}
}


/// A primitive type
///
/// Although rust doesn't truly have a concept of 'primitives',
/// these are the most basic types needed to construct all the others.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PrimitiveType {
    /// The zero-length type '()'
    ///
    /// This is zero-length in the Rust sense,
    /// not the C sense
    Unit,
    /// The type for functions/instructions that can never return or occur
    ///
    /// It is undefined behavior for this type to even exist
    #[cfg(feature = "never")]
    Never,
    /// A boolean type, corresponding to Rust's [bool] type
    Bool,
    /// An untyped pointer (which is possibly null)
    Pointer,
    /// An integer
    Integer {
        /// The size of the integer
        size: IntSize,
        /// If the integer is signed
        signed: bool
    },
    /// A float
    Float {
        /// The size/precision of the float
        size: FloatSize
    },
}
impl PrimitiveType {
    /// The type information for this primitive type
    pub fn type_info(&self) -> &TypeInfo<'static> {
        use self::PrimitiveType::*;
        use self::IntSize::*;
        use self::FloatSize::*;
        match *self {
            Unit => &TypeInfo::Unit,
            Never => &TypeInfo::Never,
            Bool => &TypeInfo::Bool,
            Pointer => &TypeInfo::Pointer,
            Integer { size: Byte, signed: true } => &TypeInfo::Integer { size: Byte, signed: true },
            Integer { size: Short, signed: true } => &TypeInfo::Integer { size: Short, signed: true },
            Integer { size: Int, signed: true } => &TypeInfo::Integer { size: Int, signed: true },
            Integer { size: Long, signed: true } => &TypeInfo::Integer { size: Long, signed: true },
            Integer { size: Byte, signed: false } => &TypeInfo::Integer { size: Byte, signed: false },
            Integer { size: Short, signed: false } => &TypeInfo::Integer { size: Int, signed: false },
            Integer { size: Int, signed: false } => &TypeInfo::Integer { size: Short, signed: false },
            Integer { size: Long, signed: false } => &TypeInfo::Integer { size: Long, signed: false },
            Float { size: Single } => &TypeInfo::Float { size: Single },
            Float { size: Double } => &TypeInfo::Float { size: Double },
        }
    }

    /// The number of bytes this type tales up
    pub fn bytes(&self) -> usize {
        match self {
            PrimitiveType::Unit | PrimitiveType::Never => 0,
            PrimitiveType::Integer { size, signed: _ } => size.bytes(),
            PrimitiveType::Float { size } => size.bytes(),
            PrimitiveType::Pointer => {
                assert_eq!(std::mem::size_of::<usize>(), std::mem::size_of::<*mut ()>());
                std::mem::size_of::<*mut ()>() as usize
            },
            PrimitiveType::Bool => {
                assert_eq!(std::mem::size_of::<bool>(), 1);
                1
            },
        }
    }
    /// The size of this type, in bytes
    #[inline]
    pub fn size(self) -> usize {
        self.bytes() as usize
    }
}
/// Compare two primitive types based on their sizes
impl PartialOrd for PrimitiveType {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (*self, *other) {
            (
                PrimitiveType::Integer { size: first, signed: first_signed },
                PrimitiveType::Integer { size: second, signed: second_signed }
            ) => {
                Some(first.cmp(&second)
                    .then(first_signed.cmp(&second_signed)))
            },
            (PrimitiveType::Float { size: first }, PrimitiveType::Float { size: second }) => Some(first.cmp(&second)),
            (first, other) if first == other => Some(Ordering::Equal),
            _ => None
        }
    }
}

impl<T: StaticReflect> Copy for TypeId<'_, T> {}
impl<T: StaticReflect> Clone for TypeId<'_, T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

/// An static reference to a type
#[derive(Eq, PartialEq, Hash)]
pub struct TypeId<'a, T: StaticReflect = ()> {
    value: &'a TypeInfo<'a>,
    marker: PhantomData<fn() -> T>
}
/*
  * TODO: Fix to use derive
  *
  * Right now that doesn't work since it requires `T: 'a`
 */
#[cfg(feature = "gc")]
unsafe_gc_impl! {
    target => TypeId<'a, T>,
    params => ['a, T: StaticReflect],
    bounds => {
        Trace => always,
        TraceImmutable => always,
        GcSafe => always,
        GcRebrand => { where 'a: 'new_gc, T: 'new_gc },
        GcErase => { where 'a: 'min, T: 'min }
    },
    null_trace => always,
    branded_type => Self,
    erased_type => Self,
    NEEDS_TRACE => false,
    NEEDS_DROP => ::std::mem::needs_drop::<Self>(),
    visit => |self, visitor| { Ok(()) /* nop */ }
}
impl TypeId<'static> {
    /// Get the erased TypeId of the specified type `T`
    #[inline]
    pub const fn erased<T: StaticReflect>() -> TypeId<'static> {
        TypeId::<T>::get().erase()
    }
}
impl<T: StaticReflect> TypeId<'static, T> {
    /// Get the TypeId of the corresponding (generic) type
    #[inline]
    pub const fn get() -> Self {
        TypeId {
            value: &T::TYPE_INFO,
            marker: PhantomData
        }
    }
    /// Return a checked [TypeId] corresponding to the specified
    #[inline]
    pub const fn from_static(s: &'static TypeInfo<'static>) -> Self {
        TypeId {
            value: s,
            marker: PhantomData
        }
    }
}
impl<'tp, T: StaticReflect> TypeId<'tp, T> {
    /// Erase this type id,
    /// ignoring its statically-known generic
    /// parameters
    ///
    /// The generic parameters are unchecked, but are
    /// very useful for ensuring safety.
    #[inline]
    pub const fn erase(self) -> TypeId<'tp> {
        TypeId {
            value: self.value,
            marker: PhantomData,
        }
    }
    /// If this type is a boolean
    #[inline]
    pub fn is_bool(self) -> bool {
        matches!(self.primitive(), Some(PrimitiveType::Bool))
    }
    /// If this type is an integer (of any size)
    #[inline]
    pub fn is_int(self) -> bool {
        matches!(self.primitive(), Some(PrimitiveType::Integer { .. }))
    }
    /// If this type is a pointer
    #[inline]
    pub fn is_ptr(self) -> bool {
        matches!(self.primitive(), Some(PrimitiveType::Pointer { .. }))
    }
    /// If this type is a floating point number (of any size)
    #[inline]
    pub fn is_float(self) -> bool {
        matches!(self.primitive(), Some(PrimitiveType::Float { .. }))
    }
    /// If this type is a primitive
    #[inline]
    pub fn is_primitive(self) -> bool {
        self.primitive().is_some()
    }
    /// Convert this type into its corresponding [PrimitiveType],
    /// or `None` if it's not a primitive.
    #[inline]
    pub fn primitive(self) -> Option<PrimitiveType> {
        Some(match *self.value {
            TypeInfo::Unit => PrimitiveType::Unit,
            TypeInfo::Never => PrimitiveType::Never,
            TypeInfo::Bool => PrimitiveType::Bool,
            TypeInfo::Pointer => PrimitiveType::Pointer,
            TypeInfo::Integer { size, signed } => PrimitiveType::Integer { size, signed },
            TypeInfo::Float { size } => PrimitiveType::Float { size },
            _ => return None
        })
    }
    /// A reference to the underlying type
    #[inline]
    pub const fn type_ref(self) -> &'tp TypeInfo<'tp> {
        self.value
    }
    /// Create a [TypeId] from the specified reference
    #[inline]
    pub const fn from_ref(tp: &'tp TypeInfo<'tp>) -> Self {
        TypeId {
            marker: PhantomData,
            value: tp
        }
    }
    /// The information on this type's named fields
    #[inline]
    pub const fn named_field_info(&self) -> <T as FieldReflect>::NamedFieldInfo
        where T: FieldReflect {
        T::NAMED_FIELD_INFO
    }

}
impl<'a, T: StaticReflect> TypeId<'a, *mut T> {
    /// The target of the pointer type
    ///
    /// NOTE: This relies on static typing information
    #[inline]
    pub const fn pointer_target(self) -> TypeId<'a, T> {
        TypeId::get()
    }
}
impl<'tp> From<&'tp TypeInfo<'tp>> for TypeId<'tp> {
    #[inline]
    fn from(static_type: &'tp TypeInfo<'tp>) -> Self {
        TypeId::from_ref(static_type)
    }
}
impl<T: StaticReflect> Display for TypeId<'_, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.value, f)
    }
}
impl<T: StaticReflect> Debug for TypeId<'_, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_tuple("TypeId")
            .field(self.value)
            .finish()
    }
}

/// A indexed identifier of a field
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FieldId<'a> {
    /// The owner of the field
    pub owner: TypeId<'a>,
    /// The index of the field
    pub index: usize,
}