Macro plugin_types

Source
macro_rules! plugin_types {
    (
        input: $InputTy:ty,
        output: $OutputTy:ty,
        $(#[$model_meta:meta])*
        model: $ModelAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident $(,)?
    ) => { ... };
    (
        input: mut $MutateTy:ty,
        output: $OutputTy:ty,
        $(#[$model_meta:meta])*
        model: $ModelAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident $(,)?
    ) => { ... };
    (
        input: $(mut)? $InputTy:ty,
        output: $OutputTy:ty,
        $(borrow: [$($borrow_lt:lifetime)* $(,)?],)?
        root: $Root:ident,
        $(#[$model_meta:meta])*
        family: $FamilyAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident
        $(, provides: [$($provider:tt)*])? $(,)?
    ) => { ... };
}
Expand description

Declares associated plugin types inside a trait.

Supports both:

  • concrete plugin model binding, or
  • plugin family binding for late model selection.

Exactly one of model or family must be specified. Exactly one of input or input: mut must be specified.

§Syntax

§Immutable Concrete Model

plugin_types! {
    input: InputType,        // Required: immutable input type
    output: OutputType,      // Required: output type
    model: ModelAssoc,       // Required: associated plugin model
    context: ContextAssoc,   // Required: associated context provider
}

§Mutable Concrete Model

plugin_types! {
    input: mut MutateType,   // Required: mutable input type
    output: OutputType,      // Required: output type
    model: ModelAssoc,       // Required: associated plugin model
    context: ContextAssoc,   // Required: associated context provider
}

§Plugin Family (Immutable or Mutable)

plugin_types! {
    input: InputType,        // or: input: mut MutateType
    output: OutputType,      // Required: output type
    borrow: ['a],            // Optional: lifetime parameters of input/output
    root: PluginFamilyRoot,  // Required: plugin family root trait
    family: FamilyAssoc,     // Required: associated plugin family type
    context: ContextAssoc,   // Required: associated context provider
    provides: [Send + Sync], // Optional: bounds on context
}

§Lifetimes

  • lifetimes expands to <...> on the family associated type
  • Enables lifetime-parameterized associated types (GATs)

§Context Bounds

You may restrict the family’s context type using provides.

plugin_types! {
    input: Input,
    output: Output,
    root: PluginFamilyRoot,
    family: FamilyAssoc,    
    context: MyContext,
    provides: [Send + Sync + 'static],
}

Expands roughly to:

type MyContext: ModelContext<Context: Send + Sync + 'static>;

§Input / Output Constraints

§Plugin Models (Immutable & Mutable)

Input and Output must not contain generics or lifetime-based types (no GATs)

§Plugin Families

  • Input and Output may include lifetimes
  • Enabled via parameter borrow: ['a]
  • Type generics are still not supported
Model   -> concrete associated types only (no generics, no lifetimes, no GATS)
Family  -> supports lifetimes only (via borrow)

§Examples

§Concrete Model

pub trait Increment {
    type Input;
    type Output;

    plugin_types! {
        input: Self::Input,
        output: Self::Output,
        model: AddOneModel,
        context: AddOneCtx,
    }
}

§Plugin Family

pub trait MathOps {
    type Input;
    type Output;

    plugin_types! {
        input: Self::Input,
        output: Self::Output,
        root: MathFamilyRoot,
        family: MathFamily,
        context: MathContext,
    }
}