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