Blanket impls¶
Level: 301 · deep dive
One line: impl<T: Display> ToString for T implements a trait for every type that meets a bound — one impl, infinitely many types — and the coherence rules exist so that two such impls can never both apply to the same type.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- Reading std's most-used blanket impls:
ToStringforDisplay,Intofor everyFrom,ToOwnedfor everyClone,impl<R: Read + ?Sized> Read for &mut R - Writing one:
impl<T: Iterator> MyExt for T— the extension trait pattern E0119conflicting implementations: why adding a blanket impl blocks every specific impl it overlaps with- Why a downstream crate may implement
From<Mine> for Theirsbut notimpl<T> Theirs for T— the orphan rule applied to generics (seeFromandInto) - Adding a blanket impl to a published crate as a breaking change — semver hazards
- Specialization: the unstable feature that would allow overlap, and why it is still unstable
The trap it exists for¶
Writing impl<T: Display> MyTrait for T and then impl MyTrait for MyType with a custom body. If MyType: Display — now or in any future version of your code — the two overlap and the second is E0119. The blanket impl claimed every displayable type, including yours.
Where this sits¶
Step 6: one blanket impl covers every Clone type walks through one blanket impl in detail. Extension traits is the most common reason to write one. This page is the rule for all of them.
See also¶
- Step 6: one blanket impl covers every
Clonetype — one blanket impl, read closely - Extension traits — the pattern that needs a blanket impl
FromandInto— the orphan rule, and theIntoyou get for free- "No method named …" —
E0599when a blanket impl's bound is not met - Where the bound goes — the bound that decides which types an impl covers
- Associated types or type parameters? — how the trait's shape limits its impls
If you are coming from another language¶
- Go. Interfaces are satisfied implicitly by method sets, so every type with
String() stringis aStringerwithout any impl — a blanket impl everywhere, with no way to add behaviour the type does not already have. - C++. A template specialization or a constrained function template reaches every type that meets a concept; overlapping ones are resolved by partial ordering, which Rust refuses rather than ranks.
- Java. A default method on an interface adds behaviour to every implementer, but only to classes that opt in by implementing it; Rust's blanket impl reaches types that never mentioned the trait.
Po polsku¶
Implementacja hurtowa (blanket impl), np. impl<T: Display> ToString for T, implementuje cechę dla każdego typu spełniającego ograniczenie. Reguły spójności (coherence) pilnują, żeby dwie takie implementacje nigdy nie pasowały do tego samego typu — stąd E0119, gdy po implementacji hurtowej dopisujesz konkretną dla typu, który też spełnia jej warunek.
Szukaj po polsku: implementacja hurtowa · spójność cech · rust blanket implementation · rust E0119 conflicting implementations