Sub topic 14
Blanket Implementations
A blanket implementation implements a trait for every type that meets a bound. This chapter assumes you know trait bounds, generics, and the orphan rule from the earlier chapters.
The syntax
Normally you write impl Trait for Type. In a blanket implementation, the implementing type is a generic parameter with a bound:
use std::fmt::Display;
trait Describe {
fn describe(&self) -> String;
}
impl<T: Display + ?Sized> Describe for T {
fn describe(&self) -> String {
format!("<{}>", self)
}
}
fn main() {
println!("{}", 42_i32.describe());
println!("{}", "hi".describe());
println!("{}", String::from("ana").describe());
}This prints <42>, <hi>, and <ana>. You wrote one impl block, and i32, str, and String all gained describe. So does any type you define later, as long as it implements Display.
A real example from the standard library
The standard library uses this pattern for ToString. Roughly, it contains impl<T: Display + ?Sized> ToString for T. Because of this, you never implement ToString yourself. You implement Display, and to_string() comes with it.
Why `?Sized`
Every generic parameter has an implicit Sized bound. Without + ?Sized, the example above would not cover str or [T], because their sizes are not known at compile time. Add ?Sized when the trait method only takes &self, so the impl reaches unsized types too.
Rules and gotchas
- Overlap is an error. Once
impl<T: Display> Describe for Texists, you cannot writeimpl Describe for i32.i32implementsDisplay, so the two impls would overlap. Rust rejects this even if you meant the specific one to win. Rust has no specialization on stable. - Orphan rule. You can write a blanket impl only for a trait you define in your crate. Writing
impl<T: Display> Display for Tis not allowed. The same rule means you can't add a blanket impl of someone else's trait. - Adding one later can break users. If you publish a crate and add a new blanket impl to an existing trait, downstream types that implement the trait directly may now overlap. Treat it as a breaking change.
- Method name clashes. If a type has its own method called
describe, the inherent method wins over the trait method. UseDescribe::describe(&x)to call the trait version.
When to use one
Use a blanket impl when the new behavior can be built entirely from the bound. describe only needs Display, so it fits. If the behavior differs between types, use ordinary per-type impls instead.
Blanket impls are also a way to extend a trait with a helper layer. For example, a trait Ext can be implemented for every T: Iterator, which adds methods to all iterators at once. The standard library's Iterator methods and the itertools crate use this idea.
Exercise
You have trait Describe with impl<T: Display> Describe for T. You then add impl Describe for Vec<i32>. Does the program compile? Why or why not?
Show answer
It compiles. Vec<i32> does not implement Display, so it is not covered by the blanket impl and the two impls do not overlap. If the specific impl were for i32, it would fail with a conflicting implementations error, because i32 implements Display. One caveat is that Rust also considers whether Vec<i32> could implement Display in the future. That is only a concern for foreign types and traits. Here Vec and Display are both foreign, but only an upstream crate could add the impl, and Rust allows this case because adding the impl would be a breaking change in the standard library.