Skip to main content

ConcatPath

Joining two type-level paths.

info

Generated machinery​

You are not expected to call ConcatPath directly. #[cgp_component] uses it in the RedirectLookup impl it generates for every component, to append the component's type parameters to the lookup path that namespaces and the open statement route along. This page explains the operation, so that a composed path in an expansion or an error message is legible. The one case for naming it is generic code that composes paths rather than writing one out.

Overview​

In Context-Generic Programming (CGP), a Path! is a type-level list of segments: the route a namespaced or open-dispatched component lookup is redirected along. Composing two such routes means splicing one list onto the end of another, and ConcatPath is that operation. It is the path-level analogue of ConcatProduct, with the same two-impl recursion over a different list.

Definition​

ConcatPath carries a single associated type and nothing else:

pub trait ConcatPath<Other: ?Sized> {
type Output: ?Sized;
}

Self is the first path and Other the second, and Output is the first path's segments followed by the second's. Both sides may be unsized, since path types are markers rather than types anything is instantiated at. The trait has neither a method nor a value: ConcatPath is a pure type-level computation resolved during trait resolution, so it names the combined path type and nothing runs.

Usage​

ConcatPath is not in the prelude. Import it from cgp::core::base::traits, the same module as StaticFormat, which is defined in the same crate. StaticString comes from cgp::core::field::traits instead.

Examples​

Two paths joined at the type level, and a generic signature naming a composed route:

use cgp::core::base::traits::ConcatPath;
use cgp::prelude::*;

pub type Outer = Path!(@a.b);
pub type Inner = Path!(@c.d);

pub type Joined = <Outer as ConcatPath<Inner>>::Output;

// Compiles only if the joined path is `@a.b.c.d`.
pub fn assert_joined(path: PhantomData<Joined>) -> PhantomData<Path!(@a.b.c.d)> {
path
}

// A generic signature naming the composed route.
pub fn descend<Outer: ?Sized, Inner: ?Sized>(
) -> PhantomData<<Outer as ConcatPath<Inner>>::Output>
where
Outer: ConcatPath<Inner>,
{
PhantomData
}

assert_joined compiles only because the joined path is @a.b.c.d. Note the leading @: Path! requires it, and Path!(a.b) does not parse. The ?Sized bounds on descend are needed because a path is an unsized marker.

When to use it​

Reach for it when composing paths in generic code, which is rare outside the generated lookup impls. If you are writing a path literally, Path! already gives you the whole thing and there is nothing to concatenate.

  • Use ChainGetters to reach a field on a nested context. It chains getters through a Product! list, not a path, so this trait is not involved.
  • Use ConcatProduct for field lists rather than paths. The two recursions are the same shape over different lists and are not interchangeable.
  • Use StaticString if what you want is the segments as text. This produces a type; decoding it means decoding each segment's symbol.

Under the hood​

Two impls, one per list node. Each node keeps its head segment and rebuilds the tail; the terminator becomes the other path outright:

impl<Head: ?Sized, Tail: ?Sized, Other: ?Sized> ConcatPath<Other> for PathCons<Head, Tail>
where
Tail: ConcatPath<Other>,
{
type Output = PathCons<Head, <Tail as ConcatPath<Other>>::Output>;
}

impl<Other: ?Sized> ConcatPath<Other> for Nil {
type Output = Other;
}

Note the ?Sized on every parameter, including the associated type: that lets both operands be the unsized markers a path is built from, and it is the one way this recursion differs from ConcatProduct's otherwise identical shape. So the result is the first path's segments followed by the second's, in order, with the cost of resolution proportional to the first path's length.

Its one consumer shows the job. For a component Show<T>, #[cgp_component] generates a RedirectLookup provider impl that appends T to the path it was given before reading the table; cargo cgp expand shows the bounds:

impl<__Context__, T, __Components__, __Path__> ShowImpl<__Context__, T>
for RedirectLookup<__Components__, __Path__>
where
__Path__: ConcatPath<Path!(@T)>,
__Components__: DelegateComponent<<__Path__ as ConcatPath<Path!(@T)>>::Output>,
<__Components__ as DelegateComponent<
<__Path__ as ConcatPath<Path!(@T)>>::Output,
>>::Delegate: ShowImpl<__Context__, T>,
{
fn show(__context__: &__Context__, value: &T) -> String {
<__Components__ as DelegateComponent<
<__Path__ as ConcatPath<Path!(@T)>>::Output,
>>::Delegate::show(__context__, value)
}
}

That is how @test.ShowImplComponent becomes @test.ShowImplComponent.u64 for a u64 value, the key a namespaced or open entry is written at.

Common Mistakes​

Path! requires a leading @. Path!(@a.b) parses, and pub type Outer = Path!(a.b); fails with:

error: expected `@`

This is the most common way an otherwise-correct ConcatPath example fails to build.

It produces a type, not a joined string. This is path composition for trait resolution. If you want the segments as text, decode the symbols with StaticString.

Both sides may be unsized, which is why the trait carries ?Sized on the parameter and the output, a detail worth noticing if you write a bound over it and get an unexpected Sized error.

Its module differs from StaticString's. ConcatPath comes from cgp::core::base::traits and StaticString from cgp::core::field::traits, which catches people importing one and assuming the other came with it.

Order is part of the type. @a.b then @c.d is a different path from the reverse, and nothing normalizes them.

The ideas behind it:

  • Namespaces: reusable wiring tables and the paths that route into them.

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.