DefaultImpls1
Resolving a namespace default keyed on a component and a type.
Generated machinery
You do not implement DefaultImpls1.
#[default_impl(...)] emits the impls and a for … in statement inside
delegate_components! consumes them. You name the trait in those two
places only; this page explains what each of them emits, and the positional rule that is easy to get
backwards.
Overview
In Context-Generic Programming (CGP), DefaultNamespace keys a
default on the component alone: one component, one default provider. CGP enables pluggable trait
implementations in Rust, and DefaultImpls1 is where a per-type default is registered. DefaultImpls1 adds a second type to the key, which a per-type default
needs: the same component resolving differently for String than for u64.
Definition
DefaultImpls1 carries a single associated type and nothing else:
pub trait DefaultImpls1<T, Components> {
type Delegate;
}
Self is the key being looked up, the instance type, such as String. T is the further lookup
type, which the attribute fills with the component name. Components is the table the lookup runs
against, and Delegate is the resolved provider. The trait has neither a method nor data, so
resolving a default projects Delegate from the matching impl, exactly as with
DelegateComponent.
Three traits exist rather than one variadic trait because each fixes the key's arity at the type level,
which lets the projection <Key as Trait<…, Delegate = Provider>> resolve cleanly.
DefaultImpls2 is the two-type form.
The parameter names are misleading about which position holds what. For this trait and its sibling,
Self is the instance type and the component name is a leading parameter, the opposite of
DefaultNamespace. The next section works it
out.
Usage
It is not in the prelude. Import it from cgp::core::component, and you will need to, because the
generated impl references the trait by name:
use cgp::core::component::DefaultImpls1;
You name it in two places: the target of a for … in loop inside
delegate_components!, and the
#[default_impl(...)] attribute that registers a provider as a default.
The macros generate the impls and the forwarding.
The one thing to get right
Registering ShowString as the String default for ShowImplComponent emits:
impl<Components> DefaultImpls1<ShowImplComponent, Components> for String {
type Delegate = ShowString;
}
Self is String; the trait's T parameter is filled by ShowImplComponent.
The rule that governs this comes from the attribute rather than the trait, and once you have it the
positions stop being surprising. #[default_impl(Key in NamespacePath)] makes Key the impl's
Self and appends the table parameter to whatever NamespacePath names, so the leading arguments
are whatever you wrote inside the path. The same rule is why the for … in loop's bound reads
T: DefaultImpls1<Component, App, Delegate = Provider>, with the loop variable in the Self
position.
Examples
The whole chain, from a provider declaring itself the String default to a context pulling
registered defaults in and adding an entry for a type the registry does not cover:
use core::fmt::Display;
use cgp::prelude::*;
use cgp::core::component::DefaultImpls1;
#[cgp_component(ShowImpl)]
#[prefix(@test in DefaultNamespace)]
pub trait Show<T> {
fn show(&self, value: &T) -> String;
}
#[cgp_impl(new ShowString)]
#[default_impl(String in DefaultImpls1<ShowImplComponent>)]
impl ShowImpl<String> {
fn show(&self, value: &String) -> String {
format!("{value:?}")
}
}
#[cgp_impl(new ShowWithDisplay)]
impl<T: Display> ShowImpl<T> {
fn show(&self, value: &T) -> String {
value.to_string()
}
}
pub struct App;
delegate_components! {
App {
namespace DefaultNamespace;
for <T, Provider> in DefaultImpls1<ShowImplComponent> {
@test.ShowImplComponent.T: Provider,
}
// `u64` has no registered default, so the context supplies one.
@test.ShowImplComponent.u64: ShowWithDisplay,
}
}
check_components! {
App {
ShowImplComponent: [String, u64],
}
}
pub fn demo() {
assert_eq!(App.show(&"hi".to_owned()), "\"hi\"");
assert_eq!(App.show(&5u64), "5");
}
Environmental context,
parameter-targeted. App carries the
wiring and the shown value is a parameter. The loop wires every type with a registered default by
projecting T: DefaultImpls1<ShowImplComponent, App, Delegate = Provider>. Only String has a
registered default, so the direct u64 line adds a type beside it; had u64 a registered default
too, the loop's impl and the direct entry would both cover its path, and the compiler would reject
them, as Common Mistakes shows.
A whole namespace can also be the loop target. After
cgp_namespace! {
new DefaultShowComponents {
[String, u64]: ShowWithDisplay,
}
}
a for <T, Provider> in DefaultShowComponents { … } loop wires the listed types through the same
projection.
When to use it
Use it for a per-type default, and reach for the simpler trait when the key is a component alone.
DefaultNamespacewhen one component has one default. That is the common case, and it is what#[prefix(...)]registers a component into.DefaultImpls1when the same component resolves differently per type.#[default_impl]is usually pointed at it.DefaultImpls2for a two-type key.- Define your own namespace trait when you want a named table of your own;
#[default_impl]accepts any path, so these three are conveniences rather than the only options.
One constraint decides where a #[default_impl] may be written, and it is Rust's orphan rule rather
than anything CGP chose. The emitted impl is impl Namespace<..> for Key, and Rust accepts it when
the crate owns the namespace trait, or when a local type appears in the impl header ahead of the table
parameter. That local type may be the key itself, or the component named inside the namespace path, so
a crate that owns ShowImplComponent may write String in DefaultImpls1<ShowImplComponent> against
the foreign DefaultImpls1. A path key is a PathCons list, which is never a local type even when it
contains a local marker, so a #[prefix]-ed component's path can be
registered only in the namespace's own crate. Wiring that must live downstream goes in the namespace
body of the crate that owns it, or into a local namespace that inherits the foreign one.
Under the hood
A for <T, Provider> in DefaultImpls1<Component> { … } loop emits a
DelegateComponent impl, with its
IsProviderFor pair, whose where clause projects the default.
cargo cgp expand on the example's App shows the first:
impl<
__Wildcard__,
T,
Provider,
> DelegateComponent<
PathCons<Symbol!("test"), PathCons<ShowImplComponent, PathCons<T, __Wildcard__>>>,
> for App
where
T: DefaultImpls1<ShowImplComponent, App, Delegate = Provider>,
{
type Delegate = Provider;
}
Read it as: for each type T that has a default, wire that key to the projected Provider.
Because the loop variables appear only in that bound and in the key, the key must mention T,
otherwise the parameters are unconstrained and the compiler rejects the impl with E0207, which
reads as a puzzling error about generic parameters rather than about the loop.
The registration side is the mirror. #[default_impl] emits an
impl of this trait for the key type:
impl<__Components__> DefaultImpls1<ShowImplComponent, __Components__> for String {
type Delegate = ShowString;
}
It carries only the parameters naming the key and provider plus the table, never the provider's
own where clause. A provider whose bounds come from #[use_type], #[uses], #[implicit], or
#[use_provider] therefore registers cleanly, because those bounds stay on the provider's impl and
its IsProviderFor, and are checked when a real context resolves
it.
Common Mistakes
It is not in the prelude. Import from cgp::core::component.
DefaultNamespace is.
Self is the instance type, not the component. The parameter names suggest otherwise. Read
#[default_impl(Key in Path)] as "Key becomes Self" and the positions follow.
A for … in loop's key must mention the loop variable. Writing a fixed type where the variable
belongs:
for <T, Provider> in DefaultImpls1<ShowImplComponent> {
@test.ShowImplComponent.String: Provider,
}
leaves both loop variables unconstrained:
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
...
error[E0207]: the type parameter `Provider` is not constrained by the impl trait, self type, or predicates
#[default_impl] on a prefixed component is confined to the namespace's crate, by the orphan rule.
Put downstream wiring in the namespace body instead.
A registered default cannot be overridden from the context. Adding
@test.ShowImplComponent.String: ShowWithDisplay beside the example's loop, for a type the loop
already wires through its registered default, overlaps the loop's impl:
error[E0119]: conflicting implementations of trait `IsProviderFor<PathCons<Symbol<4, cgp::macro_prelude::Chars<'t', cgp::macro_prelude::Chars<'e', cgp::macro_prelude::Chars<'s', cgp::macro_prelude::Chars<'t', Nil>>>>>, PathCons<ShowImplComponent, PathCons<String, _>>>, _, _>` for type `App`
...
error[E0119]: conflicting implementations of trait `DelegateComponent<PathCons<Symbol<4, cgp::macro_prelude::Chars<'t', cgp::macro_prelude::Chars<'e', cgp::macro_prelude::Chars<'s', cgp::macro_prelude::Chars<'t', Nil>>>>>, PathCons<ShowImplComponent, PathCons<String, _>>>>` for type `App`
The registration impl carries none of the provider's bounds, which is deliberate: they are checked where the provider is used rather than where it is registered.
Related constructs
DefaultNamespace: the one-key form, and the common case.DefaultImpls2: the two-type form.#[default_impl(...)]: the attribute that emits impls of this trait.cgp_namespace!: defines a namespace.#[prefix(...)]: registers a component into a namespace under a path.delegate_components!: carries thefor … inloop.DelegateComponent: what the loop ultimately writes.IsProviderFor: where a registered provider's real bounds are checked.RedirectLookup: the usualDelegatevalue.
The ideas behind it:
- Namespaces: reusable, inheritable wiring tables and preset-style configuration.
Source
namespaces.rs: the three lookup traits- The
#[default_impl]attribute:attributes/default_impl/ - Header and loop codegen:
delegate_component/statement/
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.