Skip to main content

WithContext

The WithProvider-adapted spelling of UseContext: answer a named type or getter component from the context's own generic HasType or HasField entry.

Overview​

Under Context-Generic Programming (CGP), a named component can take its answer from the context's own foundational entry, and WithContext is the alias WithProvider<UseContext>. It pairs the WithProvider adapter with UseContext, which implements the two foundational traits by asking the context, the type the implementation runs against: TypeProvider through the context's own HasType<Tag>, and FieldGetter through its own HasField<Tag>. WithProvider passes the component's marker as that tag, so wiring a component to WithContext answers it from the context's HasType or HasField entry keyed by the component itself. Like every CGP provider, it carries no runtime value.

WithContext is distinct from the bare UseContext, and the two are not interchangeable. UseContext implements a component's provider trait directly, by calling the context's consumer trait for that same component, which is how it serves as a higher-order provider's default inner provider and why wiring a component to it loops. WithContext goes through the foundational traits instead, so it reaches a different entry: the generic HasType or HasField table the context already has. It is the least-used member of the With… family.

Usage​

WithContext is not in the prelude; import it from cgp::core::component. It takes no type parameter and appears as the value of a type component's or single-method getter's wiring entry:

delegate_components! {
App {
TypeProviderComponent: UseType<String>,
NameTypeProviderComponent: WithContext,
}
}

What the entry needs from the context depends on the component's kind:

  • A #[cgp_type] component, such as HasNameType, resolves its type through the context's HasType<NameTypeProviderComponent>. The built-in TypeProviderComponent, imported from cgp::core::types, is the usual way to supply it; wired to UseType<String>, it answers every tag with String.
  • A single-method #[cgp_getter] component, such as HasName, reads the context's HasField<NameGetterComponent> entry. No derive generates a HasField impl keyed by a component marker, so the context implements it by hand.

Examples​

A context supplies one type for every tag through the built-in HasType, and routes a named type component to that answer with WithContext:

use core::marker::PhantomData;
use cgp::core::component::WithContext;
use cgp::prelude::*;
use cgp::core::types::TypeProviderComponent;

#[cgp_type]
pub trait HasNameType {
type Name;
}

pub struct App;

delegate_components! {
App {
TypeProviderComponent: UseType<String>,
NameTypeProviderComponent: WithContext,
}
}

check_components! {
App {
NameTypeProviderComponent,
}
}

pub fn name_type(name: PhantomData<<App as HasNameType>::Name>) -> PhantomData<String> {
name
}

App resolves HasNameType::Name by asking its own HasType<NameTypeProviderComponent>, which the TypeProviderComponent: UseType<String> entry answers with String, so name_type compiles only because Name is String. App is an environmental context with no fields.

When to use it​

Reach for WithContext when a context already answers a generic HasType or HasField lookup and a named component should take that answer. That is a narrow arrangement, so the adjacent constructs usually fit better:

  • UseType<T> on the named type component, to fix its type directly;
  • UseFields or UseField on a getter component, to read a named field;
  • an #[implicit] argument, where a provider needs a value from its own context;
  • the bare UseContext, as a higher-order provider's default inner provider.

Under the hood​

WithContext is a type alias:

pub type WithContext = WithProvider<UseContext>;

The WithProvider impl that #[cgp_type] generates bounds its inner provider by TypeProvider<__Context__, NameTypeProviderComponent>, and the one #[cgp_getter] generates bounds it by FieldGetter<__Context__, NameGetterComponent>. UseContext implements both by asking the context, the first through the UseContext impl #[cgp_component] generates for HasType and the second through a hand-written impl beside FieldGetter:

impl<Context, Tag, Field> FieldGetter<Context, Tag> for UseContext
where
Context: HasField<Tag, Value = Field>,
{
type Value = Field;

fn get_field(context: &Context, _tag: PhantomData<Tag>) -> &Self::Value {
context.get_field(PhantomData)
}
}

With the component's marker as Tag, the lookups are Context: HasType<NameTypeProviderComponent> and Context: HasField<NameGetterComponent>.

  • WithProvider — the adapter this alias specializes.
  • UseContext — the inner provider it wraps, whose direct form serves as a higher-order provider's inner default.
  • HasType / TypeProvider — the generic type lookup a type component reaches through it.
  • FieldGetter and HasField — the field lookup a getter component reaches through it.
  • WithField, WithType — the sibling aliases for a specific field or concrete type.

The ideas behind it:

  • Abstract types — the tag-indexed HasType component this routes a named type component through.

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.