cgp_component

Attribute Macro cgp_component 

Source
#[cgp_component]
Expand description

#[cgp_component] is the most basic macro used to define a CGP component.

The macro can be used on a Rust trait, which would be used as the base consumer trait for the macro to generate the other constructs, including the provider trait and the blanket implementations.

The macro accepts two forms of attribute arguments. If only an identifier is provided, the identifier is used as the name of the provider trait. Otherwise, the macro accepts a list of key/value pairs, with the following keys allowed:

  • name - the name of the component. If not provided, the name would be in the format {provider_trait_name}Component.

  • provider - the name of the provider trait.

  • context - the identifier used for the generic context type. If not provided, the default identifier Context would be used.

  • derive_delegate - a list of generic dispatcher wrappers to derive the UseDelegate pattern on, with the matching generic parameters specified in the generic argument of the wrapper. Refer to UseDelegate for more details.

§Extension Macros

There are two other macros that extends #[cgp_component] that can be used for deriving specialized CGP components: #[cgp_type] can be used to define abstract type components, and #[cgp_getter] can be used to define getter components.

These macros share the same arguments and expansion as #[cgp_component], but also derive additional constructs for the specialized use cases.

§Example

Given the following simplified component definition:

#[cgp_component(Greeter)]
pub trait CanGreet {
    fn greet(&self);
}

is equivalent to the fully explicit definition:

#[cgp_component {
    name: GreeterComponent,
    provider: Greeter,
    context: Context,
}]
pub trait CanGreet {
    fn greet(&self);
}