Skip to main content

Traits

Context-Generic Programming (CGP) enables pluggable trait implementations in Rust, chosen at compile time, and the traits on these pages are the machinery its macros generate and consume. A component definition, a wiring table, a derive, or a field read all expand into impls of these traits, so you meet the names here in an expansion or a compiler error far more often than you write them. This section groups the traits by the job they do, so that a name from a diagnostic has a page to land on.

If you are new to CGP, start with the reference overview rather than here. This page is the fuller map for once the essentials are familiar.

The traits you meet first​

Three traits carry every wiring, and a wiring error names one of them. DelegateComponent records which provider a context uses for each component. IsProviderFor makes a missing dependency show up by name instead of hiding behind a bare "trait not implemented." CanUseComponent is the bound a check asserts. Read Wiring and checking first.

Reading a value out of a context is the next most common need. Field access covers reading one field by its type-level name, through HasField and the provider-side and lifetime-safe traits around it.

The extensible-data families​

Most of the remaining traits build and read structs and enums generically, by their fields and variants. They divide into families that mirror the extensible-data derives:

  • Field structure exposes a type's whole shape as a single type, and moves a value through it in both directions.
  • Record builders assemble a struct one field at a time, with field presence tracked at compile time so an incomplete record cannot be finalized.
  • Optional and defaulted fields relax that strict builder, filling a missing field from Default or reporting it at run time.
  • Extensible variants construct an enum from one named variant and take one apart variant by variant, with exhaustiveness proven in the type.
  • Structural casts convert between two data types by their shared fields or variants, with no hand-written conversion.
  • Type-level list algebra holds the per-field state markers and the list operations the families above are built from.

The specialized machinery​

Three smaller groups each serve one area:


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.