Skip to content(if available)orjump to list(if available)

“Bypassing” specialization in Rust

maybevoid

With Context-Generic Programming (CGP), there is a way to get around the coherence restriction through a provider trait, and have overlapping implementations in safe, stable Rust. I have a proof of concept implementation of example in the article here: https://gist.github.com/soareschen/d37d74b26ecd0709517a80a3f...

maybevoid

For those who are new to the concept, in a nutshell, CGP allows you to bypass the coherence restrictions in traits/typeclasses, and define multiple overlapping and generic implementations for each CGP trait. As a consequence, when you define a new type, you need to specifically choose an implementation to be used with that type. This is called a wiring step, where you choose the provider implementations for your context.

On the surface, this addition doesn't seem significant. However, it opens up a world of possibilities for enabling powerful design patterns that are based on trait implementations that are named, generic, and overlappable. One of the greatest strengths of CGP is to enable safe, zero-cost, compile-time dispatching through the trait system to accomplish the things that in OOP would typically require dynamic dispatch with runtime errors.

ta8645

For anyone interested in reading about CGP in Rust, here is the website:

https://contextgeneric.dev/

Just a FYI from their main page:

As of 2025, CGP remains in its early stages of development. While promising, it still has several rough edges, particularly in areas such as documentation, tooling, debugging techniques, community support, and ecosystem maturity.

...

At this stage, CGP is best suited for early adopters and potential contributors who are willing to experiment and help shape its future.

hackyhacky

In Haskell, we call this problem "overlapping instances," and can be allowed with an optional compiler flag. Nevertheless, I try to avoid using it, because of the potential for unforeseen consequences and reduced clarity in which code is actually being run.

In this use case, my instinct tells me that trait specialization is the wrong tool for the job. The author is trying to dispatch different functions based on a flag set in the caller. I can think of two more elegant ways to do this:

* Make the file system itself a trait, with implementations for read-only and read/write. Pass the concrete implementation as a parameter to the function using it.

* Store the capabilities as a variable in the file system object and query it at runtime. This can be done with an if.

Sometimes the simpler approach is better.

nesarkvechnep

Your proposal might work but then the user won’t have the compile-time guarantee that they can’t try to write to a read-only filesystem, since the capability will be queried at run-time.

dwattttt

I may be misunderstanding, but

> Make the file system itself a trait, with implementations for read-only and read/write

Those are separate concrete implementations between read-only and read-write; a write operation can be implemented on the read-write implementation only.

nesarkvechnep

If the filesystem is a trait which has a read and write method. Your concrete implementations have to implement both.

SkiFire13

> Lastly, the unstable specialization feature also deals with lifetimes, which as I mentioned is the feature's biggest obstacle on the way to stabilization.

The issue is really that `specialization` *has* to deal with lifetimes, even when there is apparently none. The example core with `Read` and `Read + Seek` also suffers from lifetime issues: some type could implement `Seek` depending on some lifetime and this would make your specialization unsound. For this reason `min_specialization` does not accept your specialization.

konstantinua00

can't it be solved by negative traits?

isn't the problem that rw is still r, so passes checks for both?

can't you make one rw and the other r(-w)?

SkiFire13

Note that negative traits are not for "this trait is not implemented" (i.e. a missing `impl Trait for Type`) but instead for "this trait is guaranteed to not be implemented" (i.e. `impl !Trait for Type`)

38

[dead]