Extensible variants
Constructing an enum from one named variant, and taking one apart variant by variant.
Overview
In Context-Generic Programming (CGP), these traits are the enum counterpart of the record
builder: where a record is a product of named fields, an enum is a sum of named
variants, and the same name-driven, decoupled
treatment applies. They let generic code build and match an enum it cannot name, with exhaustiveness
proven in the type rather than by a wildcard arm. The impls come from
#[derive(ExtractField)] and
#[derive(FromVariant)].
The family splits into deconstruction and construction.
Deconstruction takes an enum apart. ExtractField pulls one variant out of an
extractor or hands back a narrowed remainder, and the three accessors obtain that extractor in one
ownership mode each: HasExtractor consumes the value,
HasExtractorRef borrows it, and HasExtractorMut
borrows it mutably. FinalizeExtract discharges the exhausted remainder with no
wildcard, and FinalizeExtractResult is the form that closes a chain from
the Result a step returns.
Construction is the smaller half. FromVariant builds an enum from a single
variant chosen by its type-level name, which lets code work in a narrow local enum and widen the
result.
The ideas behind them
- Extensible variants: partial variants, the exhaustiveness argument, and the extensible visitor pattern.
- Dispatching: routing a variant to the implementation that handles it.
An AI agent wrote this page using the CGP knowledge base. Its content was verified against the library's source. See How AI is used in this project.