Skip to main content

Field structure

A type's whole shape as a single type, and the conversions that move a value through it.

Overview​

In Context-Generic Programming (CGP), where field access reads one named field, these traits describe a type's whole shape at once, every field, its name, and its type, as a single associated type. That is what lets generic code walk a struct or an enum it cannot name: a serializer, a validator, a merge, a dispatcher. The impls come from #[derive(HasFields)].

The family splits into traits that name the shape and conversions that move a value in and out of it.

The shape traits describe the type. HasFields names the owned shape (a Product! for a struct, a Sum! for an enum), and HasFieldsRef names the borrowed shape, with every value a reference.

The conversions move a concrete value through the shape. ToFields takes a value apart into its shape and FromFields rebuilds it, and the two round-trip through the identical type so nothing can fail. ToFieldsRef is the borrowing form, for code that reads a shape without consuming the value.

The ideas behind them​


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.