r/rust • • 18h ago

💡 ideas & proposals Trying Incoherent Rust today with Context-Generic Programming

https://contextgeneric.dev/blog/incoherent-rust-today/

Author here. If you read Boxy's An Incoherent Rust and Nadrieril's posts on explicit trait proofs back in March and hoped to experiment with named impls, you can already try a good part of that design on stable Rust. My post translates that design, which it calls Incoherent Rust, into Context-Generic Programming (CGP), a library that gives Rust pluggable trait implementations, chosen at compile time.

The core move is small: a named impl becomes a provider type, and each context type chooses the providers it uses. So two impls that Rust would reject as overlapping both compile in CGP, and each context picks one. The compiler resolves that choice, so a call goes straight to the chosen provider, with no runtime lookup.

The post then covers these topics:

  • Incoherent trait bounds, and binding an impl to a struct so a HashMap always hashes the same way.
  • Contexts and capabilities, with an arena reaching a Deserialize impl through a field of the context.
  • The context type as a top-level dictionary that holds the other impls' dictionaries, in the sense of Nadrieril's dictionary-passing style.
  • Where CGP falls short: there are no nested scoped impls, &mut or owned capabilities need interior mutability, and an existing trait such as serde's Serialize can't adopt the pattern without a breaking change.
  • Ideas from CGP for the design: impls as first-class types, "incoherent functions", and selfless traits with methods.
17 Upvotes

5 comments sorted by

8

u/CalmCephalopod 15h ago

Waiting for someone smarter than me to comment.

5

u/bluejekyll hickory-dns · trust-dns 14h ago

Follow the link to the first post, you might find its background information helpful, “Foundational crates such as serde define foundational traits such as Serialize, and then every crate in the ecosystem needs to implement the Serialize traits for their own types. If a crate doesn’t implement serde’s traits for its types then those types can’t be used with serde as downstream crates cannot implement serde’s traits for another crate’s types.”

Basically, it looks like this post is suggesting a way to deal with this issue. I’m not fully grokking the solution at the moment, but hopefully that context helps.

8

u/juanfnavarror 11h ago edited 9h ago

Yes, they have built a system where you can have multiple trait impl’s for the same struct and choose which one to use either at the call site or compile time. They however made the messaging too complex and cared so much about credit that forgot to make it something anyone would want to use.

1

u/_choam_ 8h ago

Like haskell overlapping instances?

1

u/candyFawny 4h ago

this went straight over my head too