Record builders
Assembling a struct one field at a time, with field presence tracked in the type.
Overview
Context-Generic Programming (CGP) builds and reads Rust structs generically, by their
named fields, and these traits are its record builder. They build a record incrementally, where the
fields come from several independent places and the result must still be checked at compile time.
The build starts from an empty partial value and adds fields one by one, and each field's presence
lives in the type, so finalizing an incomplete record is a missing impl rather than a runtime panic.
The impls come from #[derive(BuildField)], which also
generates the partial companion type.
The family divides into entry points, the update primitive and its two directions, and the finalize step.
The entry points obtain a partial value to build into. HasBuilder produces an
empty one, and IntoBuilder turns a complete value into a full one to redistribute.
The field operations move one field between states. UpdateField is the
primitive the derive writes; BuildField sets an absent field and
TakeField removes a present one, each a blanket impl over the primitive in one
direction.
The finalize step ends a build. FinalizeBuild turns a fully-present partial
value back into the concrete struct, and exists only at that configuration.
PartialData names the destination type at every configuration, which is how
generic builder code knows what it is building before the build is complete.
The ideas behind them
- Extensible records: partial records and the extensible builder pattern.
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.