Macro plugin_context

Source
macro_rules! plugin_context {
    (
        $(#[$meta:meta])*
        name: $vis:vis $Name:ident,
        context: $ContextType:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        value: $ContextLiteral:expr $(,)?
    ) => { ... };
}
Expand description

Generates a stateless plugin context marker type for a plugin model or a plugin family.

This macro defines:

  1. A zero-sized marker struct representing the plugin context provider.
  2. An implementation of the ModelContext trait for that marker, including a constructor function context() returning a value on demand.

The value construction is on-demand (via ModelContext::context) which allows the context to depend on other constants, statics, or computed values while remaining compile-time friendly.

When marker is specified, the generated struct stores them in PhantomData fields so the type system correctly tracks them without affecting runtime behavior.

  • Type generics are tracked using PhantomData<(T, ...)>, preserving the usual marker semantics for type parameters.

§Syntax

plugin_context! {
    #[attributes...]                // Optional: struct-level attributes (docs, derives, etc.)
    name: pub ContextName,          // Required: visibility and name of the context marker struct
    context: ContextType,           // Required: type representing the context data

    marker: [T, U],                // Optional: phantom-data parameters applied to the marker
    bounds: [T: Default, U: Clone], // Optional: trait bounds for the generated impl

    value: ContextExpression,       // Required: expression producing the context value
}

§Attributes

Optional doc comments or other attributes can be attached to the generated marker by placing them above the macro invocation.

§Generics Support

  • Only type generics (T, U, etc.) are supported via marker: [...]
  • Lifetime generics are not supported
  • The generics are tracked using PhantomData and do not affect runtime behavior

§Examples

§Basic Context

plugin_context! {
    name: pub ElectionContext,
    context: PhragmenConfig,
    value: PhragmenConfig { sequential: true }
}

§Generic Context

plugin_context! {
    name: pub GenericContext,
    marker: [T],
    context: PhragmenConfig<T>,
    value: PhragmenConfig { sequential: true }
}