cgp v0.7.0
Version 0.7.0 of cgp, the library for Context-Generic Programming (CGP) that enables pluggable trait implementations in Rust, adds attributes that write context-generic code as plain functions, and removes #[cgp_context]. This page lists every change a user can see and the steps that take a project there from v0.6.1.
Released on 2026-02-28.
- Release post: Supercharge Rust functions with implicit arguments using CGP v0.7.0
- Crate:
cgp0.7.0 on crates.io - Changes: v0.6.1...v0.7.0
Summary
#[cgp_fn] turns a function into a trait, and #[implicit], #[uses], #[extend], #[use_provider], and #[use_type] replace the getter traits and where clauses that surround it. #[cgp_context] is removed, so a context is a plain struct that delegate_components! wires, and check_components! and delegate_and_check_components! take that context directly. ProvideType is renamed TypeProvider, and #[cgp_type] no longer generates its {Type}Of alias.
Compatibility
- Minimum Rust version: 1.89, unchanged from v0.6.1.
- Released together: the workspace's 23 crates published on crates.io, all at 0.7.0.
Upgrading
A project moving from v0.6.1 takes these steps, in this order:
- Change the version to
cgp = "0.7.0". - Remove
#[cgp_context]from each context, per the removal. - Rewrite the check macros:
check_components!anddelegate_and_check_components!. - Rename the type provider:
ProvideTypeandTypeComponent, per the rename. - Replace the generated aliases of
#[cgp_type], per the alias removal. - Build the crate and fix what the compiler reports.
Breaking changes
#[cgp_context] is removed
#[cgp_context] is removed from cgp and from the prelude (#205). A context is an ordinary struct that delegate_components! wires directly.
- Old:
#[cgp_context] pub struct App;generated anAppComponentsstruct that held the wiring table, so the table was written asdelegate_components! { AppComponents { … } }. - New:
pub struct App;withdelegate_components! { App { GreeterComponent: GreetHello } }, which v0.6.1 already accepts. The attribute fails withcannot find attribute `cgp_context` in this scope. - What to do: delete the attribute, and write the context's name wherever the code named
AppComponents. Update the context's check macros as the next two entries describe.
check_components! takes the context and an optional trait name
check_components! writes the context where it wrote the check trait's name, and an attribute names the trait (#214, #215). See check_components!.
- Old:
check_components! { CanUseApp for App { GreeterComponent } }. - New:
check_components! { App { GreeterComponent } }generates a check trait named__CheckApp, andcheck_components! { #[check_trait(CanUseApp)] App { GreeterComponent } }names it. A#[check_providers(...)]attribute may stand beside#[check_trait(...)]in either order. - What to do: delete
CanUseApp for, and add#[check_trait(CanUseApp)]where other code names the trait. The old form fails withexpected curly braces.
delegate_and_check_components! delegates on the context
delegate_and_check_components! wires the context itself and takes no separate table type (#214). See delegate_and_check_components!.
- Old:
delegate_and_check_components! { CanUseApp for App; App { GreeterComponent: GreetHello } }, where the name afterforand the table type after;came first, and the table type could differ from the context, asAppComponentsdid. - New:
delegate_and_check_components! { App { GreeterComponent: GreetHello } }wiresAppand generates a check trait named__CanUseApp, and#[check_trait(CanUseApp)]names it. - What to do: delete the
CanUseApp for App;prefix, and add#[check_trait(...)]where other code names the trait. A table that is not the context, such as one reached through a hand-writtenimpl<Name> DelegateComponent<Name> for App, is wired withdelegate_components!and checked with a separatecheck_components!.
ProvideType and TypeComponent are renamed TypeProvider and TypeProviderComponent
The provider trait of HasType and its component key take the names that #[cgp_type] gives its own providers (#211). UseType, UseDelegatedType, and UseField implement the renamed trait, and the prelude exports TypeProvider where it exported ProvideType. See HasType.
- Old:
ProvideType<Context, Tag>with the componentTypeComponent. - New:
TypeProvider<Context, Tag>, which the prelude exports, with the componentTypeProviderComponentincgp::core::types. - What to do: rename the trait and the key in each import, impl, bound, and
#[cgp_provider(TypeComponent)]attribute.
The {Type}Of alias is removed from #[cgp_type]
#[cgp_type] no longer generates a type alias for the associated type (#212). The aliases ErrorOf and RuntimeOf remain, written by hand in cgp::core::error and cgp::extra::runtime.
- Old:
#[cgp_type] pub trait HasScalarType { type Scalar; }also generatedpub type ScalarOf<Context> = <Context as HasScalarType>::Scalar;. - New: the trait alone. A use of
ScalarOf<App>fails withcannot find type `ScalarOf` in this scope. - What to do: write
<App as HasScalarType>::Scalar, or import the type with#[use_type(HasScalarType::Scalar)]and name itScalar.
New features
Functions
#[cgp_fn]turns a function into a trait with one blanket impl (#197, #198). The function name in PascalCase names the trait, and#[cgp_fn(CanGreet)]names it otherwise. A generic parameter on the function lands on the trait and the impl, and awhereclause lands on the impl alone. The trait takes the function's visibility, and the prelude exports the attribute. See#[cgp_fn].#[implicit]fills an argument from the context's field of the same name. Infn rectangle_area(&self, #[implicit] width: f64, #[implicit] height: f64) -> f64, a caller passes neither argument, and any struct that derivesHasFieldand has the two fields can callrectangle_area()(#197, #198, #201). The attribute also works in the methods of a#[cgp_impl]block, which removes the getter trait that a provider needed to read a field. See#[implicit].- An implicit argument is read according to its type. An owned argument is cloned out of the field, so its type needs
Cloneand notCopy. A&Targument borrows the field, a&strargument borrows aStringfield, and a&mut Targument needs a&mut selfreceiver and is the function's only implicit argument. The function's first parameter must beself.
Dependencies
#[uses]imports a trait that the body calls onself(#197, #201).#[uses(RectangleArea)]on a functionscaled_rectangle_areaaddsSelf: RectangleAreato its impl alone, so a function that importsScaledRectangleAreacannot callrectangle_areathrough it. It works in#[cgp_fn]and in#[cgp_impl]. See#[uses].#[extend]imports a trait and re-exports it (#197, #202).#[extend(RectangleArea)]on a functionscaled_rectangle_areamakesRectangleAreaa supertrait ofScaledRectangleArea, so a function that importsScaledRectangleAreawith#[uses]can callrectangle_areaas well. It works in#[cgp_fn],#[cgp_component], and the getter macros.#[extend_where(...)]adds a predicate to thewhereclauses of both the generated trait and its impl. See#[extend]and#[extend_where].#[use_provider]completes the bound of an inner provider (#204, #207, #210).#[use_provider(Inner: AreaCalculator)]addsInner: AreaCalculator<Self>, inserting theSelfargument that a hand-written bound needs, and several bounds on one provider join with+. It works in#[cgp_impl]and#[cgp_fn]. See#[use_provider].#[use_type]imports an abstract type under a bare name (#197, #200, #201, #202, #203).#[use_type(HasScalarType::Scalar)]lets the signature and body writeScalarforSelf::Scalarand addsHasScalarTypeas a supertrait. The brace form takes an alias,HasScalarType::{Scalar as S}, and an equality,HasScalarType::{Scalar = f64}, which pins the abstract type to a concrete one. The form@Types::HasScalarType::Scalarimports a type from a context other thanSelf. The attribute works in#[cgp_fn],#[cgp_impl],#[cgp_component], and the getter macros. See#[use_type].
Providers
#[cgp_impl(Self)]writes an impl of a consumer trait for one concrete context (#206).#[cgp_impl(Self)] impl CanCalculateArea for Rectangle { … }implements the consumer trait forRectanglealone, and a#[use_provider(RectangleArea: AreaCalculator)]attribute may import a provider for its body. The impl names its context type. See#[cgp_impl].
Wiring and checking
#[check_params(...)]and#[skip_check]configure adelegate_and_check_components!key (#214).#[check_params(f64)]before a key lists the generic arguments to check the component with, on one key or on a bracketed list of keys, and#[skip_check]leaves a key out of the check. Seedelegate_and_check_components!.
Improvements
- A context that implements a provider trait for itself implements the consumer trait. The blanket impl of a consumer trait requires
Context: Greeter<Context>, where it requiredContext: DelegateComponent<GreeterComponent>andContext::Delegate: Greeter<Context>(#205). A context thatdelegate_components!wires satisfies both forms in the same way. One that implements the provider trait by hand, as inimpl Greeter<App> for App, now implementsCanGreettoo. - A call without a receiver names its trait.
App::make()is ambiguous when the consumer trait and the provider trait are both in scope, and it fails withE0034. Write<App as CanMake>::make().
Fixes
#[cgp_type]with a self-referential bound: a trait such astype Scalar: Mul<Output = Self::Scalar> + Copy;builds, where v0.6.1 failed withE0275on theUseTypeprovider (#208). See#[cgp_type].
An AI agent wrote this page from the release's source changes and the CGP knowledge base. Its content was verified against the library's source at the release's tag. See How AI is used in this project.