Skip to main content

Namespaces and defaults

The lookup traits behind a namespace and a per-type default.

Overview​

In Context-Generic Programming (CGP), a namespace is a reusable table of default wirings that a context opts into and then completes with the entries the namespace leaves open. CGP enables pluggable trait implementations in Rust, and a namespace is where a group of those choices gets a name. Resolving one of those defaults means asking, for a given key, what the namespace delegates to, and these three traits answer it, differing only in how many types take part in the key. You name them where the syntax requires it: a namespace header or a for … in loop inside delegate_components!, and the #[default_impl] attribute. The macros generate the impls.

  • DefaultNamespace keys a default on the component alone, the common case that #[prefix(...)] registers into. It is the one member of the group that is in the prelude.
  • DefaultImpls1 adds one further type, for a per-type default where the same component resolves differently for String than for u64.
  • DefaultImpls2 does the same under a pair of types. Nothing in the library emits it; it is a provided extension point.

One edge is worth carrying across the group: for the two DefaultImpls traits the instance type takes the Self position and the component name is a leading parameter, which is the opposite of the parameter names' suggestion. The DefaultImpls1 page works it out.

The ideas behind them​

  • Namespaces: reusable, inheritable wiring tables and preset-style configuration.

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.