Cgp-serde: A modular serialization library for Serde powered by CGP

https://news.ycombinator.com/rss Hits: 3
Summary

This is a companion blog post for my RustLab presentation titled How to Stop Fighting with Coherence and Start Writing Context-Generic Trait Impls. I am excited to announce the release of cgp-serde, a modular serialization library for Serde that leverages the power of Context-Generic Programming (CGP). In short, cgp-serde extends Serde鈥檚 original Serialize and Deserialize traits with CGP, making it possible to write overlapping or orphaned implementations of these traits and thus bypass the standard Rust coherence restrictions. Furthermore, cgp-serde allows us to leverage the powerful context and capabilities concepts in stable Rust today. This unlocks the ability to write context-dependent implementations of Deserialize, such as one that uses an arena allocator to deserialize a 'a T value, a concept detailed in the proposal article. For those readers new to the project, here is a quick introduction: Context-Generic Programming (CGP) is a modular programming paradigm that enables you to bypass the coherence restrictions in Rust traits, allowing for overlapping and orphan implementations of any CGP trait. You can adapt almost any existing Rust trait to use CGP today by applying the #[cgp_component] macro to the trait definition. After this annotation, you can write named implementations of the trait using #[cgp_impl], which can be defined without being constrained by the coherence rules. You can then selectively enable and reuse the named implementation for your type using the delegate_components! macro. For instance, we can, in principle, annotate the standard library鈥檚 Hash trait with #[cgp_component] like this: #[cgp_component(HashProvider)] pub trait Hash { ... } This change does not affect existing code that uses or implements Hash, but it allows for new, potentially overlapping implementations, such as one that works for any type that also implements Display: #[cgp_impl(HashWithDisplay)] impl<T: Display> HashProvider for T { ... } You can then apply and reuse t...

First seen: 2025-11-14 16:52

Last seen: 2025-11-14 18:52