Rust's trait system is famously powerful - and famously strict about avoiding ambiguity.
One such rule is that you can't have multiple blanket implementations of the same trait that could potentially apply to the same type.
What Is a Blanket Implementation?
A blanket implementation is a trait implementation that applies to any type meeting certain constraints, typically via generics.
A classic example from the standard library is how From
and Into
work together:
Thanks to this, when you implement From<T>
for U
, you automatically get Into<U>
for T
. Very ergonomic!
The Restriction
However, Rust enforces a key rule: no two blanket implementations may overlap - even in theory. Consider:
Even if no type currently implements both TraitA
and TraitB
, the compiler will reject this. The reason? Some type might satisfy both in the future, and that would make the implementation ambiguous.
A Real-World Problem
While working on Joydb, I ran into this exact problem.
I have an Adapter
trait responsible for persisting data.
In practice, there are two common ways to implement it:
- A unified adapter that stores all data in a single file (e.g., JSON). In Joydb, this is
UnifiedAdapter
. - A partitioned adapter that stores each relation in a separate file (e.g., one CSV per relation), called
PartitionedAdapter
.
Ideally, users would only need to implement one of those and get the Adapter
trait "for free".
But Rust won't let me define two conflicting blanket implementations. So... is there a workaround? 🤔
The Trait Definitions in Joydb
Here are the relevant traits in Joydb:
So the question becomes: how can I let someone implement either UnifiedAdapter
or PartitionedAdapter
, and then get Adapter
automatically?
The Workaround: Associated Type + Marker Structs
I discovered a solution in this Rust forum post, and I believe it deserves more visibility.
The key idea is to use:
- Marker structs like
Unified<T>
andPartitioned<T>
to wrap adapter types. - A helper trait,
BlanketAdapter
, implemented for each marker type. - An associated type in the
Adapter
trait to delegate behavior.
Step 1: Marker Structs
use PhantomData;
;
;
These zero-sized types are used solely for type-level dispatch.
Step 2: The BlanketAdapter
Trait
And the implementations:
Now we have non-conflicting blanket impls because they apply to different types (Unified<A>
vs. Partitioned<A>
).
Step 3: The Adapter
Trait with Associated Type
The key piece: the associated type Target
tells Adapter
whether to delegate to Unified<Self>
or Partitioned<Self>
.
Usage Example
Let's say we need to implement a JsonAdapter
that writes everything to a single file.
It can be implemented as a UnifiedAdapter
:
No code duplication. No conflicts. The only overhead is 3 extra lines to link things together
Final Thoughts
This pattern - using marker types + associated types - gives you the flexibility of alternative blanket implementations while staying within Rust's coherence rules.
It's especially useful when you want to support mutually exclusive behaviors under a unified interface, without compromising on ergonomics.
Links
Psss! Are you looking for a passionate Rust dev?
My friend is looking for a job in Berlin or remote. Reach out to this guy.