Skip to main content

core/mem/
type_info.rs

1//! MVP for exposing compile-time information about types in a
2//! runtime or const-eval processable way.
3
4use crate::any::TypeId;
5use crate::fmt;
6use crate::intrinsics::{self, type_id, type_of};
7use crate::marker::PointeeSized;
8use crate::ptr::DynMetadata;
9
10/// Compile-time type information.
11#[derive(Debug)]
12#[non_exhaustive]
13#[lang = "type_info"]
14#[unstable(feature = "type_info", issue = "146922")]
15pub struct Type {
16    /// Per-type information
17    pub kind: TypeKind,
18}
19
20/// Info of a trait implementation, you can retrieve the vtable with [Self::get_vtable]
21#[derive(Debug, PartialEq, Eq)]
22#[unstable(feature = "type_info", issue = "146922")]
23#[non_exhaustive]
24pub struct TraitImpl<T: PointeeSized> {
25    pub(crate) vtable: DynMetadata<T>,
26}
27
28impl<T: PointeeSized> TraitImpl<T> {
29    /// Gets the raw vtable for type reflection mapping
30    pub const fn get_vtable(&self) -> DynMetadata<T> {
31        self.vtable
32    }
33}
34
35impl TypeId {
36    /// Compute the type information of a concrete type.
37    /// It can only be called at compile time.
38    #[unstable(feature = "type_info", issue = "146922")]
39    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
40    pub const fn info(self) -> Type {
41        type_of(self)
42    }
43}
44
45impl Type {
46    /// Returns the type information of the generic type parameter.
47    ///
48    /// Note: Unlike `TypeId`s obtained via `TypeId::of`, the `Type`
49    /// struct and its fields contain `TypeId`s that are not necessarily
50    /// derived from types that outlive `'static`. This means that using
51    /// the `TypeId`s (transitively) obtained from this function will
52    /// be able to break invariants that other `TypeId` consuming crates
53    /// may have assumed to hold.
54    #[unstable(feature = "type_info", issue = "146922")]
55    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
56    pub const fn of<T: ?Sized>() -> Self {
57        const { type_id::<T>().info() }
58    }
59}
60
61/// Compile-time type information.
62#[derive(Debug)]
63#[non_exhaustive]
64#[unstable(feature = "type_info", issue = "146922")]
65pub enum TypeKind {
66    /// Tuples.
67    Tuple(Tuple),
68    /// Arrays.
69    Array(Array),
70    /// Slices.
71    Slice(Slice),
72    /// Dynamic Traits.
73    DynTrait(DynTrait),
74    /// Structs.
75    Struct(Struct),
76    /// Enums.
77    Enum(Enum),
78    /// Unions.
79    Union(Union),
80    /// Primitive boolean type.
81    Bool(Bool),
82    /// Primitive character type.
83    Char(Char),
84    /// Primitive signed and unsigned integer type.
85    Int(Int),
86    /// Primitive floating-point type.
87    Float(Float),
88    /// String slice type.
89    Str(Str),
90    /// References.
91    Reference(Reference),
92    /// Pointers.
93    Pointer(Pointer),
94    /// Function pointers.
95    FnPtr(FnPtr),
96    /// FIXME(#146922): add all the common types
97    Other,
98}
99
100/// Compile-time type information about tuples.
101#[derive(Debug)]
102#[non_exhaustive]
103#[unstable(feature = "type_info", issue = "146922")]
104pub struct Tuple {
105    /// All fields of a tuple.
106    pub fields: &'static [Field],
107}
108
109/// Compile-time type information about fields of tuples, structs and enum variants.
110#[derive(Debug)]
111#[non_exhaustive]
112#[unstable(feature = "type_info", issue = "146922")]
113pub struct Field {
114    /// The name of the field.
115    pub name: &'static str,
116    /// The field's type.
117    pub ty: TypeId,
118    /// Offset in bytes from the parent type
119    pub offset: usize,
120}
121
122/// Compile-time type information about arrays.
123#[derive(Debug)]
124#[non_exhaustive]
125#[unstable(feature = "type_info", issue = "146922")]
126pub struct Array {
127    /// The type of each element in the array.
128    pub element_ty: TypeId,
129    /// The length of the array.
130    pub len: usize,
131}
132
133/// Compile-time type information about slices.
134#[derive(Debug)]
135#[non_exhaustive]
136#[unstable(feature = "type_info", issue = "146922")]
137pub struct Slice {
138    /// The type of each element in the slice.
139    pub element_ty: TypeId,
140}
141
142/// Compile-time type information about dynamic traits.
143/// FIXME(#146922): Add super traits and generics
144#[derive(Debug)]
145#[non_exhaustive]
146#[unstable(feature = "type_info", issue = "146922")]
147pub struct DynTrait {
148    /// The predicates of  a dynamic trait.
149    pub predicates: &'static [DynTraitPredicate],
150}
151
152/// Compile-time type information about a dynamic trait predicate.
153#[derive(Debug)]
154#[non_exhaustive]
155#[unstable(feature = "type_info", issue = "146922")]
156pub struct DynTraitPredicate {
157    /// The type of the trait as a dynamic trait type.
158    pub trait_ty: Trait,
159}
160
161/// Compile-time type information about a trait.
162#[derive(Debug)]
163#[non_exhaustive]
164#[unstable(feature = "type_info", issue = "146922")]
165pub struct Trait {
166    /// The TypeId of the trait as a dynamic type
167    pub ty: TypeId,
168    /// Whether the trait is an auto trait
169    pub is_auto: bool,
170}
171
172/// Compile-time type information about structs.
173#[derive(Debug)]
174#[non_exhaustive]
175#[unstable(feature = "type_info", issue = "146922")]
176pub struct Struct {
177    /// Instantiated generics of the struct.
178    pub generics: &'static [Generic],
179    /// All fields of the struct.
180    pub fields: &'static [Field],
181    /// Whether the struct field list is non-exhaustive.
182    pub non_exhaustive: bool,
183}
184
185/// Compile-time type information about unions.
186#[derive(Debug)]
187#[non_exhaustive]
188#[unstable(feature = "type_info", issue = "146922")]
189pub struct Union {
190    /// Instantiated generics of the union.
191    pub generics: &'static [Generic],
192    /// All fields of the union.
193    pub fields: &'static [Field],
194}
195
196/// Compile-time type information about enums.
197#[derive(Debug)]
198#[non_exhaustive]
199#[unstable(feature = "type_info", issue = "146922")]
200pub struct Enum {
201    /// Instantiated generics of the enum.
202    pub generics: &'static [Generic],
203    /// All variants of the enum.
204    pub variants: &'static [Variant],
205    /// Whether the enum variant list is non-exhaustive.
206    pub non_exhaustive: bool,
207}
208
209/// Compile-time type information about variants of enums.
210#[derive(Debug)]
211#[non_exhaustive]
212#[unstable(feature = "type_info", issue = "146922")]
213pub struct Variant {
214    /// The name of the variant.
215    pub name: &'static str,
216    /// All fields of the variant.
217    pub fields: &'static [Field],
218    /// Whether the enum variant fields is non-exhaustive.
219    pub non_exhaustive: bool,
220}
221
222/// Compile-time type information about instantiated generics of structs, enum and union variants.
223#[derive(Debug)]
224#[non_exhaustive]
225#[unstable(feature = "type_info", issue = "146922")]
226pub enum Generic {
227    /// Lifetimes.
228    Lifetime(Lifetime),
229    /// Types.
230    Type(GenericType),
231    /// Const parameters.
232    Const(Const),
233}
234
235/// Compile-time type information about generic lifetimes.
236#[derive(Debug)]
237#[non_exhaustive]
238#[unstable(feature = "type_info", issue = "146922")]
239pub struct Lifetime {
240    // No additional information to provide for now.
241}
242
243/// Compile-time type information about instantiated generic types.
244#[derive(Debug)]
245#[non_exhaustive]
246#[unstable(feature = "type_info", issue = "146922")]
247pub struct GenericType {
248    /// The type itself.
249    pub ty: TypeId,
250}
251
252/// Compile-time type information about generic const parameters.
253#[derive(Debug)]
254#[non_exhaustive]
255#[unstable(feature = "type_info", issue = "146922")]
256pub struct Const {
257    /// The const's type.
258    pub ty: TypeId,
259}
260
261/// Compile-time type information about `bool`.
262#[derive(Debug)]
263#[non_exhaustive]
264#[unstable(feature = "type_info", issue = "146922")]
265pub struct Bool {
266    // No additional information to provide for now.
267}
268
269/// Compile-time type information about `char`.
270#[derive(Debug)]
271#[non_exhaustive]
272#[unstable(feature = "type_info", issue = "146922")]
273pub struct Char {
274    // No additional information to provide for now.
275}
276
277/// Compile-time type information about signed and unsigned integer types.
278#[derive(Debug)]
279#[non_exhaustive]
280#[unstable(feature = "type_info", issue = "146922")]
281pub struct Int {
282    /// The bit width of the signed integer type.
283    pub bits: u32,
284    /// Whether the integer type is signed.
285    pub signed: bool,
286}
287
288/// Compile-time type information about floating-point types.
289#[derive(Debug)]
290#[non_exhaustive]
291#[unstable(feature = "type_info", issue = "146922")]
292pub struct Float {
293    /// The bit width of the floating-point type.
294    pub bits: u32,
295}
296
297/// Compile-time type information about string slice types.
298#[derive(Debug)]
299#[non_exhaustive]
300#[unstable(feature = "type_info", issue = "146922")]
301pub struct Str {
302    // No additional information to provide for now.
303}
304
305/// Compile-time type information about references.
306#[derive(Debug)]
307#[non_exhaustive]
308#[unstable(feature = "type_info", issue = "146922")]
309pub struct Reference {
310    /// The type of the value being referred to.
311    pub pointee: TypeId,
312    /// Whether this reference is mutable or not.
313    pub mutable: bool,
314}
315
316/// Compile-time type information about pointers.
317#[derive(Debug)]
318#[non_exhaustive]
319#[unstable(feature = "type_info", issue = "146922")]
320pub struct Pointer {
321    /// The type of the value being pointed to.
322    pub pointee: TypeId,
323    /// Whether this pointer is mutable or not.
324    pub mutable: bool,
325}
326
327#[derive(Debug)]
328#[unstable(feature = "type_info", issue = "146922")]
329/// Function pointer, e.g. fn(u8),
330pub struct FnPtr {
331    /// Unsafety, true is unsafe
332    pub unsafety: bool,
333
334    /// Abi, e.g. extern "C"
335    pub abi: Abi,
336
337    /// Function inputs
338    pub inputs: &'static [TypeId],
339
340    /// Function return type, default is TypeId::of::<()>
341    pub output: TypeId,
342
343    /// Vardiadic function, e.g. extern "C" fn add(n: usize, mut args: ...);
344    pub variadic: bool,
345}
346
347#[derive(Debug, Default)]
348#[non_exhaustive]
349#[unstable(feature = "type_info", issue = "146922")]
350/// Abi of [FnPtr]
351pub enum Abi {
352    /// Named abi, e.g. extern "custom", "stdcall" etc.
353    Named(&'static str),
354
355    /// Default
356    #[default]
357    ExternRust,
358
359    /// C-calling convention
360    ExternC,
361}
362
363impl TypeId {
364    /// Returns the size of the type represented by this `TypeId`. `None` if it is unsized.
365    ///
366    /// # Examples
367    ///
368    /// ```
369    /// #![feature(type_info)]
370    /// use std::any::TypeId;
371    ///
372    /// assert_eq!(const { TypeId::of::<u32>().size() }, Some(4));
373    /// assert_eq!(const { TypeId::of::<[u8; 16]>().size() }, Some(16));
374    /// ```
375    #[unstable(feature = "type_info", issue = "146922")]
376    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
377    pub const fn size(self) -> Option<usize> {
378        intrinsics::size_of_type_id(self)
379    }
380
381    /// Returns the number of variants of the type represented by this `TypeId`.
382    ///
383    /// For enums, this is the number of variants. For structs and unions, this is always 1.
384    ///
385    /// ```
386    /// #![feature(type_info)]
387    /// use std::any::TypeId;
388    ///
389    /// assert_eq!(const { TypeId::of::<Option<()>>().variants() }, 2);
390    ///
391    /// struct Unit;
392    /// struct Point {
393    ///     x: u32,
394    ///     y: u32,
395    /// }
396    /// assert_eq!(const { TypeId::of::<Unit>().variants() }, 1);
397    /// assert_eq!(const { TypeId::of::<Point>().variants() }, 1);
398    /// assert_eq!(const { TypeId::of::<(f32, f32)>().variants() }, 1);
399    /// ```
400    #[unstable(feature = "type_info", issue = "146922")]
401    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
402    pub const fn variants(self) -> usize {
403        intrinsics::type_id_variants(self)
404    }
405
406    /// Returns the number of fields at the given `variant_index` of the type represented by this `TypeId`.
407    ///
408    /// ```
409    /// #![feature(type_info)]
410    /// use std::any::TypeId;
411    ///
412    /// assert_eq!(const { TypeId::of::<u32>().fields(0) }, 0);
413    ///
414    /// struct Point {
415    ///     x: u32,
416    ///     y: u32,
417    /// }
418    /// assert_eq!(const { TypeId::of::<Point>().fields(0) }, 2);
419    ///
420    /// enum Enum {
421    ///     Unit,
422    ///     Tuple(u32, u64),
423    ///     Struct { x: u32, y: u32, z: String },
424    /// }
425    /// assert_eq!(const { TypeId::of::<Enum>().fields(0) }, 0);
426    /// assert_eq!(const { TypeId::of::<Enum>().fields(1) }, 2);
427    /// assert_eq!(const { TypeId::of::<Enum>().fields(2) }, 3);
428    /// ```
429    ///
430    /// The variant index refers to the source order index of a variant in a type.
431    ///
432    /// For enums, these are always `0..variant_count`, regardless of any custom discriminants that may have been defined.
433    /// `struct`s, `tuples`, and `unions`s are considered to have a single variant with variant index zero.
434    ///
435    /// ```
436    /// enum Number {
437    ///     Seven = 7, // variant index == 0
438    ///     Six = 6,   // variant index == 1
439    /// }
440    /// ```
441    ///
442    /// Out-of-bounds indexing will be treated as a compile-time error.
443    ///
444    /// ```compile_fail,E0080
445    /// # #![feature(type_info)]
446    /// # use std::any::TypeId;
447    /// #
448    /// # struct Point {
449    /// #     x: u32,
450    /// #     y: u32,
451    /// # }
452    /// # enum Enum {
453    /// #     Unit,
454    /// #     Tuple(u32, u64),
455    /// #     Struct { x: u32, y: u32, z: String },
456    /// # }
457    /// const {
458    ///     _ = TypeId::of::<Point>().fields(10); // error: indexing out of bounds: the len is 2 but the index is 10
459    ///     _ = TypeId::of::<Enum>().fields(10); // error: indexing out of bounds: the len is 3 but the index is 10
460    /// }
461    /// ```
462    #[unstable(feature = "type_info", issue = "146922")]
463    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
464    pub const fn fields(self, variant_index: usize) -> usize {
465        intrinsics::type_id_fields(self, variant_index)
466    }
467
468    /// Returns the field representing type at the given index of the type represented by this `TypeId`.
469    ///
470    /// ```
471    /// #![feature(type_info)]
472    /// use std::any::TypeId;
473    ///
474    /// struct Point {
475    ///     x: u32,
476    ///     y: u32,
477    /// }
478    /// assert_eq!(const { TypeId::of::<Point>().field(0, 0).type_id() }, TypeId::of::<u32>());
479    /// assert_eq!(const { TypeId::of::<Point>().field(0, 1).type_id() }, TypeId::of::<u32>());
480    ///
481    /// enum Enum {
482    ///     Unit,
483    ///     Tuple(u32, u64),
484    ///     Struct { x: u32, y: u32, z: String },
485    /// }
486    /// assert_eq!(const { TypeId::of::<Enum>().field(1, 0).type_id() }, TypeId::of::<u32>());
487    /// assert_eq!(const { TypeId::of::<Enum>().field(2, 2).type_id() }, TypeId::of::<String>());
488    /// ```
489    ///
490    /// The variant index and field index refer to the source order index of a variant in a type and
491    /// the source order index of a field in a variant, respectively.
492    ///
493    /// For enums, variant indexes are always `0..variant_count`, regardless of any custom discriminants that may have been defined.
494    /// `struct`s, `tuples`, and `unions`s are considered to have a single variant with variant index zero.
495    ///
496    /// As for field indexes, they may not be the same as the layout order for `repr(Rust)` types, but they are for `repr(C)` types.
497    ///
498    /// ```
499    /// enum Enum {
500    ///     Foo,  // variant index == 0
501    ///     Bar { // variant index == 1
502    ///         a: (), // field index == 0 in `Bar`
503    ///         b: (), // field index == 1 in `Bar`
504    ///     }
505    /// }
506    /// ```
507    ///
508    /// Out-of-bounds indexing will be treated as a compile-time error.
509    ///
510    /// ```compile_fail,E0080
511    /// # #![feature(type_info)]
512    /// # use std::any::TypeId;
513    /// #
514    /// # struct Point {
515    /// #     x: u32,
516    /// #     y: u32,
517    /// # }
518    /// # enum Enum {
519    /// #     Unit,
520    /// #     Tuple(u32, u64),
521    /// #     Struct { x: u32, y: u32, z: String },
522    /// # }
523    /// const {
524    ///     _ = TypeId::of::<Point>().field(0, 10); // error: indexing out of bounds: the len is 2 but the index is 10
525    ///     _ = TypeId::of::<Enum>().field(2, 10); // error: indexing out of bounds: the len is 3 but the index is 10
526    /// }
527    /// ```
528    #[unstable(feature = "type_info", issue = "146922")]
529    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
530    pub const fn field(self, variant_index: usize, field_index: usize) -> FieldId {
531        FieldId {
532            frt_type_id: intrinsics::type_id_field_representing_type(
533                self,
534                variant_index,
535                field_index,
536            ),
537        }
538    }
539}
540
541/// Field representing type ID. Representing a field of a struct, tuple or enum variant.
542#[derive(Copy, PartialOrd, Ord, Hash)]
543#[derive_const(Clone, PartialEq, Eq)]
544#[unstable(feature = "type_info", issue = "146922")]
545pub struct FieldId {
546    frt_type_id: TypeId,
547}
548
549#[unstable(feature = "type_info", issue = "146922")]
550impl fmt::Debug for FieldId {
551    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
552        write!(f, "FieldId({:#034x})", self.frt_type_id.as_u128())
553    }
554}
555
556impl FieldId {
557    /// Returns the `TypeId` of the actual field type.
558    ///
559    /// ```
560    /// #![feature(type_info)]
561    /// use std::any::TypeId;
562    ///
563    /// struct Point {
564    ///     x: u32,
565    ///     y: u32,
566    /// }
567    /// assert_eq!(
568    ///     const { TypeId::of::<Point>().field(0, 0).type_id() },
569    ///     TypeId::of::<u32>()
570    /// );
571    /// ```
572    #[unstable(feature = "type_info", issue = "146922")]
573    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
574    pub const fn type_id(self) -> TypeId {
575        intrinsics::field_representing_type_actual_type_id(self.frt_type_id)
576    }
577}