Skip to main content

TransformMapDefault

The transform marker that makes every field present, defaulting whatever is not.

info

Generated machinery​

You are not expected to name TransformMapDefault directly. It is the marker CanFinalizeWithDefault and CanBuildWithDefault drive, and you write a call to one of those. This page explains the three conversions behind them, which is also the model to copy if you write a transform of your own.

Overview​

Context-Generic Programming (CGP) tracks each field of a partial record with a type-level marker, and CanFinalizeWithDefault works by re-marking every field to IsPresent and then calling the ordinary finalize_build. TransformMapDefault is the marker that carries the per-field conversions making that possible.

You name it only when extending the layer. Using the defaulting workflow means calling finalize_with_default or build_with_default, without naming the marker.

Definition​

TransformMapDefault is a zero-sized marker type:

pub struct TransformMapDefault;

It stores nothing. It becomes a transform by implementing TransformMap once for each state a field might currently be in, every impl targeting IsPresent:

a field currentlybecomes
IsPresentitself, unchanged
IsNothingDefault::default()
IsOptionalits contents, or the default if None

Because all three impls target IsPresent, applying the marker across a record leaves every field present, the configuration FinalizeBuild accepts. The impl bodies are in Under the hood. The marker is not in the prelude; import it from cgp-field-extra.

Usage​

It is not in the prelude. Import it from cgp::extra::field::impls:

use cgp::extra::field::impls::TransformMapDefault;

There is nothing to call. The marker exists to be named in a TransformMapFields bound, which is where an operation says which conversion it drives:

Builder: TransformMapFields<TransformMapDefault, IsPresent>

A field's type must implement Default unless the field is already IsPresent, since two of the three impls carry the bound. That is where the defaulting layer's one real requirement comes from.

Examples​

A generic function that drives the defaulting conversion across any builder, and a core builder passed through it:

use cgp::core::field::impls::IsPresent;
use cgp::core::field::traits::{BuildField, FinalizeBuild, HasBuilder, TransformMapFields};
use cgp::extra::field::impls::TransformMapDefault;
use cgp::prelude::*;

#[derive(Debug, PartialEq, CgpData)]
pub struct Context {
pub foo: String,
pub bar: u64,
}

pub fn fill_defaults<Builder>(builder: Builder) -> Builder::Output
where
Builder: TransformMapFields<TransformMapDefault, IsPresent>,
{
builder.transform_map_fields()
}

pub fn demo() {
let full = fill_defaults(
Context::builder().build_field(PhantomData::<Symbol!("foo")>, "foo".to_owned()),
);
let context = full.finalize_build();
assert_eq!(context.bar, 0);
}

fill_defaults is CanFinalizeWithDefault without its last step: the bound names the marker and the target, and the result is at the all-present configuration, so finalize_build resolves on it. Swapping the marker for TransformOptional, with IsOptional as the target, is how ToOptional is written.

When to use it​

Name it only when writing an operation that drives the defaulting conversion. Everything else is served by the two operations already built on it.

Under the hood​

The marker is zero-sized, so it stores nothing. What it carries is three TransformMap impls, distinguished by their source marker:

impl<T> TransformMap<IsPresent, IsPresent, T> for TransformMapDefault {
fn transform_mapped(value: T) -> T {
value
}
}

impl<T: Default> TransformMap<IsNothing, IsPresent, T> for TransformMapDefault {
fn transform_mapped(_value: ()) -> T {
T::default()
}
}

impl<T: Default> TransformMap<IsOptional, IsPresent, T> for TransformMapDefault {
fn transform_mapped(value: Option<T>) -> T {
value.unwrap_or_default()
}
}

Note that each argument type is the source marker's projection (T, then (), then Option<T>), which keeps the three from overlapping. And note that only two carry the T: Default bound: a field already present does not need a default, which is why a fully-set core builder can be finalized this way regardless of its field types. An optional builder cannot: its set fields are still IsOptional, so the third impl, and its bound, applies to every field.

Three impls are needed rather than one because TransformMapFields resolves a conversion per field from whatever state that field is in, and a record may hold fields in all three states at once.

Common Mistakes​

It is not in the prelude. Import from cgp::extra::field::impls.

Two of its three impls require Default. A field whose type has none makes the walk unresolvable whenever that field is not IsPresent: unset on a core builder, and always on an optional one. Through a method call such as finalize_with_default, the error names the unmet TransformMapFields<TransformMapDefault, IsPresent> bound rather than the field; CanFinalizeWithDefault shows it.

It always targets IsPresent. It cannot be used to reach any other configuration; that is TransformOptional's job.

It is a marker, not an operation. It lacks a method to call and needs nothing wired. It is named in a bound.

A defaulted field is indistinguishable from one set to the default value in the result, which is the trade against FinalizeOptional.

The ideas behind it:

Source​


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.