Attribute Macro dyn_object
#[dyn_object]Available on crate feature
macros only.Expand description
Make a trait compatible with DynObject.
§Arguments
bounds: Additional bounds, e.g.Send, allowing to useDynObject<dyn Trait + Send>.
§Examples
// Allows using both `DynObject<dyn Future>` and `DynObject<dyn Future + Send>`
#[dyn_object]
#[dyn_object(bounds = Send)]
trait Callback {
fn call(&self, arg: &str);
}
impl<F: Fn(&str)> Callback for F {
fn call(&self, arg: &str) {
self(arg)
}
}
// no allocation
let callback = DynObject::<dyn Callback>::new(|arg: &str| println!("{arg}"));§Limitations
When combined to dyn_trait, generic parameters are not supported.
ⓘ
#[dyn_utils::dyn_trait(trait = DynCallback)]
#[dyn_trait(dyn_utils::dyn_object)]
trait Callback<T> {
fn call(&self, arg: T) -> impl Future<Output = ()> + Send;
}When possible, using associated types instead overcomes this limitation
#[dyn_utils::dyn_trait(trait = DynCallback)]
#[dyn_trait(dyn_utils::dyn_object)]
trait Callback {
type Arg;
fn call(&self, arg: Self::Arg) -> impl Future<Output = ()> + Send;
}