cgp_type

Attribute Macro cgp_type 

Source
#[cgp_type]
Expand description

#[cgp_type] is an extension to #[cgp_component] that derives additional constructs for abstract type components.

This macro can only be used on an abstract type trait that contains a single associated type as its item, and nothing else.

The macro can be used with no attribute argument, in which case the provider type would be named in the format {associated_type_name}TypeProvider.

In addition to the component constructs generated by #[cgp_component], the macro also generates a provider for UseType<Type>, that implements the provider trait using Type.

The macro also generates a type alias for accessing the associated type, with the type alias named in the format {associated_type_name}Of.

For advanced use cases, the macro also generates an implementation of WithProvider<Provider>, which implements the provider trait if the given Provider implements TypeProvider<Context, ComponentName>.

ยงExample

Given the following abstract type trait definition:

โ“˜
#[cgp_type]
pub trait HasNameType {
    type Name: Display;
}

would be equivalent to the following fully explicit definition:

โ“˜
#[cgp_type {
    name: NameTypeProviderComponent,
    provider: NameTypeProvider,
    context: Context,
}]
pub trait HasNameType {
    type Name: Display;
}

Which would generate the following constructs:

โ“˜
impl<Context, Type> NameTypeProvider<Context> for UseType<Type>
where
    Type: Display,
{
    type Name = Type;
}

type NameOf<Context> = <Context as HasNameType>::Name;

impl<Context, Provider> HasNameType<Context> for WithProvider<Provider>
where
    Provider: TypeProvider<Context, NameTypeProviderComponent>,
    Provider::Type: Display,
{
    type Name = Provider::Type;
}