1use crate::any::TypeId;
5use crate::intrinsics::{type_id, type_of};
6use crate::marker::PointeeSized;
7use crate::ptr::DynMetadata;
8
9#[derive(Debug)]
11#[non_exhaustive]
12#[lang = "type_info"]
13#[unstable(feature = "type_info", issue = "146922")]
14pub struct Type {
15 pub kind: TypeKind,
17 pub size: Option<usize>,
19}
20
21#[derive(Debug, PartialEq, Eq)]
23#[unstable(feature = "type_info", issue = "146922")]
24#[non_exhaustive]
25pub struct TraitImpl<T: PointeeSized> {
26 pub(crate) vtable: DynMetadata<T>,
27}
28
29impl<T: PointeeSized> TraitImpl<T> {
30 pub const fn get_vtable(&self) -> DynMetadata<T> {
32 self.vtable
33 }
34}
35
36impl TypeId {
37 #[unstable(feature = "type_info", issue = "146922")]
40 #[rustc_const_unstable(feature = "type_info", issue = "146922")]
41 pub const fn info(self) -> Type {
42 type_of(self)
43 }
44}
45
46impl Type {
47 #[unstable(feature = "type_info", issue = "146922")]
56 #[rustc_const_unstable(feature = "type_info", issue = "146922")]
57 pub const fn of<T: ?Sized>() -> Self {
58 const { type_id::<T>().info() }
59 }
60}
61
62#[derive(Debug)]
64#[non_exhaustive]
65#[unstable(feature = "type_info", issue = "146922")]
66pub enum TypeKind {
67 Tuple(Tuple),
69 Array(Array),
71 Slice(Slice),
73 DynTrait(DynTrait),
75 Struct(Struct),
77 Enum(Enum),
79 Union(Union),
81 Bool(Bool),
83 Char(Char),
85 Int(Int),
87 Float(Float),
89 Str(Str),
91 Reference(Reference),
93 Pointer(Pointer),
95 FnPtr(FnPtr),
97 Other,
99}
100
101#[derive(Debug)]
103#[non_exhaustive]
104#[unstable(feature = "type_info", issue = "146922")]
105pub struct Tuple {
106 pub fields: &'static [Field],
108}
109
110#[derive(Debug)]
112#[non_exhaustive]
113#[unstable(feature = "type_info", issue = "146922")]
114pub struct Field {
115 pub name: &'static str,
117 pub ty: TypeId,
119 pub offset: usize,
121}
122
123#[derive(Debug)]
125#[non_exhaustive]
126#[unstable(feature = "type_info", issue = "146922")]
127pub struct Array {
128 pub element_ty: TypeId,
130 pub len: usize,
132}
133
134#[derive(Debug)]
136#[non_exhaustive]
137#[unstable(feature = "type_info", issue = "146922")]
138pub struct Slice {
139 pub element_ty: TypeId,
141}
142
143#[derive(Debug)]
146#[non_exhaustive]
147#[unstable(feature = "type_info", issue = "146922")]
148pub struct DynTrait {
149 pub predicates: &'static [DynTraitPredicate],
151}
152
153#[derive(Debug)]
155#[non_exhaustive]
156#[unstable(feature = "type_info", issue = "146922")]
157pub struct DynTraitPredicate {
158 pub trait_ty: Trait,
160}
161
162#[derive(Debug)]
164#[non_exhaustive]
165#[unstable(feature = "type_info", issue = "146922")]
166pub struct Trait {
167 pub ty: TypeId,
169 pub is_auto: bool,
171}
172
173#[derive(Debug)]
175#[non_exhaustive]
176#[unstable(feature = "type_info", issue = "146922")]
177pub struct Struct {
178 pub generics: &'static [Generic],
180 pub fields: &'static [Field],
182 pub non_exhaustive: bool,
184}
185
186#[derive(Debug)]
188#[non_exhaustive]
189#[unstable(feature = "type_info", issue = "146922")]
190pub struct Union {
191 pub generics: &'static [Generic],
193 pub fields: &'static [Field],
195}
196
197#[derive(Debug)]
199#[non_exhaustive]
200#[unstable(feature = "type_info", issue = "146922")]
201pub struct Enum {
202 pub generics: &'static [Generic],
204 pub variants: &'static [Variant],
206 pub non_exhaustive: bool,
208}
209
210#[derive(Debug)]
212#[non_exhaustive]
213#[unstable(feature = "type_info", issue = "146922")]
214pub struct Variant {
215 pub name: &'static str,
217 pub fields: &'static [Field],
219 pub non_exhaustive: bool,
221}
222
223#[derive(Debug)]
225#[non_exhaustive]
226#[unstable(feature = "type_info", issue = "146922")]
227pub enum Generic {
228 Lifetime(Lifetime),
230 Type(GenericType),
232 Const(Const),
234}
235
236#[derive(Debug)]
238#[non_exhaustive]
239#[unstable(feature = "type_info", issue = "146922")]
240pub struct Lifetime {
241 }
243
244#[derive(Debug)]
246#[non_exhaustive]
247#[unstable(feature = "type_info", issue = "146922")]
248pub struct GenericType {
249 pub ty: TypeId,
251}
252
253#[derive(Debug)]
255#[non_exhaustive]
256#[unstable(feature = "type_info", issue = "146922")]
257pub struct Const {
258 pub ty: TypeId,
260}
261
262#[derive(Debug)]
264#[non_exhaustive]
265#[unstable(feature = "type_info", issue = "146922")]
266pub struct Bool {
267 }
269
270#[derive(Debug)]
272#[non_exhaustive]
273#[unstable(feature = "type_info", issue = "146922")]
274pub struct Char {
275 }
277
278#[derive(Debug)]
280#[non_exhaustive]
281#[unstable(feature = "type_info", issue = "146922")]
282pub struct Int {
283 pub bits: u32,
285 pub signed: bool,
287}
288
289#[derive(Debug)]
291#[non_exhaustive]
292#[unstable(feature = "type_info", issue = "146922")]
293pub struct Float {
294 pub bits: u32,
296}
297
298#[derive(Debug)]
300#[non_exhaustive]
301#[unstable(feature = "type_info", issue = "146922")]
302pub struct Str {
303 }
305
306#[derive(Debug)]
308#[non_exhaustive]
309#[unstable(feature = "type_info", issue = "146922")]
310pub struct Reference {
311 pub pointee: TypeId,
313 pub mutable: bool,
315}
316
317#[derive(Debug)]
319#[non_exhaustive]
320#[unstable(feature = "type_info", issue = "146922")]
321pub struct Pointer {
322 pub pointee: TypeId,
324 pub mutable: bool,
326}
327
328#[derive(Debug)]
329#[unstable(feature = "type_info", issue = "146922")]
330pub struct FnPtr {
332 pub unsafety: bool,
334
335 pub abi: Abi,
337
338 pub inputs: &'static [TypeId],
340
341 pub output: TypeId,
343
344 pub variadic: bool,
346}
347
348#[derive(Debug, Default)]
349#[non_exhaustive]
350#[unstable(feature = "type_info", issue = "146922")]
351pub enum Abi {
353 Named(&'static str),
355
356 #[default]
358 ExternRust,
359
360 ExternC,
362}