Attribute Macro despatma::interpolate_traits[][src]

#[interpolate_traits]

Expands a list of despatma_lib::TraitSpecifier elements over a template. The TRAIT and CONCRETE markers in the template will be replaced with each passed in element. The template is annotated with this attribute.

This macro can be used to create concrete implementations for abstract_factory.

Example input

use despatma::interpolate_traits;

// GuiFactory and Factory is defined in the abstract_factory example
struct GnomeFactory{}
impl GuiFactory for GnomeFactory{}

// Implement the factory method for each GUI element
#[interpolate_traits(
    Button => GnomeButton,
    Scroller => Gnome2Scroller,
    Window => Gnome3Window,
)]
impl Factory<dyn TRAIT> for GnomeFactory {
    fn create(&self, name: String) -> Box<dyn TRAIT> {
        Box::new(CONCRETE::new(name))
    }
}

Output

This will implement the factory method (expand the template) for each element as follow.

use despatma::interpolate_traits;

// GuiFactory and Factory is defined in the abstract_factory example
struct GnomeFactory{}
impl GuiFactory for GnomeFactory{}

// Implement the factory method for each GUI element
impl Factory<dyn Button> for GnomeFactory {
    fn create(&self, name: String) -> Box<dyn Button> {
        Box::new(GnomeButton::new(name))
    }
}
impl Factory<dyn Scroller> for GnomeFactory {
    fn create(&self, name: String) -> Box<dyn Scroller> {
        Box::new(Gnome2Scroller::new(name))
    }
}
impl Factory<dyn Window> for GnomeFactory {
    fn create(&self, name: String) -> Box<dyn Window> {
        Box::new(Gnome3Window::new(name))
    }
}