Attribute Macro make_trait_castable

Source
#[make_trait_castable]
Expand description

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

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

Example:

extern crate trait_cast;

use trait_cast::{make_trait_castable, TraitcastTarget, TraitcastTo, TraitcastableAny};


#[make_trait_castable(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();
}