[][src]Attribute Macro cast_trait_object::dyn_upcast

#[dyn_upcast]
This is supported on crate feature proc-macros only.

Allow upcast from a trait to one of its supertraits. This can be used on traits or on types. For types you need to specify the path to the super trait inside the parenthesis after the macro name like so: #[dyn_upcast(SuperTrait)] while for traits don't need to specify anything #[dyn_upcast].

Examples

use cast_trait_object::*;

#[dyn_upcast]
trait Super {}

trait Sub: Super {}

#[dyn_upcast(Super)]
struct Foo;
impl Super for Foo {}
impl Sub for Foo {}

// We can now cast from one trait object to another:
let foo: &dyn Sub = &Foo;
let foo: &dyn Super = foo.dyn_upcast();

The macro can also be applied to a trait implementation (impl Trait for Type) instead of directly on a type:

#[dyn_upcast]
impl Super for Foo {}