Skip to main content

core/
any.rs

1//! Utilities for dynamic typing or type reflection.
2//!
3//! # `Any` and `TypeId`
4//!
5//! `Any` itself can be used to get a `TypeId`, and has more features when used
6//! as a trait object. As `&dyn Any` (a borrowed trait object), it has the `is`
7//! and `downcast_ref` methods, to test if the contained value is of a given type,
8//! and to get a reference to the inner value as a type. As `&mut dyn Any`, there
9//! is also the `downcast_mut` method, for getting a mutable reference to the
10//! inner value. `Box<dyn Any>` adds the `downcast` method, which attempts to
11//! convert to a `Box<T>`. See the [`Box`] documentation for the full details.
12//!
13//! Note that `&dyn Any` is limited to testing whether a value is of a specified
14//! concrete type, and cannot be used to test whether a type implements a trait.
15//!
16//! [`Box`]: ../../std/boxed/struct.Box.html
17//!
18//! # Smart pointers and `dyn Any`
19//!
20//! One piece of behavior to keep in mind when using `Any` as a trait object,
21//! especially with types like `Box<dyn Any>` or `Arc<dyn Any>`, is that simply
22//! calling `.type_id()` on the value will produce the `TypeId` of the
23//! *container*, not the underlying trait object. This can be avoided by
24//! converting the smart pointer into a `&dyn Any` instead, which will return
25//! the object's `TypeId`. For example:
26//!
27//! ```
28//! use std::any::{Any, TypeId};
29//!
30//! let boxed: Box<dyn Any> = Box::new(3_i32);
31//!
32//! // You're more likely to want this:
33//! let actual_id = (&*boxed).type_id();
34//! // ... than this:
35//! let boxed_id = boxed.type_id();
36//!
37//! assert_eq!(actual_id, TypeId::of::<i32>());
38//! assert_eq!(boxed_id, TypeId::of::<Box<dyn Any>>());
39//! ```
40//!
41//! ## Examples
42//!
43//! Consider a situation where we want to log a value passed to a function.
44//! We know the value we're working on implements `Debug`, but we don't know its
45//! concrete type. We want to give special treatment to certain types: in this
46//! case printing out the length of `String` values prior to their value.
47//! We don't know the concrete type of our value at compile time, so we need to
48//! use runtime reflection instead.
49//!
50//! ```rust
51//! use std::fmt::Debug;
52//! use std::any::Any;
53//!
54//! // Logger function for any type that implements `Debug`.
55//! fn log<T: Any + Debug>(value: &T) {
56//!     let value_any = value as &dyn Any;
57//!
58//!     // Try to convert our value to a `String`. If successful, we want to
59//!     // output the `String`'s length as well as its value. If not, it's a
60//!     // different type: just print it out unadorned.
61//!     match value_any.downcast_ref::<String>() {
62//!         Some(as_string) => {
63//!             println!("String ({}): {}", as_string.len(), as_string);
64//!         }
65//!         None => {
66//!             println!("{value:?}");
67//!         }
68//!     }
69//! }
70//!
71//! // This function wants to log its parameter out prior to doing work with it.
72//! fn do_work<T: Any + Debug>(value: &T) {
73//!     log(value);
74//!     // ...do some other work
75//! }
76//!
77//! fn main() {
78//!     let my_string = "Hello World".to_string();
79//!     do_work(&my_string);
80//!
81//!     let my_i8: i8 = 100;
82//!     do_work(&my_i8);
83//! }
84//! ```
85//!
86
87#![stable(feature = "rust1", since = "1.0.0")]
88
89use crate::intrinsics::{self, type_id_vtable};
90use crate::mem::transmute;
91use crate::mem::type_info::{TraitImpl, TypeKind};
92use crate::{fmt, hash, ptr};
93
94///////////////////////////////////////////////////////////////////////////////
95// Any trait
96///////////////////////////////////////////////////////////////////////////////
97
98/// A trait to emulate dynamic typing.
99///
100/// Most types implement `Any`. However, any type which contains a non-`'static` reference does not.
101/// See the [module-level documentation][mod] for more details.
102///
103/// [mod]: crate::any
104// This trait is not unsafe, though we rely on the specifics of it's sole impl's
105// `type_id` function in unsafe code (e.g., `downcast`). Normally, that would be
106// a problem, but because the only impl of `Any` is a blanket implementation, no
107// other code can implement `Any`.
108//
109// We could plausibly make this trait unsafe -- it would not cause breakage,
110// since we control all the implementations -- but we choose not to as that's
111// both not really necessary and may confuse users about the distinction of
112// unsafe traits and unsafe methods (i.e., `type_id` would still be safe to call,
113// but we would likely want to indicate as such in documentation).
114#[stable(feature = "rust1", since = "1.0.0")]
115#[rustc_diagnostic_item = "Any"]
116pub trait Any: 'static {
117    /// Gets the `TypeId` of `self`.
118    ///
119    /// If called on a `dyn Any` trait object
120    /// (or a trait object of a subtrait of `Any`),
121    /// this returns the `TypeId` of the underlying
122    /// concrete type, not that of `dyn Any` itself.
123    ///
124    /// # Examples
125    ///
126    /// ```
127    /// use std::any::{Any, TypeId};
128    ///
129    /// fn is_string(s: &dyn Any) -> bool {
130    ///     TypeId::of::<String>() == s.type_id()
131    /// }
132    ///
133    /// assert_eq!(is_string(&0), false);
134    /// assert_eq!(is_string(&"cookie monster".to_string()), true);
135    /// ```
136    #[stable(feature = "get_type_id", since = "1.34.0")]
137    fn type_id(&self) -> TypeId;
138}
139
140#[stable(feature = "rust1", since = "1.0.0")]
141impl<T: 'static + ?Sized> Any for T {
142    fn type_id(&self) -> TypeId {
143        TypeId::of::<T>()
144    }
145}
146
147///////////////////////////////////////////////////////////////////////////////
148// Extension methods for Any trait objects.
149///////////////////////////////////////////////////////////////////////////////
150
151#[stable(feature = "rust1", since = "1.0.0")]
152impl fmt::Debug for dyn Any {
153    #[ferrocene::prevalidated]
154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155        f.debug_struct("Any").finish_non_exhaustive()
156    }
157}
158
159// Ensure that the result of e.g., joining a thread can be printed and
160// hence used with `unwrap`. May eventually no longer be needed if
161// dispatch works with upcasting.
162#[stable(feature = "rust1", since = "1.0.0")]
163impl fmt::Debug for dyn Any + Send {
164    #[ferrocene::prevalidated]
165    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166        f.debug_struct("Any").finish_non_exhaustive()
167    }
168}
169
170#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
171impl fmt::Debug for dyn Any + Send + Sync {
172    #[ferrocene::prevalidated]
173    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174        f.debug_struct("Any").finish_non_exhaustive()
175    }
176}
177
178impl dyn Any {
179    /// Returns `true` if the inner type is the same as `T`.
180    ///
181    /// # Examples
182    ///
183    /// ```
184    /// use std::any::Any;
185    ///
186    /// fn is_string(s: &dyn Any) {
187    ///     if s.is::<String>() {
188    ///         println!("It's a string!");
189    ///     } else {
190    ///         println!("Not a string...");
191    ///     }
192    /// }
193    ///
194    /// is_string(&0);
195    /// is_string(&"cookie monster".to_string());
196    /// ```
197    #[stable(feature = "rust1", since = "1.0.0")]
198    #[inline]
199    pub fn is<T: Any>(&self) -> bool {
200        // Get `TypeId` of the type this function is instantiated with.
201        let t = TypeId::of::<T>();
202
203        // Get `TypeId` of the type in the trait object (`self`).
204        let concrete = self.type_id();
205
206        // Compare both `TypeId`s on equality.
207        t == concrete
208    }
209
210    /// Returns some reference to the inner value if it is of type `T`, or
211    /// `None` if it isn't.
212    ///
213    /// # Examples
214    ///
215    /// ```
216    /// use std::any::Any;
217    ///
218    /// fn print_if_string(s: &dyn Any) {
219    ///     if let Some(string) = s.downcast_ref::<String>() {
220    ///         println!("It's a string({}): '{}'", string.len(), string);
221    ///     } else {
222    ///         println!("Not a string...");
223    ///     }
224    /// }
225    ///
226    /// print_if_string(&0);
227    /// print_if_string(&"cookie monster".to_string());
228    /// ```
229    #[stable(feature = "rust1", since = "1.0.0")]
230    #[inline]
231    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
232        if self.is::<T>() {
233            // SAFETY: just checked whether we are pointing to the correct type, and we can rely on
234            // that check for memory safety because we have implemented Any for all types; no other
235            // impls can exist as they would conflict with our impl.
236            unsafe { Some(self.downcast_unchecked_ref()) }
237        } else {
238            None
239        }
240    }
241
242    /// Returns some mutable reference to the inner value if it is of type `T`, or
243    /// `None` if it isn't.
244    ///
245    /// # Examples
246    ///
247    /// ```
248    /// use std::any::Any;
249    ///
250    /// fn modify_if_u32(s: &mut dyn Any) {
251    ///     if let Some(num) = s.downcast_mut::<u32>() {
252    ///         *num = 42;
253    ///     }
254    /// }
255    ///
256    /// let mut x = 10u32;
257    /// let mut s = "starlord".to_string();
258    ///
259    /// modify_if_u32(&mut x);
260    /// modify_if_u32(&mut s);
261    ///
262    /// assert_eq!(x, 42);
263    /// assert_eq!(&s, "starlord");
264    /// ```
265    #[stable(feature = "rust1", since = "1.0.0")]
266    #[inline]
267    pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
268        if self.is::<T>() {
269            // SAFETY: just checked whether we are pointing to the correct type, and we can rely on
270            // that check for memory safety because we have implemented Any for all types; no other
271            // impls can exist as they would conflict with our impl.
272            unsafe { Some(self.downcast_unchecked_mut()) }
273        } else {
274            None
275        }
276    }
277
278    /// Returns a reference to the inner value as type `dyn T`.
279    ///
280    /// # Examples
281    ///
282    /// ```
283    /// #![feature(downcast_unchecked)]
284    ///
285    /// use std::any::Any;
286    ///
287    /// let x: Box<dyn Any> = Box::new(1_usize);
288    ///
289    /// unsafe {
290    ///     assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1);
291    /// }
292    /// ```
293    ///
294    /// # Safety
295    ///
296    /// The contained value must be of type `T`. Calling this method
297    /// with the incorrect type is *undefined behavior*.
298    #[unstable(feature = "downcast_unchecked", issue = "90850")]
299    #[inline]
300    pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T {
301        debug_assert!(self.is::<T>());
302        // SAFETY: caller guarantees that T is the correct type
303        unsafe { &*(self as *const dyn Any as *const T) }
304    }
305
306    /// Returns a mutable reference to the inner value as type `dyn T`.
307    ///
308    /// # Examples
309    ///
310    /// ```
311    /// #![feature(downcast_unchecked)]
312    ///
313    /// use std::any::Any;
314    ///
315    /// let mut x: Box<dyn Any> = Box::new(1_usize);
316    ///
317    /// unsafe {
318    ///     *x.downcast_unchecked_mut::<usize>() += 1;
319    /// }
320    ///
321    /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
322    /// ```
323    ///
324    /// # Safety
325    ///
326    /// The contained value must be of type `T`. Calling this method
327    /// with the incorrect type is *undefined behavior*.
328    #[unstable(feature = "downcast_unchecked", issue = "90850")]
329    #[inline]
330    pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T {
331        debug_assert!(self.is::<T>());
332        // SAFETY: caller guarantees that T is the correct type
333        unsafe { &mut *(self as *mut dyn Any as *mut T) }
334    }
335}
336
337impl dyn Any + Send {
338    /// Forwards to the method defined on the type `dyn Any`.
339    ///
340    /// # Examples
341    ///
342    /// ```
343    /// use std::any::Any;
344    ///
345    /// fn is_string(s: &(dyn Any + Send)) {
346    ///     if s.is::<String>() {
347    ///         println!("It's a string!");
348    ///     } else {
349    ///         println!("Not a string...");
350    ///     }
351    /// }
352    ///
353    /// is_string(&0);
354    /// is_string(&"cookie monster".to_string());
355    /// ```
356    #[stable(feature = "rust1", since = "1.0.0")]
357    #[inline]
358    pub fn is<T: Any>(&self) -> bool {
359        <dyn Any>::is::<T>(self)
360    }
361
362    /// Forwards to the method defined on the type `dyn Any`.
363    ///
364    /// # Examples
365    ///
366    /// ```
367    /// use std::any::Any;
368    ///
369    /// fn print_if_string(s: &(dyn Any + Send)) {
370    ///     if let Some(string) = s.downcast_ref::<String>() {
371    ///         println!("It's a string({}): '{}'", string.len(), string);
372    ///     } else {
373    ///         println!("Not a string...");
374    ///     }
375    /// }
376    ///
377    /// print_if_string(&0);
378    /// print_if_string(&"cookie monster".to_string());
379    /// ```
380    #[stable(feature = "rust1", since = "1.0.0")]
381    #[inline]
382    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
383        <dyn Any>::downcast_ref::<T>(self)
384    }
385
386    /// Forwards to the method defined on the type `dyn Any`.
387    ///
388    /// # Examples
389    ///
390    /// ```
391    /// use std::any::Any;
392    ///
393    /// fn modify_if_u32(s: &mut (dyn Any + Send)) {
394    ///     if let Some(num) = s.downcast_mut::<u32>() {
395    ///         *num = 42;
396    ///     }
397    /// }
398    ///
399    /// let mut x = 10u32;
400    /// let mut s = "starlord".to_string();
401    ///
402    /// modify_if_u32(&mut x);
403    /// modify_if_u32(&mut s);
404    ///
405    /// assert_eq!(x, 42);
406    /// assert_eq!(&s, "starlord");
407    /// ```
408    #[stable(feature = "rust1", since = "1.0.0")]
409    #[inline]
410    pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
411        <dyn Any>::downcast_mut::<T>(self)
412    }
413
414    /// Forwards to the method defined on the type `dyn Any`.
415    ///
416    /// # Examples
417    ///
418    /// ```
419    /// #![feature(downcast_unchecked)]
420    ///
421    /// use std::any::Any;
422    ///
423    /// let x: Box<dyn Any> = Box::new(1_usize);
424    ///
425    /// unsafe {
426    ///     assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1);
427    /// }
428    /// ```
429    ///
430    /// # Safety
431    ///
432    /// The contained value must be of type `T`. Calling this method
433    /// with the incorrect type is *undefined behavior*.
434    #[unstable(feature = "downcast_unchecked", issue = "90850")]
435    #[inline]
436    pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T {
437        // SAFETY: guaranteed by caller
438        unsafe { <dyn Any>::downcast_unchecked_ref::<T>(self) }
439    }
440
441    /// Forwards to the method defined on the type `dyn Any`.
442    ///
443    /// # Examples
444    ///
445    /// ```
446    /// #![feature(downcast_unchecked)]
447    ///
448    /// use std::any::Any;
449    ///
450    /// let mut x: Box<dyn Any> = Box::new(1_usize);
451    ///
452    /// unsafe {
453    ///     *x.downcast_unchecked_mut::<usize>() += 1;
454    /// }
455    ///
456    /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
457    /// ```
458    ///
459    /// # Safety
460    ///
461    /// The contained value must be of type `T`. Calling this method
462    /// with the incorrect type is *undefined behavior*.
463    #[unstable(feature = "downcast_unchecked", issue = "90850")]
464    #[inline]
465    pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T {
466        // SAFETY: guaranteed by caller
467        unsafe { <dyn Any>::downcast_unchecked_mut::<T>(self) }
468    }
469}
470
471impl dyn Any + Send + Sync {
472    /// Forwards to the method defined on the type `Any`.
473    ///
474    /// # Examples
475    ///
476    /// ```
477    /// use std::any::Any;
478    ///
479    /// fn is_string(s: &(dyn Any + Send + Sync)) {
480    ///     if s.is::<String>() {
481    ///         println!("It's a string!");
482    ///     } else {
483    ///         println!("Not a string...");
484    ///     }
485    /// }
486    ///
487    /// is_string(&0);
488    /// is_string(&"cookie monster".to_string());
489    /// ```
490    #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
491    #[inline]
492    pub fn is<T: Any>(&self) -> bool {
493        <dyn Any>::is::<T>(self)
494    }
495
496    /// Forwards to the method defined on the type `Any`.
497    ///
498    /// # Examples
499    ///
500    /// ```
501    /// use std::any::Any;
502    ///
503    /// fn print_if_string(s: &(dyn Any + Send + Sync)) {
504    ///     if let Some(string) = s.downcast_ref::<String>() {
505    ///         println!("It's a string({}): '{}'", string.len(), string);
506    ///     } else {
507    ///         println!("Not a string...");
508    ///     }
509    /// }
510    ///
511    /// print_if_string(&0);
512    /// print_if_string(&"cookie monster".to_string());
513    /// ```
514    #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
515    #[inline]
516    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
517        <dyn Any>::downcast_ref::<T>(self)
518    }
519
520    /// Forwards to the method defined on the type `Any`.
521    ///
522    /// # Examples
523    ///
524    /// ```
525    /// use std::any::Any;
526    ///
527    /// fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) {
528    ///     if let Some(num) = s.downcast_mut::<u32>() {
529    ///         *num = 42;
530    ///     }
531    /// }
532    ///
533    /// let mut x = 10u32;
534    /// let mut s = "starlord".to_string();
535    ///
536    /// modify_if_u32(&mut x);
537    /// modify_if_u32(&mut s);
538    ///
539    /// assert_eq!(x, 42);
540    /// assert_eq!(&s, "starlord");
541    /// ```
542    #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
543    #[inline]
544    pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
545        <dyn Any>::downcast_mut::<T>(self)
546    }
547
548    /// Forwards to the method defined on the type `Any`.
549    ///
550    /// # Examples
551    ///
552    /// ```
553    /// #![feature(downcast_unchecked)]
554    ///
555    /// use std::any::Any;
556    ///
557    /// let x: Box<dyn Any> = Box::new(1_usize);
558    ///
559    /// unsafe {
560    ///     assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1);
561    /// }
562    /// ```
563    /// # Safety
564    ///
565    /// The contained value must be of type `T`. Calling this method
566    /// with the incorrect type is *undefined behavior*.
567    #[unstable(feature = "downcast_unchecked", issue = "90850")]
568    #[inline]
569    pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T {
570        // SAFETY: guaranteed by caller
571        unsafe { <dyn Any>::downcast_unchecked_ref::<T>(self) }
572    }
573
574    /// Forwards to the method defined on the type `Any`.
575    ///
576    /// # Examples
577    ///
578    /// ```
579    /// #![feature(downcast_unchecked)]
580    ///
581    /// use std::any::Any;
582    ///
583    /// let mut x: Box<dyn Any> = Box::new(1_usize);
584    ///
585    /// unsafe {
586    ///     *x.downcast_unchecked_mut::<usize>() += 1;
587    /// }
588    ///
589    /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
590    /// ```
591    /// # Safety
592    ///
593    /// The contained value must be of type `T`. Calling this method
594    /// with the incorrect type is *undefined behavior*.
595    #[unstable(feature = "downcast_unchecked", issue = "90850")]
596    #[inline]
597    pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T {
598        // SAFETY: guaranteed by caller
599        unsafe { <dyn Any>::downcast_unchecked_mut::<T>(self) }
600    }
601}
602
603///////////////////////////////////////////////////////////////////////////////
604// TypeID and its methods
605///////////////////////////////////////////////////////////////////////////////
606
607/// A `TypeId` represents a globally unique identifier for a type.
608///
609/// Each `TypeId` is an opaque object which does not allow inspection of what's
610/// inside but does allow basic operations such as cloning, comparison,
611/// printing, and showing.
612///
613/// A `TypeId` is currently only available for types which ascribe to `'static`,
614/// but this limitation may be removed in the future.
615///
616/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
617/// noting that the hashes and ordering will vary between Rust releases. Beware
618/// of relying on them inside of your code!
619///
620/// # Layout
621///
622/// Like other [`Rust`-representation][repr-rust] types, `TypeId`'s size and layout are unstable.
623/// In particular, this means that you cannot rely on the size and layout of `TypeId` remaining the
624/// same between Rust releases; they are subject to change without prior notice between Rust
625/// releases.
626///
627/// [repr-rust]: https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.rust.unspecified
628///
629/// # Danger of Improper Variance
630///
631/// You might think that subtyping is impossible between two static types,
632/// but this is false; there exists a static type with a static subtype.
633/// To wit, `fn(&str)`, which is short for `for<'any> fn(&'any str)`, and
634/// `fn(&'static str)`, are two distinct, static types, and yet,
635/// `fn(&str)` is a subtype of `fn(&'static str)`, since any value of type
636/// `fn(&str)` can be used where a value of type `fn(&'static str)` is needed.
637///
638/// This means that abstractions around `TypeId`, despite its
639/// `'static` bound on arguments, still need to worry about unnecessary
640/// and improper variance: it is advisable to strive for invariance
641/// first. The usability impact will be negligible, while the reduction
642/// in the risk of unsoundness will be most welcome.
643///
644/// ## Examples
645///
646/// Suppose `SubType` is a subtype of `SuperType`, that is,
647/// a value of type `SubType` can be used wherever
648/// a value of type `SuperType` is expected.
649/// Suppose also that `CoVar<T>` is a generic type, which is covariant over `T`
650/// (like many other types, including `PhantomData<T>` and `Vec<T>`).
651///
652/// Then, by covariance, `CoVar<SubType>` is a subtype of `CoVar<SuperType>`,
653/// that is, a value of type `CoVar<SubType>` can be used wherever
654/// a value of type `CoVar<SuperType>` is expected.
655///
656/// Then if `CoVar<SuperType>` relies on `TypeId::of::<SuperType>()` to uphold any invariants,
657/// those invariants may be broken because a value of type `CoVar<SuperType>` can be created
658/// without going through any of its methods, like so:
659/// ```
660/// type SubType = fn(&());
661/// type SuperType = fn(&'static ());
662/// type CoVar<T> = Vec<T>; // imagine something more complicated
663///
664/// let sub: CoVar<SubType> = CoVar::new();
665/// // we have a `CoVar<SuperType>` instance without
666/// // *ever* having called `CoVar::<SuperType>::new()`!
667/// let fake_super: CoVar<SuperType> = sub;
668/// ```
669///
670/// The following is an example program that tries to use `TypeId::of` to
671/// implement a generic type `Unique<T>` that guarantees unique instances for each `Unique<T>`,
672/// that is, and for each type `T` there can be at most one value of type `Unique<T>` at any time.
673///
674/// ```
675/// mod unique {
676///     use std::any::TypeId;
677///     use std::collections::BTreeSet;
678///     use std::marker::PhantomData;
679///     use std::sync::Mutex;
680///
681///     static ID_SET: Mutex<BTreeSet<TypeId>> = Mutex::new(BTreeSet::new());
682///
683///     // TypeId has only covariant uses, which makes Unique covariant over TypeAsId 🚨
684///     #[derive(Debug, PartialEq)]
685///     pub struct Unique<TypeAsId: 'static>(
686///         // private field prevents creation without `new` outside this module
687///         PhantomData<TypeAsId>,
688///     );
689///
690///     impl<TypeAsId: 'static> Unique<TypeAsId> {
691///         pub fn new() -> Option<Self> {
692///             let mut set = ID_SET.lock().unwrap();
693///             (set.insert(TypeId::of::<TypeAsId>())).then(|| Self(PhantomData))
694///         }
695///     }
696///
697///     impl<TypeAsId: 'static> Drop for Unique<TypeAsId> {
698///         fn drop(&mut self) {
699///             let mut set = ID_SET.lock().unwrap();
700///             (!set.remove(&TypeId::of::<TypeAsId>())).then(|| panic!("duplicity detected"));
701///         }
702///     }
703/// }
704///
705/// use unique::Unique;
706///
707/// // `OtherRing` is a subtype of `TheOneRing`. Both are 'static, and thus have a TypeId.
708/// type TheOneRing = fn(&'static ());
709/// type OtherRing = fn(&());
710///
711/// fn main() {
712///     let the_one_ring: Unique<TheOneRing> = Unique::new().unwrap();
713///     assert_eq!(Unique::<TheOneRing>::new(), None);
714///
715///     let other_ring: Unique<OtherRing> = Unique::new().unwrap();
716///     // Use that `Unique<OtherRing>` is a subtype of `Unique<TheOneRing>` 🚨
717///     let fake_one_ring: Unique<TheOneRing> = other_ring;
718///     assert_eq!(fake_one_ring, the_one_ring);
719///
720///     std::mem::forget(fake_one_ring);
721/// }
722/// ```
723#[derive(Copy, PartialOrd, Ord)]
724#[derive_const(Clone, Eq)]
725#[stable(feature = "rust1", since = "1.0.0")]
726#[lang = "type_id"]
727#[ferrocene::prevalidated]
728pub struct TypeId {
729    /// This needs to be an array of pointers, since there is provenance
730    /// in the first array field. This provenance knows exactly which type
731    /// the TypeId actually is, allowing CTFE and miri to operate based off it.
732    /// At runtime all the pointers in the array contain bits of the hash, making
733    /// the entire `TypeId` actually just be a `u128` hash of the type.
734    pub(crate) data: [*const (); 16 / size_of::<*const ()>()],
735}
736
737// SAFETY: the raw pointer is always an integer
738#[stable(feature = "rust1", since = "1.0.0")]
739unsafe impl Send for TypeId {}
740// SAFETY: the raw pointer is always an integer
741#[stable(feature = "rust1", since = "1.0.0")]
742unsafe impl Sync for TypeId {}
743
744#[stable(feature = "rust1", since = "1.0.0")]
745#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
746impl const PartialEq for TypeId {
747    #[inline]
748    #[ferrocene::prevalidated]
749    fn eq(&self, other: &Self) -> bool {
750        #[cfg(miri)]
751        return crate::intrinsics::type_id_eq(*self, *other);
752        #[cfg(not(miri))]
753        {
754            let this = self;
755            crate::intrinsics::const_eval_select!(
756                @capture { this: &TypeId, other: &TypeId } -> bool:
757                if const {
758                    crate::intrinsics::type_id_eq(*this, *other)
759                } else {
760                    // Ideally we would just invoke `type_id_eq` unconditionally here,
761                    // but since we do not MIR inline intrinsics, because backends
762                    // may want to override them (and miri does!), MIR opts do not
763                    // clean up this call sufficiently for LLVM to turn repeated calls
764                    // of `TypeId` comparisons against one specific `TypeId` into
765                    // a lookup table.
766                    // SAFETY: We know that at runtime none of the bits have provenance and all bits
767                    // are initialized. So we can just convert the whole thing to a `u128` and compare that.
768                    unsafe {
769                        crate::mem::transmute::<_, u128>(*this) == crate::mem::transmute::<_, u128>(*other)
770                    }
771                }
772            )
773        }
774    }
775}
776
777impl TypeId {
778    /// Returns the `TypeId` of the generic type parameter.
779    ///
780    /// # Examples
781    ///
782    /// ```
783    /// use std::any::{Any, TypeId};
784    ///
785    /// fn is_string<T: ?Sized + Any>(_s: &T) -> bool {
786    ///     TypeId::of::<String>() == TypeId::of::<T>()
787    /// }
788    ///
789    /// assert_eq!(is_string(&0), false);
790    /// assert_eq!(is_string(&"cookie monster".to_string()), true);
791    /// ```
792    #[must_use]
793    #[stable(feature = "rust1", since = "1.0.0")]
794    #[rustc_const_stable(feature = "const_type_id", since = "1.91.0")]
795    #[ferrocene::prevalidated]
796    pub const fn of<T: ?Sized + 'static>() -> TypeId {
797        const { intrinsics::type_id::<T>() }
798    }
799
800    /// Checks if the [TypeId] implements the trait. If it does it returns [TraitImpl] which can be used to build a fat pointer.
801    /// It can only be called at compile time. `self` must be the [TypeId] of a sized type or None will be returned.
802    ///
803    /// # Examples
804    ///
805    /// ```
806    /// #![feature(type_info)]
807    /// use std::any::{TypeId};
808    ///
809    /// pub trait Blah {}
810    /// impl Blah for u8 {}
811    ///
812    /// assert!(const { TypeId::of::<u8>().trait_info_of::<dyn Blah>() }.is_some());
813    /// assert!(const { TypeId::of::<u16>().trait_info_of::<dyn Blah>() }.is_none());
814    /// ```
815    #[unstable(feature = "type_info", issue = "146922")]
816    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
817    pub const fn trait_info_of<
818        T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized + 'static,
819    >(
820        self,
821    ) -> Option<TraitImpl<T>> {
822        // SAFETY: The vtable was obtained for `T`, so it is guaranteed to be `DynMetadata<T>`.
823        // The intrinsic can't infer this because it is designed to work with arbitrary TypeIds.
824        unsafe { transmute(self.trait_info_of_trait_type_id(const { TypeId::of::<T>() })) }
825    }
826
827    /// Checks if the [TypeId] implements the trait of `trait_represented_by_type_id`. If it does it returns [TraitImpl] which can be used to build a fat pointer.
828    /// It can only be called at compile time. `self` must be the [TypeId] of a sized type or None will be returned.
829    ///
830    /// # Examples
831    ///
832    /// ```
833    /// #![feature(type_info)]
834    /// use std::any::{TypeId};
835    ///
836    /// pub trait Blah {}
837    /// impl Blah for u8 {}
838    ///
839    /// assert!(const { TypeId::of::<u8>().trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()) }.is_some());
840    /// assert!(const { TypeId::of::<u16>().trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()) }.is_none());
841    /// ```
842    #[unstable(feature = "type_info", issue = "146922")]
843    #[rustc_const_unstable(feature = "type_info", issue = "146922")]
844    pub const fn trait_info_of_trait_type_id(
845        self,
846        trait_represented_by_type_id: TypeId,
847    ) -> Option<TraitImpl<*const ()>> {
848        if self.info().size.is_none() {
849            return None;
850        }
851
852        if matches!(trait_represented_by_type_id.info().kind, TypeKind::DynTrait(_))
853            && let Some(vtable) = type_id_vtable(self, trait_represented_by_type_id)
854        {
855            Some(TraitImpl { vtable })
856        } else {
857            None
858        }
859    }
860
861    #[ferrocene::prevalidated]
862    fn as_u128(self) -> u128 {
863        let mut bytes = [0; 16];
864
865        // This is a provenance-stripping memcpy.
866        for (i, chunk) in self.data.iter().copied().enumerate() {
867            let chunk = chunk.addr().to_ne_bytes();
868            let start = i * chunk.len();
869            bytes[start..(start + chunk.len())].copy_from_slice(&chunk);
870        }
871        u128::from_ne_bytes(bytes)
872    }
873}
874
875#[stable(feature = "rust1", since = "1.0.0")]
876impl hash::Hash for TypeId {
877    #[inline]
878    fn hash<H: hash::Hasher>(&self, state: &mut H) {
879        // We only hash the lower 64 bits of our (128 bit) internal numeric ID,
880        // because:
881        // - The hashing algorithm which backs `TypeId` is expected to be
882        //   unbiased and high quality, meaning further mixing would be somewhat
883        //   redundant compared to choosing (the lower) 64 bits arbitrarily.
884        // - `Hasher::finish` returns a u64 anyway, so the extra entropy we'd
885        //   get from hashing the full value would probably not be useful
886        //   (especially given the previous point about the lower 64 bits being
887        //   high quality on their own).
888        // - It is correct to do so -- only hashing a subset of `self` is still
889        //   compatible with an `Eq` implementation that considers the entire
890        //   value, as ours does.
891        let data =
892        // SAFETY: The `offset` stays in-bounds, it just moves the pointer to the 2nd half of the `TypeId`.
893        // Only the first ptr-sized chunk ever has provenance, so that second half is always
894        // fine to read at integer type.
895            unsafe { crate::ptr::read_unaligned(self.data.as_ptr().cast::<u64>().offset(1)) };
896        data.hash(state);
897    }
898}
899
900#[stable(feature = "rust1", since = "1.0.0")]
901impl fmt::Debug for TypeId {
902    #[ferrocene::prevalidated]
903    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
904        write!(f, "TypeId({:#034x})", self.as_u128())
905    }
906}
907
908/// Returns the name of a type as a string slice.
909///
910/// # Note
911///
912/// This is intended for diagnostic use. The exact contents and format of the
913/// string returned are not specified, other than being a best-effort
914/// description of the type. For example, amongst the strings
915/// that `type_name::<Option<String>>()` might return are `"Option<String>"` and
916/// `"std::option::Option<std::string::String>"`.
917///
918/// The returned string must not be considered to be a unique identifier of a
919/// type as multiple types may map to the same type name. Similarly, there is no
920/// guarantee that all parts of a type will appear in the returned string. In
921/// addition, the output may change between versions of the compiler. For
922/// example, lifetime specifiers were omitted in some earlier versions.
923///
924/// The current implementation uses the same infrastructure as compiler
925/// diagnostics and debuginfo, but this is not guaranteed.
926///
927/// # Examples
928///
929/// ```rust
930/// assert_eq!(
931///     std::any::type_name::<Option<String>>(),
932///     "core::option::Option<alloc::string::String>",
933/// );
934/// ```
935#[must_use]
936#[stable(feature = "type_name", since = "1.38.0")]
937#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
938#[ferrocene::prevalidated]
939pub const fn type_name<T: ?Sized>() -> &'static str {
940    const { intrinsics::type_name::<T>() }
941}
942
943/// Returns the type name of the pointed-to value as a string slice.
944///
945/// This is the same as `type_name::<T>()`, but can be used where the type of a
946/// variable is not easily available.
947///
948/// # Note
949///
950/// Like [`type_name`], this is intended for diagnostic use and the exact output is not
951/// guaranteed. It provides a best-effort description, but the output may change between
952/// versions of the compiler.
953///
954/// In short: use this for debugging, avoid using the output to affect program behavior. More
955/// information is available at [`type_name`].
956///
957/// Additionally, this function does not resolve trait objects. This means that
958/// `type_name_of_val(&7u32 as &dyn Debug)` may return `"dyn Debug"`, but will not return `"u32"`
959/// at this time.
960///
961/// # Examples
962///
963/// Prints the default integer and float types.
964///
965/// ```rust
966/// use std::any::type_name_of_val;
967///
968/// let s = "foo";
969/// let x: i32 = 1;
970/// let y: f32 = 1.0;
971///
972/// assert!(type_name_of_val(&s).contains("str"));
973/// assert!(type_name_of_val(&x).contains("i32"));
974/// assert!(type_name_of_val(&y).contains("f32"));
975/// ```
976#[must_use]
977#[stable(feature = "type_name_of_val", since = "1.76.0")]
978#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
979#[ferrocene::prevalidated]
980pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
981    type_name::<T>()
982}
983
984/// Returns `Some(&U)` if `T` can be coerced to the trait object type `U`. Otherwise, it returns `None`.
985///
986/// # Compile-time failures
987/// Determining whether `T` can be coerced to the trait object type `U` requires compiler trait resolution.
988/// In some cases, that resolution can exceed the recursion limit,
989/// and compilation will fail instead of this function returning `None`.
990/// # Examples
991///
992/// ```rust
993/// #![feature(try_as_dyn)]
994///
995/// use core::any::try_as_dyn;
996///
997/// trait Animal {
998///     fn speak(&self) -> &'static str;
999/// }
1000///
1001/// struct Dog;
1002/// impl Animal for Dog {
1003///     fn speak(&self) -> &'static str { "woof" }
1004/// }
1005///
1006/// struct Rock; // does not implement Animal
1007///
1008/// let dog = Dog;
1009/// let rock = Rock;
1010///
1011/// let as_animal: Option<&dyn Animal> = try_as_dyn::<Dog, dyn Animal>(&dog);
1012/// assert_eq!(as_animal.unwrap().speak(), "woof");
1013///
1014/// let not_an_animal: Option<&dyn Animal> = try_as_dyn::<Rock, dyn Animal>(&rock);
1015/// assert!(not_an_animal.is_none());
1016/// ```
1017#[must_use]
1018#[unstable(feature = "try_as_dyn", issue = "144361")]
1019pub const fn try_as_dyn<
1020    T: Any + 'static,
1021    U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static,
1022>(
1023    t: &T,
1024) -> Option<&U> {
1025    let vtable: Option<ptr::DynMetadata<U>> =
1026        const { TypeId::of::<T>().trait_info_of::<U>().as_ref().map(TraitImpl::get_vtable) };
1027    match vtable {
1028        Some(dyn_metadata) => {
1029            let pointer = ptr::from_raw_parts(t, dyn_metadata);
1030            // SAFETY: `t` is a reference to a type, so we know it is valid.
1031            // `dyn_metadata` is a vtable for T, implementing the trait of `U`.
1032            Some(unsafe { &*pointer })
1033        }
1034        None => None,
1035    }
1036}
1037
1038/// Returns `Some(&mut U)` if `T` can be coerced to the trait object type `U`. Otherwise, it returns `None`.
1039///
1040/// # Compile-time failures
1041/// Determining whether `T` can be coerced to the trait object type `U` requires compiler trait resolution.
1042/// In some cases, that resolution can exceed the recursion limit,
1043/// and compilation will fail instead of this function returning `None`.
1044/// # Examples
1045///
1046/// ```rust
1047/// #![feature(try_as_dyn)]
1048///
1049/// use core::any::try_as_dyn_mut;
1050///
1051/// trait Animal {
1052///     fn speak(&self) -> &'static str;
1053/// }
1054///
1055/// struct Dog;
1056/// impl Animal for Dog {
1057///     fn speak(&self) -> &'static str { "woof" }
1058/// }
1059///
1060/// struct Rock; // does not implement Animal
1061///
1062/// let mut dog = Dog;
1063/// let mut rock = Rock;
1064///
1065/// let as_animal: Option<&mut dyn Animal> = try_as_dyn_mut::<Dog, dyn Animal>(&mut dog);
1066/// assert_eq!(as_animal.unwrap().speak(), "woof");
1067///
1068/// let not_an_animal: Option<&mut dyn Animal> = try_as_dyn_mut::<Rock, dyn Animal>(&mut rock);
1069/// assert!(not_an_animal.is_none());
1070/// ```
1071#[must_use]
1072#[unstable(feature = "try_as_dyn", issue = "144361")]
1073pub const fn try_as_dyn_mut<
1074    T: Any + 'static,
1075    U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static,
1076>(
1077    t: &mut T,
1078) -> Option<&mut U> {
1079    let vtable: Option<ptr::DynMetadata<U>> =
1080        const { TypeId::of::<T>().trait_info_of::<U>().as_ref().map(TraitImpl::get_vtable) };
1081    match vtable {
1082        Some(dyn_metadata) => {
1083            let pointer = ptr::from_raw_parts_mut(t, dyn_metadata);
1084            // SAFETY: `t` is a reference to a type, so we know it is valid.
1085            // `dyn_metadata` is a vtable for T, implementing the trait of `U`.
1086            Some(unsafe { &mut *pointer })
1087        }
1088        None => None,
1089    }
1090}