Skip to main content

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.

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:

  1. Change the version to cgp = "0.7.0".
  2. Remove #[cgp_context] from each context, per the removal.
  3. Rewrite the check macros: check_components! and delegate_and_check_components!.
  4. Rename the type provider: ProvideType and TypeComponent, per the rename.
  5. Replace the generated aliases of #[cgp_type], per the alias removal.
  6. 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 an AppComponents struct that held the wiring table, so the table was written as delegate_components! { AppComponents { … } }.
  • New: pub struct App; with delegate_components! { App { GreeterComponent: GreetHello } }, which v0.6.1 already accepts. The attribute fails with cannot 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, and check_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 with expected 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 after for and the table type after ; came first, and the table type could differ from the context, as AppComponents did.
  • New: delegate_and_check_components! { App { GreeterComponent: GreetHello } } wires App and 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-written impl<Name> DelegateComponent<Name> for App, is wired with delegate_components! and checked with a separate check_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 component TypeComponent.
  • New: TypeProvider<Context, Tag>, which the prelude exports, with the component TypeProviderComponent in cgp::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 generated pub type ScalarOf<Context> = <Context as HasScalarType>::Scalar;.
  • New: the trait alone. A use of ScalarOf<App> fails with cannot 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 it Scalar.

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 a where clause 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. In fn rectangle_area(&self, #[implicit] width: f64, #[implicit] height: f64) -> f64, a caller passes neither argument, and any struct that derives HasField and has the two fields can call rectangle_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 Clone and not Copy. A &T argument borrows the field, a &str argument borrows a String field, and a &mut T argument needs a &mut self receiver and is the function's only implicit argument. The function's first parameter must be self.

Dependencies​

  • #[uses] imports a trait that the body calls on self (#197, #201). #[uses(RectangleArea)] on a function scaled_rectangle_area adds Self: RectangleArea to its impl alone, so a function that imports ScaledRectangleArea cannot call rectangle_area through 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 function scaled_rectangle_area makes RectangleArea a supertrait of ScaledRectangleArea, so a function that imports ScaledRectangleArea with #[uses] can call rectangle_area as well. It works in #[cgp_fn], #[cgp_component], and the getter macros. #[extend_where(...)] adds a predicate to the where clauses 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)] adds Inner: AreaCalculator<Self>, inserting the Self argument 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 write Scalar for Self::Scalar and adds HasScalarType as 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::Scalar imports a type from a context other than Self. 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 for Rectangle alone, 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 a delegate_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. See delegate_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 required Context: DelegateComponent<GreeterComponent> and Context::Delegate: Greeter<Context> (#205). A context that delegate_components! wires satisfies both forms in the same way. One that implements the provider trait by hand, as in impl Greeter<App> for App, now implements CanGreet too.
  • 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 with E0034. Write <App as CanMake>::make().

Fixes​

  • #[cgp_type] with a self-referential bound: a trait such as type Scalar: Mul<Output = Self::Scalar> + Copy; builds, where v0.6.1 failed with E0275 on the UseType provider (#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.