pub trait MultiAny: Any {
// Required method
fn get_metadata(&self, type_id: TypeId) -> Option<Meta>;
}Expand description
A trait for dynamic typing that allows downcasting to both concrete types and trait objects.
Similar to std::any::Any, but supports downcasting to traits implemented by the type.
Most users should implement this trait via the #[derive(MultiAny)] macro.
§Examples
use multi_any::*;
// Traits must be annotated with `#[multi_any]`
// With "nightly" feature this is not required
#[multi_any]
trait Trait1 {}
#[multi_any]
trait Trait2 {}
// MultiAny can be derived, and implemented traits must be specified
#[derive(MultiAny)]
#[multi_any(Trait1, Trait2)]
struct Foo;
impl Trait1 for Foo {}
impl Trait2 for Foo {}
// convert Box<Foo> to Box<dyn MultiAny>
let foo: Box<dyn MultiAny> = Box::new(Foo);
// downcast to concrete type
let foo_ref: &Foo = foo.downcast_ref().unwrap();
// downcast to trait object
let trait_ref = foo.downcast_ref::<dyn Trait1>().unwrap();
// downcast to Box
let foo: Box<dyn Trait1> = foo.downcast().unwrap();Required Methods§
Sourcefn get_metadata(&self, type_id: TypeId) -> Option<Meta>
fn get_metadata(&self, type_id: TypeId) -> Option<Meta>
Returns type-erased metadata for the requested type.
§Parameters
type_id: TheTypeIdof the requested type or trait object.
§Returns
Some(Meta)if the type matches the concrete type or a trait implemented by the object.Noneif the type does not match.
Implementors must ensure that the returned Meta matches the actual data type and metadata type.
Implementations§
Source§impl dyn MultiAny
impl dyn MultiAny
Sourcepub fn downcast_ref<RequestedType>(&self) -> Option<&RequestedType>
pub fn downcast_ref<RequestedType>(&self) -> Option<&RequestedType>
Attempts to downcast a MultiAny object to a reference of type RequestedType.
Returns Some(&RequestedType) if the object can be interpreted as the requested type,
or None otherwise.
§Type Parameters
RequestedType: The concrete type or trait object to downcast to.
§Panics
- Can panic with invalid implementation of
get_metadata(Derived implementation is guaranteed to be valid.)
Sourcepub fn downcast_mut<RequestedType>(&mut self) -> Option<&mut RequestedType>
pub fn downcast_mut<RequestedType>(&mut self) -> Option<&mut RequestedType>
Attempts to downcast a MultiAny object to a mutable reference of type RequestedType.
Returns Some(&mut RequestedType) if the object can be interpreted as the requested type,
or None otherwise.
§Type Parameters
RequestedType: The concrete type or trait object to downcast to.
§Panics
- Can panic with invalid implementation of
get_metadata(Derived implementation is guaranteed to be valid.)
Sourcepub fn downcast<RequestedType>(
self: Box<Self>,
) -> Result<Box<RequestedType>, Box<Self>>
pub fn downcast<RequestedType>( self: Box<Self>, ) -> Result<Box<RequestedType>, Box<Self>>
Attempts to downcast a Box<dyn MultiAny> to a Box<RequestedType>.
Returns Ok(Box<RequestedType>) if the object can be interpreted as the requested type,
or Err(Box<dyn MultiAny>) otherwise. This is a consuming operation.
§Type Parameters
RequestedType: The concrete type or trait object to downcast to.
§Panics
- Can panic with invalid implementation of
get_metadata(Derived implementation is guaranteed to be valid.)