dyn_trait

Attribute Macro dyn_trait 

#[dyn_trait]
Available on crate feature macros only.
Expand description

Generate a dyn compatible trait from a given trait declaration.

Method with a return-position impl trait, such as async method, are converted to return a DynObject. Other non dyn-compatible items are filtered.

§Arguments

  • trait: The generated dyn-compatible trait identifier, or a string template; default to `“Dyn{}”.
  • remote: Path to the concrete trait used in the implementation; the trait declaration must be pasted. It allows supporting traits defined in other crates.

§Trait attributes

Any #[dyn_trait(...)] attribute is converted to #[...] attribute and applied to the generated dyn-compatible trait. It can be used to apply dyn_object to the generated trait.

§Method attributes

Methods with are return-position impl trait, such as async methods, can be decorated with #[dyn_trait(...)] attribute with the following arguments:

  • try_sync: (must be applied to method returning Future) Generates an additional method suffixed with _try_sync, with a optimized execution path when the concrete method is synchronous and decorated with sync.
  • storage: Defines the default storage in the returned DynObject. Each method adds a generic storage parameter, whose default value is dyn_utils::DefaultStorage when not specified with the argument.

§Examples

#[dyn_utils::dyn_trait(trait = DynCallback)] // make the trait dyn-compatible
#[dyn_trait(dyn_utils::dyn_object)] // make the dyn-compatible trait usable with DynObject
trait Callback {
    #[dyn_trait(try_sync)] // add `call_try_sync` method with synchronous shortcut
    #[dyn_trait(storage = dyn_utils::storage::Raw<128>)] // use `Raw<128> as default storage
    fn call(&self, arg: &str) -> impl Future<Output = ()> + Send;
}