TraitcastableAny

Derive Macro TraitcastableAny 

Source
#[derive(TraitcastableAny)]
{
    // Attributes available to this derive:
    #[traitcast_targets]
}
Expand description

Derive macro implementing TraitcastableAny for a struct, enum or union.

Use the arguments to specify all possible target Traits for which trait objects are supposed to be downcastable from a dyn TraitcastableAny.

Example:

extern crate trait_cast;

use trait_cast::TraitcastableAny;


#[derive(TraitcastableAny)]
#[traitcast_targets(Print)]
struct Source(i32);

trait Print {
  fn print(&self);
}
impl Print for Source {
  fn print(&self) {
    println!("{}", self.0)
  }
}

fn main() {
  let source = Box::new(Source(5));
  let castable: Box<dyn TraitcastableAny> = source;
  let x: &dyn Print = castable.downcast_ref().unwrap();
  x.print();
}