1use crate::any::TypeId;
5use crate::intrinsics::{self, 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}
18
19#[derive(Debug, PartialEq, Eq)]
21#[unstable(feature = "type_info", issue = "146922")]
22#[non_exhaustive]
23pub struct TraitImpl<T: PointeeSized> {
24 pub(crate) vtable: DynMetadata<T>,
25}
26
27impl<T: PointeeSized> TraitImpl<T> {
28 pub const fn get_vtable(&self) -> DynMetadata<T> {
30 self.vtable
31 }
32}
33
34impl TypeId {
35 #[unstable(feature = "type_info", issue = "146922")]
38 #[rustc_const_unstable(feature = "type_info", issue = "146922")]
39 pub const fn info(self) -> Type {
40 type_of(self)
41 }
42}
43
44impl Type {
45 #[unstable(feature = "type_info", issue = "146922")]
54 #[rustc_const_unstable(feature = "type_info", issue = "146922")]
55 pub const fn of<T: ?Sized>() -> Self {
56 const { type_id::<T>().info() }
57 }
58}
59
60#[derive(Debug)]
62#[non_exhaustive]
63#[unstable(feature = "type_info", issue = "146922")]
64pub enum TypeKind {
65 Tuple(Tuple),
67 Array(Array),
69 Slice(Slice),
71 DynTrait(DynTrait),
73 Struct(Struct),
75 Enum(Enum),
77 Union(Union),
79 Bool(Bool),
81 Char(Char),
83 Int(Int),
85 Float(Float),
87 Str(Str),
89 Reference(Reference),
91 Pointer(Pointer),
93 FnPtr(FnPtr),
95 Other,
97}
98
99#[derive(Debug)]
101#[non_exhaustive]
102#[unstable(feature = "type_info", issue = "146922")]
103pub struct Tuple {
104 pub fields: &'static [Field],
106}
107
108#[derive(Debug)]
110#[non_exhaustive]
111#[unstable(feature = "type_info", issue = "146922")]
112pub struct Field {
113 pub name: &'static str,
115 pub ty: TypeId,
117 pub offset: usize,
119}
120
121#[derive(Debug)]
123#[non_exhaustive]
124#[unstable(feature = "type_info", issue = "146922")]
125pub struct Array {
126 pub element_ty: TypeId,
128 pub len: usize,
130}
131
132#[derive(Debug)]
134#[non_exhaustive]
135#[unstable(feature = "type_info", issue = "146922")]
136pub struct Slice {
137 pub element_ty: TypeId,
139}
140
141#[derive(Debug)]
144#[non_exhaustive]
145#[unstable(feature = "type_info", issue = "146922")]
146pub struct DynTrait {
147 pub predicates: &'static [DynTraitPredicate],
149}
150
151#[derive(Debug)]
153#[non_exhaustive]
154#[unstable(feature = "type_info", issue = "146922")]
155pub struct DynTraitPredicate {
156 pub trait_ty: Trait,
158}
159
160#[derive(Debug)]
162#[non_exhaustive]
163#[unstable(feature = "type_info", issue = "146922")]
164pub struct Trait {
165 pub ty: TypeId,
167 pub is_auto: bool,
169}
170
171#[derive(Debug)]
173#[non_exhaustive]
174#[unstable(feature = "type_info", issue = "146922")]
175pub struct Struct {
176 pub generics: &'static [Generic],
178 pub fields: &'static [Field],
180 pub non_exhaustive: bool,
182}
183
184#[derive(Debug)]
186#[non_exhaustive]
187#[unstable(feature = "type_info", issue = "146922")]
188pub struct Union {
189 pub generics: &'static [Generic],
191 pub fields: &'static [Field],
193}
194
195#[derive(Debug)]
197#[non_exhaustive]
198#[unstable(feature = "type_info", issue = "146922")]
199pub struct Enum {
200 pub generics: &'static [Generic],
202 pub variants: &'static [Variant],
204 pub non_exhaustive: bool,
206}
207
208#[derive(Debug)]
210#[non_exhaustive]
211#[unstable(feature = "type_info", issue = "146922")]
212pub struct Variant {
213 pub name: &'static str,
215 pub fields: &'static [Field],
217 pub non_exhaustive: bool,
219}
220
221#[derive(Debug)]
223#[non_exhaustive]
224#[unstable(feature = "type_info", issue = "146922")]
225pub enum Generic {
226 Lifetime(Lifetime),
228 Type(GenericType),
230 Const(Const),
232}
233
234#[derive(Debug)]
236#[non_exhaustive]
237#[unstable(feature = "type_info", issue = "146922")]
238pub struct Lifetime {
239 }
241
242#[derive(Debug)]
244#[non_exhaustive]
245#[unstable(feature = "type_info", issue = "146922")]
246pub struct GenericType {
247 pub ty: TypeId,
249}
250
251#[derive(Debug)]
253#[non_exhaustive]
254#[unstable(feature = "type_info", issue = "146922")]
255pub struct Const {
256 pub ty: TypeId,
258}
259
260#[derive(Debug)]
262#[non_exhaustive]
263#[unstable(feature = "type_info", issue = "146922")]
264pub struct Bool {
265 }
267
268#[derive(Debug)]
270#[non_exhaustive]
271#[unstable(feature = "type_info", issue = "146922")]
272pub struct Char {
273 }
275
276#[derive(Debug)]
278#[non_exhaustive]
279#[unstable(feature = "type_info", issue = "146922")]
280pub struct Int {
281 pub bits: u32,
283 pub signed: bool,
285}
286
287#[derive(Debug)]
289#[non_exhaustive]
290#[unstable(feature = "type_info", issue = "146922")]
291pub struct Float {
292 pub bits: u32,
294}
295
296#[derive(Debug)]
298#[non_exhaustive]
299#[unstable(feature = "type_info", issue = "146922")]
300pub struct Str {
301 }
303
304#[derive(Debug)]
306#[non_exhaustive]
307#[unstable(feature = "type_info", issue = "146922")]
308pub struct Reference {
309 pub pointee: TypeId,
311 pub mutable: bool,
313}
314
315#[derive(Debug)]
317#[non_exhaustive]
318#[unstable(feature = "type_info", issue = "146922")]
319pub struct Pointer {
320 pub pointee: TypeId,
322 pub mutable: bool,
324}
325
326#[derive(Debug)]
327#[unstable(feature = "type_info", issue = "146922")]
328pub struct FnPtr {
330 pub unsafety: bool,
332
333 pub abi: Abi,
335
336 pub inputs: &'static [TypeId],
338
339 pub output: TypeId,
341
342 pub variadic: bool,
344}
345
346#[derive(Debug, Default)]
347#[non_exhaustive]
348#[unstable(feature = "type_info", issue = "146922")]
349pub enum Abi {
351 Named(&'static str),
353
354 #[default]
356 ExternRust,
357
358 ExternC,
360}
361
362impl TypeId {
363 #[unstable(feature = "type_info", issue = "146922")]
375 #[rustc_const_unstable(feature = "type_info", issue = "146922")]
376 pub const fn size(self) -> Option<usize> {
377 intrinsics::size_of_type_id(self)
378 }
379}