UseDelegatedType
Resolve an abstract type by looking its tag up in a table, instead of fixing it to one concrete type.
Overview
Under Context-Generic Programming (CGP), a context chooses the types behind its traits,
and UseDelegatedType<Components> is for the case where the concrete type an abstract type resolves
to should itself be decided by a table. The plain UseType<T> provider binds an
abstract type to one fixed T. But sometimes a single provider must answer several abstract-type
components at once, or route each type tag to a different concrete type chosen elsewhere, such as
when a namespace or a higher-order provider
supplies a coherent bundle of types. Hand-writing one UseType entry per tag would scatter that
decision; UseDelegatedType concentrates it into one Components table the provider consults. As
always, the context (the type that implements the trait)
points its type components at the provider.
The mechanism is the same indirection UseDelegate provides for behavioral
components, lifted to the type level. Where UseDelegate<Components> dispatches a method call to
whichever provider Components maps the active tag to, UseDelegatedType<Components> dispatches a
type resolution to whichever concrete type Components maps the tag to. Both read an entry out of a
DelegateComponent table keyed by the tag, but one yields behavior
and the other yields a type. Like every CGP provider, it carries no runtime value.
Usage
UseDelegatedType implements only the foundational TypeProvider, so a
#[cgp_type] component is wired to it through its
WithDelegatedType alias; see that page for the wiring form and a worked
example. Neither name is in the prelude, and both are imported from cgp::core::types.
The one place it is named directly is the built-in TypeProviderComponent, also imported from
cgp::core::types. There the lookup key is the Tag of HasType<Tag>, so the table maps tag types
to concrete types:
use core::marker::PhantomData;
use cgp::core::types::{HasType, TypeProviderComponent, UseDelegatedType};
use cgp::prelude::*;
pub struct ScalarTag;
pub struct IndexTag;
pub struct AppTypes;
delegate_components! {
AppTypes {
ScalarTag: f64,
IndexTag: usize,
}
}
pub struct App;
delegate_components! {
App {
TypeProviderComponent: UseDelegatedType<AppTypes>,
}
}
pub fn types(
scalar: PhantomData<<App as HasType<ScalarTag>>::Type>,
index: PhantomData<<App as HasType<IndexTag>>::Type>,
) -> (PhantomData<f64>, PhantomData<usize>) {
(scalar, index)
}
App answers HasType<ScalarTag> with f64 and HasType<IndexTag> with usize, and types
compiles only because they are those types. App is an
environmental context, and AppTypes is a plain
table whose values are types rather than providers.
When to use it
You choose UseDelegatedType by wiring its WithDelegatedType alias, so
the guidance on when to answer several abstract types from one shared table, rather than fixing each
one with UseType, lives with that alias, on
WithDelegatedType.
Under the hood
UseDelegatedType<Components> implements TypeProvider by looking the
type tag up in Components and reporting the delegate it finds as the abstract type:
#[cgp_provider(TypeProviderComponent)]
impl<Context, Tag, Components, Type> TypeProvider<Context, Tag> for UseDelegatedType<Components>
where
Components: DelegateComponent<Tag, Delegate = Type>,
{
type Type = Type;
}
The where clause is the whole of the behavior. Components: DelegateComponent<Tag, Delegate = Type>
reads the entry stored at key Tag in the table, and the impl sets the abstract Type to that
delegate. Because the lookup is keyed by Tag, one UseDelegatedType<Components> answers as many
distinct type tags as Components has entries, each resolving to its own concrete type. If the table
has no entry for a tag, the DelegateComponent bound is unsatisfied and the context does not implement
HasType for that tag. Contrast UseType<T>, whose impl is unconditional and always
reports the single type T: UseDelegatedType adds exactly one level of indirection, the
DelegateComponent lookup.
Related constructs
UseType— the simpler sibling that fixes an abstract type to one concrete type.UseDelegate— the behavioral counterpart that dispatches a method call through the same kind of table.DelegateComponent— the table it reads.HasType/TypeProvider— the abstract-type component it answers for.WithDelegatedType— the alias you wire, and the home of the import and worked example.WithProvider— the adapter behind theWithDelegatedTypealias.#[cgp_type]— defines the abstract-type components it resolves.
The ideas behind it:
- Abstract types — the associated types a context chooses for itself, here from a shared table.
Source
- Struct,
WithDelegatedTypealias, and theTypeProviderimpl:use_delegated_type.rs - The
HasType/TypeProvidertraits:has_type.rs;DelegateComponent:delegate_component.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.