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
Transienttrait before the blanket impl for allT: Transientwill apply to them. - In addition to importing the
Anytrait, theDowncasttrait must also be brought into scope for thedyn Anymethods to become available. - Non-
'statictypes can be erased by parameterizing the trait with the desiredTransience, which the compiler will ensure is compatible. Types that are'staticcan use anyTransiencethey 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, sodyn Any<Co<'_>>is a different type thandyn Any<()>and they could not be stored together in the sameVec. To circumvent this limitation, a typeTcan be erased to any transience that is a supertype ofT::Transience; for example, ausizecan be erased todyn Any<Co<'_>>instead of the defaultdyn Any<()>so that it can be stored in aVecwith 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 safelydowncastto the shortened lifetime instead of the original (but if you are brave and/or careful, you can get around this usingunsafehacks like raw pointer casts andstd::mem::transmute). - The
Any::type_idmethod is difficult to use on concrete types as explained in its docstring; usingTypeId::of_valinstead. - The
*_uncheckedmethods do not require nightly builds. - Only
Boxs using theGlobalallocator are supported. - Only
Sizedtypes are supported.
This trait has a blanket impl for all Transient types with a compatible
Transience, and cannot be implemented directly.
Required Methods§
sourcefn type_id(&self) -> TypeId
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> + '_
impl<R: Transience> Debug for dyn Any<R> + '_
source§impl<R: Transience> Downcast<R> for dyn Any<R> + '_
impl<R: Transience> Downcast<R> for dyn Any<R> + '_
source§fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
Err variant
if the type was incorrect.source§fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.source§fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.source§unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
source§unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
source§unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
source§impl<R: Transience> Downcast<R> for dyn Any<R> + Send + '_
impl<R: Transience> Downcast<R> for dyn Any<R> + Send + '_
source§fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
Err variant
if the type was incorrect.source§fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.source§fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.source§unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
source§unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
source§unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
source§impl<R: Transience> Downcast<R> for dyn Any<R> + Send + Sync + '_
impl<R: Transience> Downcast<R> for dyn Any<R> + Send + Sync + '_
source§fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
Err variant
if the type was incorrect.source§fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.source§fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
T, or None if it isn’t.