Supercharge Rust functions with implicit arguments using CGP v0.7.0 #21
soareschen
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If you have ever watched a Rust function signature grow from three parameters to ten because everything in the call chain needed to forward a value it did not actually use, CGP v0.7.0 has something for you.
Context-Generic Programming (CGP) is a modular programming paradigm for Rust that lets you write functions and trait implementations that are generic over a context type, without coherence restrictions, without runtime overhead, and without duplicating code across different structs. It builds entirely on Rust's own trait system — no proc-macro magic at runtime, no new language features required.
🚀 CGP v0.7.0 is out today, and the headline feature is
#[cgp_fn]with#[implicit]arguments.Here is what it looks like:
Three annotations do all of the work.
#[cgp_fn]turns a plain function into a context-generic capability.&selfis a reference to whatever context the function is called on — it does not refer to any concrete type. And#[implicit]onwidthandheighttells CGP to extract those values fromselfautomatically, so the caller never has to pass them explicitly. The function body is entirely ordinary Rust. There is nothing new to learn beyond the annotations themselves.The part worth pausing on is
Rectangle. All it does is deriveHasField. There is no manual trait implementation, noimpl CanCalculateArea for Rectangle, and no glue code of any kind. Any struct that carries awidth: f64and aheight: f64field will automatically gainrectangle_area()as a method — including structs you do not own and structs defined in entirely separate crates.This is what makes
#[cgp_fn]more than just syntactic sugar.rectangle_areais not coupled toRectangle. It is not coupled to any type at all. Two entirely independent context structs can share the same function without either one knowing the other exists, and the function's internal field dependencies are fully encapsulated — they do not propagate upward through callers the way explicit parameters do.v0.7.0 also ships
#[uses]and#[extend]for composing CGP functions together (analogous to Rust'suseandpub usefor modules),#[use_provider]for ergonomic composition of higher-order providers, and#[use_type]for importing abstract associated types so you can write functions generic over any scalar type withoutSelf::noise throughout the signature.The full release post — including desugaring walkthroughs, a comparison with Scala implicits (spoiler: CGP implicit arguments are unambiguous and non-propagating by construction), and two new step-by-step tutorials building up the full feature set from plain Rust — is available at https://contextgeneric.dev/blog/v0.7.0-release/
Beta Was this translation helpful? Give feedback.
All reactions