Attribute Macro intertrait_macros::cast_to[][src]

#[cast_to]
Expand description

Attached on an impl item or type definition, registers traits as targets for casting.

If on an impl item, no argument is allowed. But on a type definition, the target traits must be listed explicitly.

Add [sync] before the list of traits if the underlying type is Sync + Send and you need std::sync::Arc.

Examples

On a trait impl

use intertrait::*;

struct Data;

trait Greet {
    fn greet(&self);
}

// Greet can be cast into from any sub-trait of CastFrom implemented by Data.
#[cast_to]
impl Greet for Data {
    fn greet(&self) {
        println!("Hello");
    }
}

On a type definition

Use when a target trait is derived or implemented in an external crate.

use intertrait::*;

// Debug can be cast into from any sub-trait of CastFrom implemented by Data
#[cast_to(std::fmt::Debug)]
#[derive(std::fmt::Debug)]
struct Data;

For Arc

Use when the underlying type is Sync + Send and you want to use Arc.

use intertrait::*;

// Debug can be cast into from any sub-trait of CastFrom implemented by Data
#[cast_to([sync] std::fmt::Debug)]
#[derive(std::fmt::Debug)]
struct Data;