How Ambassador delegation works
masterAmbassador uses procedural macros to eliminate boilerplate when delegating trait implementations to struct fields or enum variants.
The workflow involves two main steps:
- Mark the trait: Use
#[delegatable_trait]on the trait definition to make it available for delegation. - Derive the delegation: Use
#[derive(Delegate)]and#[delegate(TraitName)]on the type (struct or enum) that should receive the implementation.
use ambassador::delegatable_trait;
#[delegatable_trait]
pub trait Shout {
fn shout(&self, input: &str) -> String;
}
#[derive(ambassador::Delegate)]
#[delegate(Shout)]
pub struct WrappedCat(Cat);