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::intrinsics::type_of;
6
7/// Compile-time type information.
8#[derive(Debug)]
9#[non_exhaustive]
10#[lang = "type_info"]
11#[unstable(feature = "type_info", issue = "146922")]
12pub struct Type {
13    /// Per-type information
14    pub kind: TypeKind,
15    /// Size of the type. `None` if it is unsized
16    pub size: Option<usize>,
17}
18
19impl TypeId {
20    /// Compute the type information of a concrete type.
21    /// It can only be called at compile time.
22    #[unstable(feature = "type_info", issue = "146922")]
23    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
24    pub const fn info(self) -> Type {
25        type_of(self)
26    }
27}
28
29impl Type {
30    /// Returns the type information of the generic type parameter.
31    #[unstable(feature = "type_info", issue = "146922")]
32    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
33    // FIXME(reflection): don't require the 'static bound
34    pub const fn of<T: ?Sized + 'static>() -> Self {
35        const { TypeId::of::<T>().info() }
36    }
37}
38
39/// Compile-time type information.
40#[derive(Debug)]
41#[non_exhaustive]
42#[unstable(feature = "type_info", issue = "146922")]
43pub enum TypeKind {
44    /// Tuples.
45    Tuple(Tuple),
46    /// Arrays.
47    Array(Array),
48    /// Slices.
49    Slice(Slice),
50    /// Dynamic Traits.
51    DynTrait(DynTrait),
52    /// Primitive boolean type.
53    Bool(Bool),
54    /// Primitive character type.
55    Char(Char),
56    /// Primitive signed and unsigned integer type.
57    Int(Int),
58    /// Primitive floating-point type.
59    Float(Float),
60    /// String slice type.
61    Str(Str),
62    /// References.
63    Reference(Reference),
64    /// Pointers.
65    Pointer(Pointer),
66    /// FIXME(#146922): add all the common types
67    Other,
68}
69
70/// Compile-time type information about tuples.
71#[derive(Debug)]
72#[non_exhaustive]
73#[unstable(feature = "type_info", issue = "146922")]
74pub struct Tuple {
75    /// All fields of a tuple.
76    pub fields: &'static [Field],
77}
78
79/// Compile-time type information about fields of tuples, structs and enum variants.
80#[derive(Debug)]
81#[non_exhaustive]
82#[unstable(feature = "type_info", issue = "146922")]
83pub struct Field {
84    /// The field's type.
85    pub ty: TypeId,
86    /// Offset in bytes from the parent type
87    pub offset: usize,
88}
89
90/// Compile-time type information about arrays.
91#[derive(Debug)]
92#[non_exhaustive]
93#[unstable(feature = "type_info", issue = "146922")]
94pub struct Array {
95    /// The type of each element in the array.
96    pub element_ty: TypeId,
97    /// The length of the array.
98    pub len: usize,
99}
100
101/// Compile-time type information about slices.
102#[derive(Debug)]
103#[non_exhaustive]
104#[unstable(feature = "type_info", issue = "146922")]
105pub struct Slice {
106    /// The type of each element in the slice.
107    pub element_ty: TypeId,
108}
109
110/// Compile-time type information about dynamic traits.
111/// FIXME(#146922): Add super traits and generics
112#[derive(Debug)]
113#[non_exhaustive]
114#[unstable(feature = "type_info", issue = "146922")]
115pub struct DynTrait {
116    /// The predicates of  a dynamic trait.
117    pub predicates: &'static [DynTraitPredicate],
118}
119
120/// Compile-time type information about a dynamic trait predicate.
121#[derive(Debug)]
122#[non_exhaustive]
123#[unstable(feature = "type_info", issue = "146922")]
124pub struct DynTraitPredicate {
125    /// The type of the trait as a dynamic trait type.
126    pub trait_ty: Trait,
127}
128
129/// Compile-time type information about a trait.
130#[derive(Debug)]
131#[non_exhaustive]
132#[unstable(feature = "type_info", issue = "146922")]
133pub struct Trait {
134    /// The TypeId of the trait as a dynamic type
135    pub ty: TypeId,
136    /// Whether the trait is an auto trait
137    pub is_auto: bool,
138}
139
140/// Compile-time type information about `bool`.
141#[derive(Debug)]
142#[non_exhaustive]
143#[unstable(feature = "type_info", issue = "146922")]
144pub struct Bool {
145    // No additional information to provide for now.
146}
147
148/// Compile-time type information about `char`.
149#[derive(Debug)]
150#[non_exhaustive]
151#[unstable(feature = "type_info", issue = "146922")]
152pub struct Char {
153    // No additional information to provide for now.
154}
155
156/// Compile-time type information about signed and unsigned integer types.
157#[derive(Debug)]
158#[non_exhaustive]
159#[unstable(feature = "type_info", issue = "146922")]
160pub struct Int {
161    /// The bit width of the signed integer type.
162    pub bits: u32,
163    /// Whether the integer type is signed.
164    pub signed: bool,
165}
166
167/// Compile-time type information about floating-point types.
168#[derive(Debug)]
169#[non_exhaustive]
170#[unstable(feature = "type_info", issue = "146922")]
171pub struct Float {
172    /// The bit width of the floating-point type.
173    pub bits: u32,
174}
175
176/// Compile-time type information about string slice types.
177#[derive(Debug)]
178#[non_exhaustive]
179#[unstable(feature = "type_info", issue = "146922")]
180pub struct Str {
181    // No additional information to provide for now.
182}
183
184/// Compile-time type information about references.
185#[derive(Debug)]
186#[non_exhaustive]
187#[unstable(feature = "type_info", issue = "146922")]
188pub struct Reference {
189    /// The type of the value being referred to.
190    pub pointee: TypeId,
191    /// Whether this reference is mutable or not.
192    pub mutable: bool,
193}
194
195/// Compile-time type information about pointers.
196#[derive(Debug)]
197#[non_exhaustive]
198#[unstable(feature = "type_info", issue = "146922")]
199pub struct Pointer {
200    /// The type of the value being pointed to.
201    pub pointee: TypeId,
202    /// Whether this pointer is mutable or not.
203    pub mutable: bool,
204}