multi-any provides a trait MultiAny that allows downcasting a Box<dyn MultiAny> to either a concrete type or a trait object. Structs can derive MultiAny for multiple traits using the provided procedural macro.
use multi_any::*;
// Traits must be annotated with `#[multi_any]`
// With the "nightly" feature, this is not required
#[multi_any]
trait Trait1 {}
#[multi_any]
trait Trait2 {}
// MultiAny can be derived; 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();This project is licensed under the MIT License.