| //! Utilities for dynamic typing or type reflection. | |
| //! | |
| //! # `Any` and `TypeId` | |
| //! | |
| //! `Any` itself can be used to get a `TypeId`, and has more features when used | |
| //! as a trait object. As `&dyn Any` (a borrowed trait object), it has the `is` | |
| //! and `downcast_ref` methods, to test if the contained value is of a given type, | |
| //! and to get a reference to the inner value as a type. As `&mut dyn Any`, there | |
| //! is also the `downcast_mut` method, for getting a mutable reference to the | |
| //! inner value. `Box<dyn Any>` adds the `downcast` method, which attempts to | |
| //! convert to a `Box<T>`. See the [`Box`] documentation for the full details. | |
| //! | |
| //! Note that `&dyn Any` is limited to testing whether a value is of a specified | |
| //! concrete type, and cannot be used to test whether a type implements a trait. | |
| //! | |
| //! [`Box`]: ../../std/boxed/struct.Box.html | |
| //! | |
| //! # Smart pointers and `dyn Any` | |
| //! | |
| //! One piece of behavior to keep in mind when using `Any` as a trait object, | |
| //! especially with types like `Box<dyn Any>` or `Arc<dyn Any>`, is that simply | |
| //! calling `.type_id()` on the value will produce the `TypeId` of the | |
| //! *container*, not the underlying trait object. This can be avoided by | |
| //! converting the smart pointer into a `&dyn Any` instead, which will return | |
| //! the object's `TypeId`. For example: | |
| //! | |
| //! ``` | |
| //! use std::any::{Any, TypeId}; | |
| //! | |
| //! let boxed: Box<dyn Any> = Box::new(3_i32); | |
| //! | |
| //! // You're more likely to want this: | |
| //! let actual_id = (&*boxed).type_id(); | |
| //! // ... than this: | |
| //! let boxed_id = boxed.type_id(); | |
| //! | |
| //! assert_eq!(actual_id, TypeId::of::<i32>()); | |
| //! assert_eq!(boxed_id, TypeId::of::<Box<dyn Any>>()); | |
| //! ``` | |
| //! | |
| //! ## Examples | |
| //! | |
| //! Consider a situation where we want to log a value passed to a function. | |
| //! We know the value we're working on implements `Debug`, but we don't know its | |
| //! concrete type. We want to give special treatment to certain types: in this | |
| //! case printing out the length of `String` values prior to their value. | |
| //! We don't know the concrete type of our value at compile time, so we need to | |
| //! use runtime reflection instead. | |
| //! | |
| //! ```rust | |
| //! use std::fmt::Debug; | |
| //! use std::any::Any; | |
| //! | |
| //! // Logger function for any type that implements `Debug`. | |
| //! fn log<T: Any + Debug>(value: &T) { | |
| //! let value_any = value as &dyn Any; | |
| //! | |
| //! // Try to convert our value to a `String`. If successful, we want to | |
| //! // output the `String`'s length as well as its value. If not, it's a | |
| //! // different type: just print it out unadorned. | |
| //! match value_any.downcast_ref::<String>() { | |
| //! Some(as_string) => { | |
| //! println!("String ({}): {}", as_string.len(), as_string); | |
| //! } | |
| //! None => { | |
| //! println!("{value:?}"); | |
| //! } | |
| //! } | |
| //! } | |
| //! | |
| //! // This function wants to log its parameter out prior to doing work with it. | |
| //! fn do_work<T: Any + Debug>(value: &T) { | |
| //! log(value); | |
| //! // ...do some other work | |
| //! } | |
| //! | |
| //! fn main() { | |
| //! let my_string = "Hello World".to_string(); | |
| //! do_work(&my_string); | |
| //! | |
| //! let my_i8: i8 = 100; | |
| //! do_work(&my_i8); | |
| //! } | |
| //! ``` | |
| //! | |
| #![stable(feature = "rust1", since = "1.0.0")] | |
| use crate::intrinsics::{self, type_id, type_id_vtable}; | |
| use crate::mem::transmute; | |
| use crate::mem::type_info::{TraitImpl, TypeKind}; | |
| use crate::{fmt, hash, ptr}; | |
| /////////////////////////////////////////////////////////////////////////////// | |
| // Any trait | |
| /////////////////////////////////////////////////////////////////////////////// | |
| /// A trait to emulate dynamic typing. | |
| /// | |
| /// Most types implement `Any`. However, any type which contains a non-`'static` reference does not. | |
| /// See the [module-level documentation][mod] for more details. | |
| /// | |
| /// [mod]: crate::any | |
| // This trait is not unsafe, though we rely on the specifics of it's sole impl's | |
| // `type_id` function in unsafe code (e.g., `downcast`). Normally, that would be | |
| // a problem, but because the only impl of `Any` is a blanket implementation, no | |
| // other code can implement `Any`. | |
| // | |
| // We could plausibly make this trait unsafe -- it would not cause breakage, | |
| // since we control all the implementations -- but we choose not to as that's | |
| // both not really necessary and may confuse users about the distinction of | |
| // unsafe traits and unsafe methods (i.e., `type_id` would still be safe to call, | |
| // but we would likely want to indicate as such in documentation). | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[rustc_diagnostic_item = "Any"] | |
| pub trait Any: 'static { | |
| /// Gets the `TypeId` of `self`. | |
| /// | |
| /// If called on a `dyn Any` trait object | |
| /// (or a trait object of a subtrait of `Any`), | |
| /// this returns the `TypeId` of the underlying | |
| /// concrete type, not that of `dyn Any` itself. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::{Any, TypeId}; | |
| /// | |
| /// fn is_string(s: &dyn Any) -> bool { | |
| /// TypeId::of::<String>() == s.type_id() | |
| /// } | |
| /// | |
| /// assert_eq!(is_string(&0), false); | |
| /// assert_eq!(is_string(&"cookie monster".to_string()), true); | |
| /// ``` | |
| #[stable(feature = "get_type_id", since = "1.34.0")] | |
| fn type_id(&self) -> TypeId; | |
| } | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| impl<T: 'static + ?Sized> Any for T { | |
| fn type_id(&self) -> TypeId { | |
| TypeId::of::<T>() | |
| } | |
| } | |
| /////////////////////////////////////////////////////////////////////////////// | |
| // Extension methods for Any trait objects. | |
| /////////////////////////////////////////////////////////////////////////////// | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| impl fmt::Debug for dyn Any { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("Any").finish_non_exhaustive() | |
| } | |
| } | |
| // Ensure that the result of e.g., joining a thread can be printed and | |
| // hence used with `unwrap`. May eventually no longer be needed if | |
| // dispatch works with upcasting. | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| impl fmt::Debug for dyn Any + Send { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("Any").finish_non_exhaustive() | |
| } | |
| } | |
| #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
| impl fmt::Debug for dyn Any + Send + Sync { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("Any").finish_non_exhaustive() | |
| } | |
| } | |
| impl dyn Any { | |
| /// Returns `true` if the inner type is the same as `T`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn is_string(s: &dyn Any) { | |
| /// if s.is::<String>() { | |
| /// println!("It's a string!"); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// is_string(&0); | |
| /// is_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn is<T: Any>(&self) -> bool { | |
| // Get `TypeId` of the type this function is instantiated with. | |
| let t = TypeId::of::<T>(); | |
| // Get `TypeId` of the type in the trait object (`self`). | |
| let concrete = self.type_id(); | |
| // Compare both `TypeId`s on equality. | |
| t == concrete | |
| } | |
| /// Returns some reference to the inner value if it is of type `T`, or | |
| /// `None` if it isn't. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn print_if_string(s: &dyn Any) { | |
| /// if let Some(string) = s.downcast_ref::<String>() { | |
| /// println!("It's a string({}): '{}'", string.len(), string); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// print_if_string(&0); | |
| /// print_if_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | |
| if self.is::<T>() { | |
| // SAFETY: just checked whether we are pointing to the correct type, and we can rely on | |
| // that check for memory safety because we have implemented Any for all types; no other | |
| // impls can exist as they would conflict with our impl. | |
| unsafe { Some(self.downcast_unchecked_ref()) } | |
| } else { | |
| None | |
| } | |
| } | |
| /// Returns some mutable reference to the inner value if it is of type `T`, or | |
| /// `None` if it isn't. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn modify_if_u32(s: &mut dyn Any) { | |
| /// if let Some(num) = s.downcast_mut::<u32>() { | |
| /// *num = 42; | |
| /// } | |
| /// } | |
| /// | |
| /// let mut x = 10u32; | |
| /// let mut s = "starlord".to_string(); | |
| /// | |
| /// modify_if_u32(&mut x); | |
| /// modify_if_u32(&mut s); | |
| /// | |
| /// assert_eq!(x, 42); | |
| /// assert_eq!(&s, "starlord"); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | |
| if self.is::<T>() { | |
| // SAFETY: just checked whether we are pointing to the correct type, and we can rely on | |
| // that check for memory safety because we have implemented Any for all types; no other | |
| // impls can exist as they would conflict with our impl. | |
| unsafe { Some(self.downcast_unchecked_mut()) } | |
| } else { | |
| None | |
| } | |
| } | |
| /// Returns a reference to the inner value as type `dyn T`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1); | |
| /// } | |
| /// ``` | |
| /// | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T { | |
| debug_assert!(self.is::<T>()); | |
| // SAFETY: caller guarantees that T is the correct type | |
| unsafe { &*(self as *const dyn Any as *const T) } | |
| } | |
| /// Returns a mutable reference to the inner value as type `dyn T`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let mut x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// *x.downcast_unchecked_mut::<usize>() += 1; | |
| /// } | |
| /// | |
| /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2); | |
| /// ``` | |
| /// | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T { | |
| debug_assert!(self.is::<T>()); | |
| // SAFETY: caller guarantees that T is the correct type | |
| unsafe { &mut *(self as *mut dyn Any as *mut T) } | |
| } | |
| } | |
| impl dyn Any + Send { | |
| /// Forwards to the method defined on the type `dyn Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn is_string(s: &(dyn Any + Send)) { | |
| /// if s.is::<String>() { | |
| /// println!("It's a string!"); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// is_string(&0); | |
| /// is_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn is<T: Any>(&self) -> bool { | |
| <dyn Any>::is::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `dyn Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn print_if_string(s: &(dyn Any + Send)) { | |
| /// if let Some(string) = s.downcast_ref::<String>() { | |
| /// println!("It's a string({}): '{}'", string.len(), string); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// print_if_string(&0); | |
| /// print_if_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | |
| <dyn Any>::downcast_ref::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `dyn Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn modify_if_u32(s: &mut (dyn Any + Send)) { | |
| /// if let Some(num) = s.downcast_mut::<u32>() { | |
| /// *num = 42; | |
| /// } | |
| /// } | |
| /// | |
| /// let mut x = 10u32; | |
| /// let mut s = "starlord".to_string(); | |
| /// | |
| /// modify_if_u32(&mut x); | |
| /// modify_if_u32(&mut s); | |
| /// | |
| /// assert_eq!(x, 42); | |
| /// assert_eq!(&s, "starlord"); | |
| /// ``` | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[inline] | |
| pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | |
| <dyn Any>::downcast_mut::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `dyn Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1); | |
| /// } | |
| /// ``` | |
| /// | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T { | |
| // SAFETY: guaranteed by caller | |
| unsafe { <dyn Any>::downcast_unchecked_ref::<T>(self) } | |
| } | |
| /// Forwards to the method defined on the type `dyn Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let mut x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// *x.downcast_unchecked_mut::<usize>() += 1; | |
| /// } | |
| /// | |
| /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2); | |
| /// ``` | |
| /// | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T { | |
| // SAFETY: guaranteed by caller | |
| unsafe { <dyn Any>::downcast_unchecked_mut::<T>(self) } | |
| } | |
| } | |
| impl dyn Any + Send + Sync { | |
| /// Forwards to the method defined on the type `Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn is_string(s: &(dyn Any + Send + Sync)) { | |
| /// if s.is::<String>() { | |
| /// println!("It's a string!"); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// is_string(&0); | |
| /// is_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
| #[inline] | |
| pub fn is<T: Any>(&self) -> bool { | |
| <dyn Any>::is::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn print_if_string(s: &(dyn Any + Send + Sync)) { | |
| /// if let Some(string) = s.downcast_ref::<String>() { | |
| /// println!("It's a string({}): '{}'", string.len(), string); | |
| /// } else { | |
| /// println!("Not a string..."); | |
| /// } | |
| /// } | |
| /// | |
| /// print_if_string(&0); | |
| /// print_if_string(&"cookie monster".to_string()); | |
| /// ``` | |
| #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
| #[inline] | |
| pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | |
| <dyn Any>::downcast_ref::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::Any; | |
| /// | |
| /// fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) { | |
| /// if let Some(num) = s.downcast_mut::<u32>() { | |
| /// *num = 42; | |
| /// } | |
| /// } | |
| /// | |
| /// let mut x = 10u32; | |
| /// let mut s = "starlord".to_string(); | |
| /// | |
| /// modify_if_u32(&mut x); | |
| /// modify_if_u32(&mut s); | |
| /// | |
| /// assert_eq!(x, 42); | |
| /// assert_eq!(&s, "starlord"); | |
| /// ``` | |
| #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
| #[inline] | |
| pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | |
| <dyn Any>::downcast_mut::<T>(self) | |
| } | |
| /// Forwards to the method defined on the type `Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// assert_eq!(*x.downcast_unchecked_ref::<usize>(), 1); | |
| /// } | |
| /// ``` | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_ref<T: Any>(&self) -> &T { | |
| // SAFETY: guaranteed by caller | |
| unsafe { <dyn Any>::downcast_unchecked_ref::<T>(self) } | |
| } | |
| /// Forwards to the method defined on the type `Any`. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(downcast_unchecked)] | |
| /// | |
| /// use std::any::Any; | |
| /// | |
| /// let mut x: Box<dyn Any> = Box::new(1_usize); | |
| /// | |
| /// unsafe { | |
| /// *x.downcast_unchecked_mut::<usize>() += 1; | |
| /// } | |
| /// | |
| /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2); | |
| /// ``` | |
| /// # Safety | |
| /// | |
| /// The contained value must be of type `T`. Calling this method | |
| /// with the incorrect type is *undefined behavior*. | |
| #[unstable(feature = "downcast_unchecked", issue = "90850")] | |
| #[inline] | |
| pub unsafe fn downcast_unchecked_mut<T: Any>(&mut self) -> &mut T { | |
| // SAFETY: guaranteed by caller | |
| unsafe { <dyn Any>::downcast_unchecked_mut::<T>(self) } | |
| } | |
| } | |
| /////////////////////////////////////////////////////////////////////////////// | |
| // TypeID and its methods | |
| /////////////////////////////////////////////////////////////////////////////// | |
| /// A `TypeId` represents a globally unique identifier for a type. | |
| /// | |
| /// Each `TypeId` is an opaque object which does not allow inspection of what's | |
| /// inside but does allow basic operations such as cloning, comparison, | |
| /// printing, and showing. | |
| /// | |
| /// A `TypeId` is currently only available for types which ascribe to `'static`, | |
| /// but this limitation may be removed in the future. | |
| /// | |
| /// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth | |
| /// noting that the hashes and ordering will vary between Rust releases. Beware | |
| /// of relying on them inside of your code! | |
| /// | |
| /// # Layout | |
| /// | |
| /// Like other [`Rust`-representation][repr-rust] types, `TypeId`'s size and layout are unstable. | |
| /// In particular, this means that you cannot rely on the size and layout of `TypeId` remaining the | |
| /// same between Rust releases; they are subject to change without prior notice between Rust | |
| /// releases. | |
| /// | |
| /// [repr-rust]: https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.rust.unspecified | |
| /// | |
| /// # Danger of Improper Variance | |
| /// | |
| /// You might think that subtyping is impossible between two static types, | |
| /// but this is false; there exists a static type with a static subtype. | |
| /// To wit, `fn(&str)`, which is short for `for<'any> fn(&'any str)`, and | |
| /// `fn(&'static str)`, are two distinct, static types, and yet, | |
| /// `fn(&str)` is a subtype of `fn(&'static str)`, since any value of type | |
| /// `fn(&str)` can be used where a value of type `fn(&'static str)` is needed. | |
| /// | |
| /// This means that abstractions around `TypeId`, despite its | |
| /// `'static` bound on arguments, still need to worry about unnecessary | |
| /// and improper variance: it is advisable to strive for invariance | |
| /// first. The usability impact will be negligible, while the reduction | |
| /// in the risk of unsoundness will be most welcome. | |
| /// | |
| /// ## Examples | |
| /// | |
| /// Suppose `SubType` is a subtype of `SuperType`, that is, | |
| /// a value of type `SubType` can be used wherever | |
| /// a value of type `SuperType` is expected. | |
| /// Suppose also that `CoVar<T>` is a generic type, which is covariant over `T` | |
| /// (like many other types, including `PhantomData<T>` and `Vec<T>`). | |
| /// | |
| /// Then, by covariance, `CoVar<SubType>` is a subtype of `CoVar<SuperType>`, | |
| /// that is, a value of type `CoVar<SubType>` can be used wherever | |
| /// a value of type `CoVar<SuperType>` is expected. | |
| /// | |
| /// Then if `CoVar<SuperType>` relies on `TypeId::of::<SuperType>()` to uphold any invariants, | |
| /// those invariants may be broken because a value of type `CoVar<SuperType>` can be created | |
| /// without going through any of its methods, like so: | |
| /// ``` | |
| /// type SubType = fn(&()); | |
| /// type SuperType = fn(&'static ()); | |
| /// type CoVar<T> = Vec<T>; // imagine something more complicated | |
| /// | |
| /// let sub: CoVar<SubType> = CoVar::new(); | |
| /// // we have a `CoVar<SuperType>` instance without | |
| /// // *ever* having called `CoVar::<SuperType>::new()`! | |
| /// let fake_super: CoVar<SuperType> = sub; | |
| /// ``` | |
| /// | |
| /// The following is an example program that tries to use `TypeId::of` to | |
| /// implement a generic type `Unique<T>` that guarantees unique instances for each `Unique<T>`, | |
| /// that is, for each type `T` there can be at most one value of type `Unique<T>` at any time. | |
| /// | |
| /// ``` | |
| /// mod unique { | |
| /// use std::any::TypeId; | |
| /// use std::collections::BTreeSet; | |
| /// use std::marker::PhantomData; | |
| /// use std::sync::Mutex; | |
| /// | |
| /// static ID_SET: Mutex<BTreeSet<TypeId>> = Mutex::new(BTreeSet::new()); | |
| /// | |
| /// // TypeId has only covariant uses, which makes Unique covariant over TypeAsId 🚨 | |
| /// #[derive(Debug, PartialEq)] | |
| /// pub struct Unique<TypeAsId: 'static>( | |
| /// // private field prevents creation without `new` outside this module | |
| /// PhantomData<TypeAsId>, | |
| /// ); | |
| /// | |
| /// impl<TypeAsId: 'static> Unique<TypeAsId> { | |
| /// pub fn new() -> Option<Self> { | |
| /// let mut set = ID_SET.lock().unwrap(); | |
| /// (set.insert(TypeId::of::<TypeAsId>())).then(|| Self(PhantomData)) | |
| /// } | |
| /// } | |
| /// | |
| /// impl<TypeAsId: 'static> Drop for Unique<TypeAsId> { | |
| /// fn drop(&mut self) { | |
| /// let mut set = ID_SET.lock().unwrap(); | |
| /// (!set.remove(&TypeId::of::<TypeAsId>())).then(|| panic!("duplicity detected")); | |
| /// } | |
| /// } | |
| /// } | |
| /// | |
| /// use unique::Unique; | |
| /// | |
| /// // `OtherRing` is a subtype of `TheOneRing`. Both are 'static, and thus have a TypeId. | |
| /// type TheOneRing = fn(&'static ()); | |
| /// type OtherRing = fn(&()); | |
| /// | |
| /// fn main() { | |
| /// let the_one_ring: Unique<TheOneRing> = Unique::new().unwrap(); | |
| /// assert_eq!(Unique::<TheOneRing>::new(), None); | |
| /// | |
| /// let other_ring: Unique<OtherRing> = Unique::new().unwrap(); | |
| /// // Use that `Unique<OtherRing>` is a subtype of `Unique<TheOneRing>` 🚨 | |
| /// let fake_one_ring: Unique<TheOneRing> = other_ring; | |
| /// assert_eq!(fake_one_ring, the_one_ring); | |
| /// | |
| /// std::mem::forget(fake_one_ring); | |
| /// } | |
| /// ``` | |
| #[derive(Copy, PartialOrd, Ord)] | |
| #[derive_const(Clone, Eq)] | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[lang = "type_id"] | |
| pub struct TypeId { | |
| /// This needs to be an array of pointers, since there is provenance | |
| /// in the first array field. This provenance knows exactly which type | |
| /// the TypeId actually is, allowing CTFE and miri to operate based off it. | |
| /// At runtime all the pointers in the array contain bits of the hash, making | |
| /// the entire `TypeId` actually just be a `u128` hash of the type. | |
| pub(crate) data: [*const (); 16 / size_of::<*const ()>()], | |
| } | |
| // SAFETY: the raw pointer is always an integer | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| unsafe impl Send for TypeId {} | |
| // SAFETY: the raw pointer is always an integer | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| unsafe impl Sync for TypeId {} | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] | |
| const impl PartialEq for TypeId { | |
| #[inline] | |
| fn eq(&self, other: &Self) -> bool { | |
| crate::intrinsics::type_id_eq(*self, *other) | |
| } | |
| } | |
| impl TypeId { | |
| /// Returns the `TypeId` of the generic type parameter. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use std::any::{Any, TypeId}; | |
| /// | |
| /// fn is_string<T: ?Sized + Any>(_s: &T) -> bool { | |
| /// TypeId::of::<String>() == TypeId::of::<T>() | |
| /// } | |
| /// | |
| /// assert_eq!(is_string(&0), false); | |
| /// assert_eq!(is_string(&"cookie monster".to_string()), true); | |
| /// ``` | |
| #[must_use] | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| #[rustc_const_stable(feature = "const_type_id", since = "1.91.0")] | |
| pub const fn of<T: ?Sized + 'static>() -> TypeId { | |
| const { intrinsics::type_id::<T>() } | |
| } | |
| /// Checks if the [TypeId] implements the trait. If it does it returns [TraitImpl] which can be used to build a fat pointer. | |
| /// It can only be called at compile time. `self` must be the [TypeId] of a sized type or None will be returned. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(type_info)] | |
| /// use std::any::{TypeId}; | |
| /// | |
| /// pub trait Blah {} | |
| /// impl Blah for u8 {} | |
| /// | |
| /// assert!(const { TypeId::of::<u8>().trait_info_of::<dyn Blah>() }.is_some()); | |
| /// assert!(const { TypeId::of::<u16>().trait_info_of::<dyn Blah>() }.is_none()); | |
| /// ``` | |
| #[unstable(feature = "type_info", issue = "146922")] | |
| #[rustc_const_unstable(feature = "type_info", issue = "146922")] | |
| #[rustc_comptime] | |
| pub fn trait_info_of<'a, T: TryAsDynCompatible<'a> + ?Sized>(self) -> Option<TraitImpl<T>> { | |
| // SAFETY: The vtable was obtained for `T`, so it is guaranteed to be `DynMetadata<T>`. | |
| // The intrinsic can't infer this because it is designed to work with arbitrary TypeIds. | |
| unsafe { transmute(self.trait_info_of_trait_type_id(const { type_id::<T>() })) } | |
| } | |
| /// 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. | |
| /// It can only be called at compile time. `self` must be the [TypeId] of a sized type or None will be returned. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// #![feature(type_info)] | |
| /// use std::any::{TypeId}; | |
| /// | |
| /// pub trait Blah {} | |
| /// impl Blah for u8 {} | |
| /// | |
| /// assert!(const { TypeId::of::<u8>().trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()) }.is_some()); | |
| /// assert!(const { TypeId::of::<u16>().trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()) }.is_none()); | |
| /// ``` | |
| #[unstable(feature = "type_info", issue = "146922")] | |
| #[rustc_const_unstable(feature = "type_info", issue = "146922")] | |
| #[rustc_comptime] | |
| pub fn trait_info_of_trait_type_id( | |
| self, | |
| trait_represented_by_type_id: TypeId, | |
| ) -> Option<TraitImpl<*const ()>> { | |
| if self.size().is_none() { | |
| return None; | |
| } | |
| if matches!(trait_represented_by_type_id.info().kind, TypeKind::DynTrait(_)) | |
| && let Some(vtable) = type_id_vtable(self, trait_represented_by_type_id) | |
| { | |
| Some(TraitImpl { vtable }) | |
| } else { | |
| None | |
| } | |
| } | |
| pub(crate) fn as_u128(self) -> u128 { | |
| let mut bytes = [0; 16]; | |
| // This is a provenance-stripping memcpy. | |
| for (i, chunk) in self.data.iter().copied().enumerate() { | |
| let chunk = chunk.addr().to_ne_bytes(); | |
| let start = i * chunk.len(); | |
| bytes[start..(start + chunk.len())].copy_from_slice(&chunk); | |
| } | |
| u128::from_ne_bytes(bytes) | |
| } | |
| } | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| impl hash::Hash for TypeId { | |
| #[inline] | |
| fn hash<H: hash::Hasher>(&self, state: &mut H) { | |
| // We only hash the lower 64 bits of our (128 bit) internal numeric ID, | |
| // because: | |
| // - The hashing algorithm which backs `TypeId` is expected to be | |
| // unbiased and high quality, meaning further mixing would be somewhat | |
| // redundant compared to choosing (the lower) 64 bits arbitrarily. | |
| // - `Hasher::finish` returns a u64 anyway, so the extra entropy we'd | |
| // get from hashing the full value would probably not be useful | |
| // (especially given the previous point about the lower 64 bits being | |
| // high quality on their own). | |
| // - It is correct to do so -- only hashing a subset of `self` is still | |
| // compatible with an `Eq` implementation that considers the entire | |
| // value, as ours does. | |
| let data = | |
| // SAFETY: The `offset` stays in-bounds, it just moves the pointer to the 2nd half of the `TypeId`. | |
| // Only the first ptr-sized chunk ever has provenance, so that second half is always | |
| // fine to read at integer type. | |
| unsafe { crate::ptr::read_unaligned(self.data.as_ptr().cast::<u64>().offset(1)) }; | |
| data.hash(state); | |
| } | |
| } | |
| #[stable(feature = "rust1", since = "1.0.0")] | |
| impl fmt::Debug for TypeId { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | |
| write!(f, "TypeId({:#034x})", self.as_u128()) | |
| } | |
| } | |
| /// Returns the name of a type as a string slice. | |
| /// | |
| /// # Note | |
| /// | |
| /// This is intended for diagnostic use. The exact contents and format of the | |
| /// string returned are not specified, other than being a best-effort | |
| /// description of the type. For example, amongst the strings | |
| /// that `type_name::<Option<String>>()` might return are `"Option<String>"` and | |
| /// `"std::option::Option<std::string::String>"`. | |
| /// | |
| /// The returned string must not be considered to be a unique identifier of a | |
| /// type as multiple types may map to the same type name. Similarly, there is no | |
| /// guarantee that all parts of a type will appear in the returned string. In | |
| /// addition, the output may change between versions of the compiler. For | |
| /// example, lifetime specifiers were omitted in some earlier versions. | |
| /// | |
| /// The current implementation uses the same infrastructure as compiler | |
| /// diagnostics and debuginfo, but this is not guaranteed. | |
| /// | |
| /// # Examples | |
| /// | |
| /// ```rust | |
| /// assert_eq!( | |
| /// std::any::type_name::<Option<String>>(), | |
| /// "core::option::Option<alloc::string::String>", | |
| /// ); | |
| /// ``` | |
| #[must_use] | |
| #[stable(feature = "type_name", since = "1.38.0")] | |
| #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] | |
| pub const fn type_name<T: ?Sized>() -> &'static str { | |
| const { intrinsics::type_name::<T>() } | |
| } | |
| /// Returns the type name of the pointed-to value as a string slice. | |
| /// | |
| /// This is the same as `type_name::<T>()`, but can be used where the type of a | |
| /// variable is not easily available. | |
| /// | |
| /// # Note | |
| /// | |
| /// Like [`type_name`], this is intended for diagnostic use and the exact output is not | |
| /// guaranteed. It provides a best-effort description, but the output may change between | |
| /// versions of the compiler. | |
| /// | |
| /// In short: use this for debugging, avoid using the output to affect program behavior. More | |
| /// information is available at [`type_name`]. | |
| /// | |
| /// Additionally, this function does not resolve trait objects. This means that | |
| /// `type_name_of_val(&7u32 as &dyn Debug)` may return `"dyn Debug"`, but will not return `"u32"` | |
| /// at this time. | |
| /// | |
| /// # Examples | |
| /// | |
| /// Prints the default integer and float types. | |
| /// | |
| /// ```rust | |
| /// use std::any::type_name_of_val; | |
| /// | |
| /// let s = "foo"; | |
| /// let x: i32 = 1; | |
| /// let y: f32 = 1.0; | |
| /// | |
| /// assert!(type_name_of_val(&s).contains("str")); | |
| /// assert!(type_name_of_val(&x).contains("i32")); | |
| /// assert!(type_name_of_val(&y).contains("f32")); | |
| /// ``` | |
| #[must_use] | |
| #[stable(feature = "type_name_of_val", since = "1.76.0")] | |
| #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] | |
| pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str { | |
| type_name::<T>() | |
| } | |
| /// Trait that is automatically implemented for all `dyn Trait<'b, C> + 'a` without assoc type bounds. | |
| /// The lifetime parameter should be the same that is used to constrain generic type parameters | |
| /// that are turned into the dyn trait constrained by `TryAsDynCompatible`. | |
| /// | |
| /// This is required for `try_as_dyn` to be able to soundly convert non-static | |
| /// types to `dyn Trait`. | |
| /// | |
| /// Note: these requirements are sufficient for soundness, but it is unclear | |
| /// if they are all necessary. We may be able to lift some requirements in favor | |
| /// of more precise ones. | |
| /// | |
| #[unstable(feature = "try_as_dyn", issue = "144361")] | |
| #[lang = "try_as_dyn"] | |
| #[rustc_deny_explicit_impl] | |
| pub trait TryAsDynCompatible<'a>: ptr::Pointee<Metadata = ptr::DynMetadata<Self>> {} | |
| /// Returns `Some(&U)` if `T` can be coerced to the dyn trait type `U`. Otherwise, it returns `None`. | |
| /// | |
| /// <div class="warning"> | |
| /// | |
| /// This function is implemented on a best-effort basis. It is not always possible to determine | |
| /// whether a generic type implements a trait; thus, this function may produce false negatives, | |
| /// returning `None` even when `T` implements the requested trait. | |
| /// | |
| /// `try_as_dyn` is guaranteed to return `None` if `T` does *not* implement the requested trait, but | |
| /// it is never guaranteed to return `Some`. It is intended to be used for performance | |
| /// optimizations and debugging, and `try_as_dyn` succeeding for a particular type should never be | |
| /// relied upon for correctness (i.e. callers must behave correctly even if `try_as_dyn` spuriously | |
| /// returns `None`). | |
| /// | |
| /// </div> | |
| /// | |
| /// # Examples of false negatives | |
| /// | |
| /// Some examples of situations where `try_as_dyn::<T, dyn Trait>` returns `None` in practice even | |
| /// when `T` implements `Trait`: | |
| /// * `T`'s impl for `Trait` is lifetime-dependent | |
| /// * `T`'s impl for `Trait` is a builtin impl (e.g. `dyn Debug` implements `Debug`) | |
| /// * `T`'s impl for `Trait` has a trait bound which requires transitively reasoning about | |
| /// lifetime-dependent or builtin impls | |
| /// | |
| /// This list is not exhaustive. There is some detailed documentation about these limitations at | |
| /// <https://doc.rust-lang.org/unstable-book/library-features/try-as-dyn.html> But the gist is | |
| /// summarized below: | |
| /// | |
| /// ## Lifetime-dependent impls | |
| /// | |
| /// `try_as_dyn` does not have access to lifetime information, thus it cannot differentiate between | |
| /// `'static` and other lifetimes and cannot reason about outlives bounds on impls. Thus it cannot | |
| /// reason about impls that have `'static` lifetimes or outlives bounds of any kind. | |
| /// | |
| /// The following impls are lifetime-dependent and produce false negatives when used with | |
| /// `try_as_dyn`: | |
| /// | |
| /// ```rust | |
| /// # trait Trait<'a, T> {} | |
| /// # struct Type<'b, U>(&'b U); | |
| /// # use std::fmt::{Debug, Display}; | |
| /// // impl mentions a 'static lifetime | |
| /// impl<'a, T: Debug, U: Display> Trait<'a, T> for Type<'static, U> {} | |
| /// ``` | |
| /// | |
| /// ``` | |
| /// # trait Trait<'a, T> {} | |
| /// # struct Type<'b, U>(&'b U); | |
| /// # use std::fmt::{Debug, Display}; | |
| /// // impl contains an outlives bound | |
| /// impl<'a, 'b, T: Debug, U: Display> Trait<'a, T> for Type<'b, U> | |
| /// where 'b: 'a {} | |
| /// ``` | |
| /// | |
| /// Impls that mention a generic parameter more than once are lifetime-dependent and produce false | |
| /// negatives, even if they don't expressly mention any lifetimes: | |
| /// | |
| /// ```rust | |
| /// # trait Trait<T> {} | |
| /// // impl mentions T more than once, creating an implied lifetime dependence | |
| /// impl<T> Trait<T> for T {} | |
| /// ``` | |
| /// | |
| /// The following impl is lifetime-**independent**, because even though it *mentions* lifetimes, | |
| /// implementation of the trait is not *conditional* over the lifetimes: | |
| /// ```rust | |
| /// # trait Trait<'a, T> {} | |
| /// # struct Type<'b, U>(&'b U); | |
| /// # use std::fmt::{Debug, Display}; | |
| /// impl<'a, 'b, T: Debug, U: Display> Trait<'a, T> for Type<'b, U> {} | |
| /// ``` | |
| /// | |
| /// Impls without generic parameters at all are also lifetime-independent, as long as they contain | |
| /// no `'static` lifetimes. | |
| /// | |
| /// ## Builtin impls | |
| /// | |
| /// Builtin impls (like `impl Debug for dyn Debug`, or automatic implementations of `Send` and | |
| /// `Sync`) have various obscure rules and often are not fully generic. To simplify reasoning about | |
| /// what is allowed and what not, all builtin impls are rejected and will neither directly nor | |
| /// indirectly contribute to a `Some` result. | |
| /// | |
| /// # Compile-time failures | |
| /// Determining whether `T` can be coerced to the dyn trait type `U` requires compiler trait resolution. | |
| /// In some cases, that resolution can exceed the recursion limit, | |
| /// and compilation will fail instead of this function returning `None`. | |
| /// | |
| /// The input type `T` must outlive the lifetime `'a` on the `dyn Trait + 'a`. | |
| /// This is basically the same rule that forbids `let x: &dyn Trait + 'static = &&some_local_variable;` | |
| /// So if you see borrow check errors around `try_as_dyn`, think about whether a normal unsizing | |
| /// coercion would be possible at all if you were using concrete types or had bounds on the input type. | |
| /// | |
| /// # Examples | |
| /// | |
| /// Using `try_as_dyn` to use bytewise comparison instead of PartialEq for certain types, similar to | |
| /// the standard library's optimization for slices: | |
| /// | |
| /// ```rust | |
| /// #![feature(try_as_dyn)] | |
| /// | |
| /// use core::any::try_as_dyn; | |
| /// | |
| /// /// Compares two objects for equality, | |
| /// fn eq<T: PartialEq + ?Sized>(x: &T, y: &T) -> bool { | |
| /// if try_as_dyn::<T, dyn BytewiseEq>(&x).is_some() { | |
| /// // T implements BytewiseEq, so we cast the slices to u8 and compare their bytes | |
| /// // instead of calling PartialEq on each individual element. | |
| /// unsafe { | |
| /// // SAFETY: x and y are valid for reads of size_of::<T>() bytes | |
| /// // BytewiseEq trait guarantees we can interperet these bytes as u8's | |
| /// // and compare them for equality | |
| /// let x = &*core::ptr::slice_from_raw_parts( | |
| /// (&raw const *x).cast::<u8>(), | |
| /// core::mem::size_of_val(x), | |
| /// ); | |
| /// let y = &*core::ptr::slice_from_raw_parts( | |
| /// (&raw const *y).cast::<u8>(), | |
| /// core::mem::size_of_val(y), | |
| /// ); | |
| /// | |
| /// x == y | |
| /// } | |
| /// } else { | |
| /// // T does not implement BytewiseEq, or try_as_dyn returned a false negative. | |
| /// // Fallback to PartialEq. | |
| /// // | |
| /// // BytewiseEq guarantees bytewise comparison and PartialEq will produce the same | |
| /// // results, so our code behaves correctly if try_as_dyn produces false negatives. | |
| /// x == y | |
| /// } | |
| /// } | |
| /// | |
| /// /// Marker trait for types that can be compared for equality | |
| /// /// using a bytewise comparison (i.e. memcmp). | |
| /// /// | |
| /// /// Implementations must ensure the type contains no uninitialized bytes, | |
| /// /// and that a bytewise comparison will produce the same result as PartialEq. | |
| /// unsafe trait BytewiseEq {} | |
| /// | |
| /// unsafe impl BytewiseEq for u8 {} | |
| /// unsafe impl BytewiseEq for u16 {} | |
| /// unsafe impl BytewiseEq for u32 {} | |
| /// | |
| /// // u16 implements BytewiseEq, so eq::<u16> will use bytewise comparison | |
| /// // (unless try_as_dyn returns a false negative) | |
| /// assert!(eq(&5u16, &5u16)); | |
| /// | |
| /// // f32 does not implement BytewiseEq, so eq::<f32> will use element-wise comparison | |
| /// assert!(eq(&5f32, &5f32)); | |
| /// ``` | |
| /// | |
| /// Using `try_as_dyn` for debugging: | |
| /// | |
| /// ```rust | |
| /// #![feature(try_as_dyn)] | |
| /// | |
| /// use core::any::{try_as_dyn, type_name}; | |
| /// use core::fmt::Debug; | |
| /// | |
| /// /// Prints a value of type T, attempting to use its Debug implementation with try_as_dyn. | |
| /// fn debug_println<T: ?Sized>(x: &T) { | |
| /// if let Some(debug) = try_as_dyn::<T, dyn Debug>(x) { | |
| /// println!("{:?}", debug); | |
| /// } else { | |
| /// // T does not implement Debug, or try_as_dyn returned a false negative. | |
| /// // Print the name of the type instead. | |
| /// // | |
| /// // We're not relying on this for correctness; it's just for debugging, | |
| /// // so we can tolerate false negatives. | |
| /// println!("<{}>", type_name::<T>()); | |
| /// } | |
| /// } | |
| /// | |
| /// /// This type does not implement Debug. | |
| /// struct NoDebug; | |
| /// | |
| /// // Prints "Hello, world!" unless try_as_dyn returns a false negative. | |
| /// debug_println(&"Hello, world!"); | |
| /// | |
| /// // Prints the name of the type, since it does not have a Debug implementation. | |
| /// debug_println(&NoDebug); | |
| /// | |
| /// // The current implementation of try_as_dyn gives a false positive in this case! | |
| /// debug_println(&"Hello, world!" as &dyn Debug); | |
| /// ``` | |
| #[must_use] | |
| #[unstable(feature = "try_as_dyn", issue = "144361")] | |
| pub const fn try_as_dyn<'a, T: ?Sized + 'a, U: TryAsDynCompatible<'a> + ?Sized>( | |
| t: &T, | |
| ) -> Option<&U> { | |
| // For unsized `T`, `trait_info_of` always returns `None` (vtable lookup is | |
| // only supported for sized types). The function therefore unconditionally | |
| // returns `None` in that case. | |
| let vtable: Option<ptr::DynMetadata<U>> = | |
| const { type_id::<T>().trait_info_of::<U>().as_ref().map(TraitImpl::get_vtable) }; | |
| match vtable { | |
| Some(dyn_metadata) => { | |
| let pointer = ptr::from_raw_parts(t as *const T as *const (), dyn_metadata); | |
| // SAFETY: `t` is a reference to a type, so we know it is valid. | |
| // `dyn_metadata` is a vtable for T, implementing the trait of `U`. | |
| // `T` is sized here because `trait_info_of` only returns `Some` for sized types, | |
| // so the thin data pointer fully describes the value. | |
| Some(unsafe { &*pointer }) | |
| } | |
| None => None, | |
| } | |
| } | |
| /// Returns `Some(&mut U)` if `T` can be coerced to the dyn trait type `U`. Otherwise, it returns `None`. | |
| /// | |
| /// See documentation of [try_as_dyn] for details about the behaviour and limitations. | |
| #[must_use] | |
| #[unstable(feature = "try_as_dyn", issue = "144361")] | |
| pub const fn try_as_dyn_mut<'a, T: ?Sized + 'a, U: TryAsDynCompatible<'a> + ?Sized>( | |
| t: &mut T, | |
| ) -> Option<&mut U> { | |
| // For unsized `T`, `trait_info_of` always returns `None` (vtable lookup is | |
| // only supported for sized types). The function therefore unconditionally | |
| // returns `None` in that case. | |
| let vtable: Option<ptr::DynMetadata<U>> = | |
| const { type_id::<T>().trait_info_of::<U>().as_ref().map(TraitImpl::get_vtable) }; | |
| match vtable { | |
| Some(dyn_metadata) => { | |
| let pointer = ptr::from_raw_parts_mut(t as *mut T as *mut (), dyn_metadata); | |
| // SAFETY: `t` is a reference to a type, so we know it is valid. | |
| // `dyn_metadata` is a vtable for T, implementing the trait of `U`. | |
| // `T` is sized here because `trait_info_of` only returns `Some` for sized types, | |
| // so the thin data pointer fully describes the value. | |
| Some(unsafe { &mut *pointer }) | |
| } | |
| None => None, | |
| } | |
| } |