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    /// Primitive boolean type.
49    Bool(Bool),
50    /// Primitive character type.
51    Char(Char),
52    /// Primitive signed and unsigned integer type.
53    Int(Int),
54    /// Primitive floating-point type.
55    Float(Float),
56    /// String slice type.
57    Str(Str),
58    /// References.
59    Reference(Reference),
60    /// Pointers.
61    Pointer(Pointer),
62    /// FIXME(#146922): add all the common types
63    Other,
64}
65
66/// Compile-time type information about tuples.
67#[derive(Debug)]
68#[non_exhaustive]
69#[unstable(feature = "type_info", issue = "146922")]
70pub struct Tuple {
71    /// All fields of a tuple.
72    pub fields: &'static [Field],
73}
74
75/// Compile-time type information about fields of tuples, structs and enum variants.
76#[derive(Debug)]
77#[non_exhaustive]
78#[unstable(feature = "type_info", issue = "146922")]
79pub struct Field {
80    /// The field's type.
81    pub ty: TypeId,
82    /// Offset in bytes from the parent type
83    pub offset: usize,
84}
85
86/// Compile-time type information about arrays.
87#[derive(Debug)]
88#[non_exhaustive]
89#[unstable(feature = "type_info", issue = "146922")]
90pub struct Array {
91    /// The type of each element in the array.
92    pub element_ty: TypeId,
93    /// The length of the array.
94    pub len: usize,
95}
96
97/// Compile-time type information about `bool`.
98#[derive(Debug)]
99#[non_exhaustive]
100#[unstable(feature = "type_info", issue = "146922")]
101pub struct Bool {
102    // No additional information to provide for now.
103}
104
105/// Compile-time type information about `char`.
106#[derive(Debug)]
107#[non_exhaustive]
108#[unstable(feature = "type_info", issue = "146922")]
109pub struct Char {
110    // No additional information to provide for now.
111}
112
113/// Compile-time type information about signed and unsigned integer types.
114#[derive(Debug)]
115#[non_exhaustive]
116#[unstable(feature = "type_info", issue = "146922")]
117pub struct Int {
118    /// The bit width of the signed integer type.
119    pub bits: u32,
120    /// Whether the integer type is signed.
121    pub signed: bool,
122}
123
124/// Compile-time type information about floating-point types.
125#[derive(Debug)]
126#[non_exhaustive]
127#[unstable(feature = "type_info", issue = "146922")]
128pub struct Float {
129    /// The bit width of the floating-point type.
130    pub bits: u32,
131}
132
133/// Compile-time type information about string slice types.
134#[derive(Debug)]
135#[non_exhaustive]
136#[unstable(feature = "type_info", issue = "146922")]
137pub struct Str {
138    // No additional information to provide for now.
139}
140
141/// Compile-time type information about references.
142#[derive(Debug)]
143#[non_exhaustive]
144#[unstable(feature = "type_info", issue = "146922")]
145pub struct Reference {
146    /// The type of the value being referred to.
147    pub pointee: TypeId,
148    /// Whether this reference is mutable or not.
149    pub mutable: bool,
150}
151
152/// Compile-time type information about pointers.
153#[derive(Debug)]
154#[non_exhaustive]
155#[unstable(feature = "type_info", issue = "146922")]
156pub struct Pointer {
157    /// The type of the value being pointed to.
158    pub pointee: TypeId,
159    /// Whether this pointer is mutable or not.
160    pub mutable: bool,
161}