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.
- Release post: CGP v0.4.1 Release: new cgp-handler crate, improved preset macros, and more
- Crate:
cgp0.4.1 on crates.io - Changes: v0.4.0...v0.4.1
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, whichcgp::extra::handlerre-exports.
Upgrading
A project moving from v0.4.0 takes these steps, in this order:
- Change the version to
cgp = "0.4.1". - Build a
CharwithPhantomData, and format a type-level string through itsChartype, 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:
NilimplementedDisplay,Char<CHAR, Tail>(pub Tail)held its tail value, and aCharlist displayed when its tail did. - New:
NilimplementsStaticFormatand no longerDisplay.Char<CHAR, Tail>(pub PhantomData<Tail>)holds aPhantomData, and implementsDisplayandStaticFormatwhen its tail implementsStaticFormat. - What to do: write
Char(PhantomData)orDefault::default()where code built aCharfrom a tail value, and display a whole type-level string rather than a bareNil.
New features
Handlers
cgp-handlerdefines three components that share one shape.CanCompute<Code, Input>, with the provider traitComputer, transforms an input intoSelf::Output.CanProduce<Code>, withProducer, produces anOutputfrom the context alone.CanHandle<Code, Input>, withHandler, is asynchronous and fallible, returnsResult<Self::Output, Self::Error>, and requiresHasAsyncErrorType. Each method takes aPhantomData<Code>that selects the operation (#105). SeeComputer,Producer, andHandler.PipeHandlersruns providers in order.PipeHandlers<Product![A, B]>feeds the output of each provider to the next, for bothComputerandHandlerproviders, andPipeHandlers<Nil>returns its input (#105). SeePipeHandlers.PromoteandTryPromoteturn aComputerinto aHandler.Promote<Provider>wraps the output inOk, andTryPromote<Provider>serves aComputerwhose output is already aResultof the context's error (#105, #107). SeePromoteandTryPromote.
Dispatching on a parameter
derive_delegatederives aUseDelegateprovider.#[cgp_component { provider: ErrorRaiser, derive_delegate: UseDelegate<SourceError> }]implements the provider trait forUseDelegate<Components>, which looks the named parameter up inComponentsand forwards to the provider it finds. A list, such asderive_delegate: [UseDelegate<Code>, UseInputDelegate<Input>], derives one wrapper per entry (#106, #107). SeeUseDelegateandUseInputDelegate.#[wrap_provider(UseDelegate)]wraps a preset's provider.cgp_preset! { #[wrap_provider(UseDelegate)] MyPreset { … } }makesMyPreset::Providerthe typeUseDelegate<MyPreset::Components>, so a table keyed by ordinary types can be a delegation target. Without the attribute,MyPreset::Providernames the structMyPreset::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 ofcgp-macroexplain 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.