Skip to content

About

MultiAny is just like Any but can downcast to trait objects

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MultiAny

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.

Links

Example

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();

License

This project is licensed under the MIT License.

About

MultiAny is just like Any but can downcast to trait objects

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages