WithField
Wire a getter component to a named context field, through the WithProvider adapter.
Overview
Under Context-Generic Programming (CGP), a getter can read a field chosen in wiring, and
WithField<Tag> is the alias WithProvider<UseField<Tag>>. It implements a getter component by
reading the context field named by Tag, on a context, the
type the method runs on, by adapting the foundational UseField<Tag> getter through
the WithProvider layer. On a getter it reads the same field the plain
UseField provider does, and the two are interchangeable there. On a
#[cgp_type] component only WithField works, and it sets the abstract
type to the field's type. Like every CGP provider, it carries no runtime value.
Usage
WithField is not in the prelude. Import it from cgp::core::field::impls. It takes one type parameter,
the field tag, and appears as the value of a getter component's wiring entry:
use cgp::core::field::impls::WithField;
delegate_components! {
Person {
NameGetterComponent: WithField<Symbol!("first_name")>,
}
}
The tag is a Symbol! for a named field or an Index<N> for a tuple field, and
the context must have a HasField implementation for it. This reads the
first_name field exactly as wiring UseField<Symbol!("first_name")> would.
Examples
A getter whose method name differs from the field it reads, wired through the alias:
use cgp::core::field::impls::WithField;
use cgp::prelude::*;
#[cgp_getter]
pub trait HasName {
fn name(&self) -> &str;
}
#[derive(HasField)]
pub struct Person {
pub first_name: String,
}
delegate_components! {
Person {
NameGetterComponent: WithField<Symbol!("first_name")>,
}
}
check_components! {
Person {
NameGetterComponent,
}
}
pub fn demo() {
let person = Person { first_name: "Alice".to_owned() };
assert_eq!(person.name(), "Alice");
}
WithField<Symbol!("first_name")> expands to WithProvider<UseField<Symbol!("first_name")>>, so
person.name() reads the first_name field. The field name lives in the wiring, not in the trait.
The same alias gives a type component a field's type, which the plain UseField cannot:
#[cgp_type]
pub trait HasWidthType {
type Width;
}
#[derive(HasField)]
pub struct Rectangle {
pub width: f32,
}
delegate_components! {
Rectangle {
WidthTypeProviderComponent: WithField<Symbol!("width")>,
}
}
Rectangle's Width is f32, the type of its width field. #[cgp_type] generates a
WithProvider impl but no UseField impl, so
WidthTypeProviderComponent: UseField<Symbol!("width")> fails with E0277, while UseField's own
TypeProvider impl serves it through WithField.
When to use it
Prefer the plain UseField<Tag> form on a getter. It reads the same field and is
the value a #[cgp_getter] component is normally wired to. Reach for
WithField on a #[cgp_type] component that should take a field's type, since only the
WithProvider route reaches UseField there.
For the common case of reading a field, an #[implicit] argument is
simpler than any getter provider. For a stored type that borrows to the getter's return type through
AsRef, use WithFieldRef.
Under the hood
WithField<Tag> is a type alias:
pub type WithField<Tag> = WithProvider<UseField<Tag>>;
The WithProvider adapter forwards a getter's provider-trait method to the inner
provider's foundational method, and UseField<Tag> is the foundational
FieldGetter that reads the field at Tag. The generated WithProvider
impl that makes this work is shown on the WithProvider page.
Related constructs
WithProvider— the adapter this alias specializes.UseField— the inner provider it wraps, and the plain form to prefer.WithFieldRef— the sibling alias for a field borrowed throughAsRef.#[cgp_getter]— defines the getter component and generates theWithProviderimpl.HasFieldand#[derive(HasField)]— the field access it reads, keyed bySymbol!orIndex<N>.
The ideas behind it:
- Impl-side dependencies — value injection through a provider's own requirements, of which reading a field is the most common form.
Source
- The alias:
use_field.rs
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.