Skip to main content

cgp v0.4.1

Version 0.4.1 of cgp, the library for Context-Generic Programming (CGP) that enables pluggable trait implementations in Rust, adds the cgp-handler crate and lets a component pick its implementation by the type of one of its parameters. This page lists every change a user can see and the steps that take a project there from v0.4.0.

Released on 2025-06-13.

Summary​

The new cgp-handler crate defines the CanCompute, CanProduce, and CanHandle components, with PipeHandlers and the promotion providers that connect them. #[cgp_component] derives a UseDelegate provider from a derive_delegate entry, and cgp_preset! can wrap its provider in UseDelegate. Type-level strings now format through a new StaticFormat trait, which changes what Char and Nil implement.

Compatibility​

  • Minimum Rust version: 1.81, unchanged from v0.4.0.
  • Released together: the workspace's 20 crates published on crates.io, all at 0.4.1.
  • New crates: cgp-handler, which cgp::extra::handler re-exports.

Upgrading​

A project moving from v0.4.0 takes these steps, in this order:

  1. Change the version to cgp = "0.4.1".
  2. Build a Char with PhantomData, and format a type-level string through its Char type, per the string formatting.

Breaking changes​

Type-level strings format through StaticFormat​

The new trait StaticFormat formats a type without a value, and Char implements Display through it (#104). See StaticFormat.

  • Old: Nil implemented Display, Char<CHAR, Tail>(pub Tail) held its tail value, and a Char list displayed when its tail did.
  • New: Nil implements StaticFormat and no longer Display. Char<CHAR, Tail>(pub PhantomData<Tail>) holds a PhantomData, and implements Display and StaticFormat when its tail implements StaticFormat.
  • What to do: write Char(PhantomData) or Default::default() where code built a Char from a tail value, and display a whole type-level string rather than a bare Nil.

New features​

Handlers​

  • cgp-handler defines three components that share one shape. CanCompute<Code, Input>, with the provider trait Computer, transforms an input into Self::Output. CanProduce<Code>, with Producer, produces an Output from the context alone. CanHandle<Code, Input>, with Handler, is asynchronous and fallible, returns Result<Self::Output, Self::Error>, and requires HasAsyncErrorType. Each method takes a PhantomData<Code> that selects the operation (#105). See Computer, Producer, and Handler.
  • PipeHandlers runs providers in order. PipeHandlers<Product![A, B]> feeds the output of each provider to the next, for both Computer and Handler providers, and PipeHandlers<Nil> returns its input (#105). See PipeHandlers.
  • Promote and TryPromote turn a Computer into a Handler. Promote<Provider> wraps the output in Ok, and TryPromote<Provider> serves a Computer whose output is already a Result of the context's error (#105, #107). See Promote and TryPromote.

Dispatching on a parameter​

  • derive_delegate derives a UseDelegate provider. #[cgp_component { provider: ErrorRaiser, derive_delegate: UseDelegate<SourceError> }] implements the provider trait for UseDelegate<Components>, which looks the named parameter up in Components and forwards to the provider it finds. A list, such as derive_delegate: [UseDelegate<Code>, UseInputDelegate<Input>], derives one wrapper per entry (#106, #107). See UseDelegate and UseInputDelegate.
  • #[wrap_provider(UseDelegate)] wraps a preset's provider. cgp_preset! { #[wrap_provider(UseDelegate)] MyPreset { … } } makes MyPreset::Provider the type UseDelegate<MyPreset::Components>, so a table keyed by ordinary types can be a delegation target. Without the attribute, MyPreset::Provider names the struct MyPreset::Components (#103).

Improvements​

  • The prelude exports UseDelegate. use cgp::prelude::*; brings it in (#103).
  • Common constructs carry documentation. The docs.rs pages for Async, #[async_trait], DelegateComponent, IsProviderFor, CanUseComponent, HasCgpProvider, Char, and the macros of cgp-macro explain what each does and give an example (#109).

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.