Trait transient::Any

source ·
pub trait Any<R: Transience = ()> {
    // Required method
    fn type_id(&self) -> TypeId;
}
Expand description

A trait to emulate dynamic typing, modeled after the std::any::Any trait with added support for non-'static types.

This trait is primarily used as the dyn Any<R> trait object, which has its methods defined on the Downcast extension trait.

§Differences from std::any::Any

  • Types must first implement (or derive) the Transient trait before the blanket impl for all T: Transient will apply to them.
  • In addition to importing the Any trait, the Downcast trait must also be brought into scope for the dyn Any methods to become available.
  • Non-'static types can be erased by parameterizing the trait with the desired Transience, which the compiler will ensure is compatible. Types that are 'static can use any Transience they want, or exclude the parameter to use the default of ().
  • Not all dyn Any<_>’s are equal; the type parameter is considered to be be a part of the type, so dyn Any<Co<'_>> is a different type than dyn Any<()> and they could not be stored together in the same Vec. To circumvent this limitation, a type T can be erased to any transience that is a supertype of T::Transience; for example, a usize can be erased to dyn Any<Co<'_>> instead of the default dyn Any<()> so that it can be stored in a Vec with covariant types such as &'a usize. Note that if the transience is upcast to a shorter lifetime (or a longer lifetime in the contravariant case), then it can only be safely downcast to the shortened lifetime instead of the original (but if you are brave and/or careful, you can get around this using unsafe hacks like raw pointer casts and std::mem::transmute).
  • The Any::type_id method is difficult to use on concrete types as explained in its docstring; using TypeId::of_val instead.
  • The *_unchecked methods do not require nightly builds.
  • Only Boxs using the Global allocator are supported.
  • Only Sized types are supported.

This trait has a blanket impl for all Transient types with a compatible Transience, and cannot be implemented directly.

Required Methods§

source

fn type_id(&self) -> TypeId

Gets the TypeId of self, typically as an erased dyn Any trait object.

Note that this method is much harder to use on a concrete type than the std::any::Any::type_id method, since the trait’s generic parameter and blanket implementation means that any concrete type T actually has an entire family of Any implementations (one for each Transience type it can use to fill the R parameter), and the specific implementation to use would need to be specified explicitly using the fully qualified path (e.g. <T as Any<Co<'static>>>::type_id(&value)) even though they would all give the same result. To avoid this issue, you can instead use the TypeId::of_val function (e.g. TypeId::of_val(&value)) or the Transient::static_type_id method (e.g. value.static_type_id())

Trait Implementations§

source§

impl<R: Transience> Debug for dyn Any<R> + '_

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Transience> Debug for dyn Any<R> + Send + '_

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Transience> Debug for dyn Any<R> + Send + Sync + '_

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Transience> Downcast<R> for dyn Any<R> + '_

source§

fn is<T: Transient>(&self) -> bool

Returns true if the concrete type of the erased object is T, which can be used to predict the outcome of calling the downcast and similar methods. Read more
source§

fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

Attempt to downcast the box to a concrete type with its lifetime parameters restored, returning the original in the Err variant if the type was incorrect.
source§

fn downcast_ref<T: Transient>(&self) -> Option<&T>

Returns a reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>

Returns a mutable reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>

Downcasts the box to a concrete type without compile-time checks. Read more
source§

unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &T

Downcasts the shared reference to a concrete type without runtime checks. Read more
source§

unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut T

Downcasts the mutable reference to a concrete type without runtime checks. Read more
source§

impl<R: Transience> Downcast<R> for dyn Any<R> + Send + '_

source§

fn is<T: Transient>(&self) -> bool

Returns true if the concrete type of the erased object is T, which can be used to predict the outcome of calling the downcast and similar methods. Read more
source§

fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

Attempt to downcast the box to a concrete type with its lifetime parameters restored, returning the original in the Err variant if the type was incorrect.
source§

fn downcast_ref<T: Transient>(&self) -> Option<&T>

Returns a reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>

Returns a mutable reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>

Downcasts the box to a concrete type without compile-time checks. Read more
source§

unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &T

Downcasts the shared reference to a concrete type without runtime checks. Read more
source§

unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut T

Downcasts the mutable reference to a concrete type without runtime checks. Read more
source§

impl<R: Transience> Downcast<R> for dyn Any<R> + Send + Sync + '_

source§

fn is<T: Transient>(&self) -> bool

Returns true if the concrete type of the erased object is T, which can be used to predict the outcome of calling the downcast and similar methods. Read more
source§

fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

Attempt to downcast the box to a concrete type with its lifetime parameters restored, returning the original in the Err variant if the type was incorrect.
source§

fn downcast_ref<T: Transient>(&self) -> Option<&T>

Returns a reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>

Returns a mutable reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.
source§

unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>

Downcasts the box to a concrete type without compile-time checks. Read more
source§

unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &T

Downcasts the shared reference to a concrete type without runtime checks. Read more
source§

unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut T

Downcasts the mutable reference to a concrete type without runtime checks. Read more

Implementors§

source§

impl<T: Transient, R: Transience> Any<R> for T