---
title: "Blanket implementations: one impl for every type that qualifies"
description: "Employing blanket implementations in Rust to minimize code duplication for maintainable libraries"
url: "https://mayorana.ch/en/blog/blanket-implementations-coherence"
language: "en"
date: "2025-08-17"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "blanket-implementations"
  - "trait-coherence"
  - "code-duplication"
  - "traits"
  - "library-design"
fr_version: "https://mayorana.ch/fr/blog/blanket-implementations-coherence.md"
---

# Blanket implementations: one impl for every type that qualifies

In a Rust library providing utility functions, use a blanket implementation to automatically apply a trait to all types that satisfy a given constraint.

This streamlines the API but requires careful handling of trait coherence to avoid conflicts.

Here's how I'd do it with an example.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td9-fig" viewBox="0 0 800 260" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Vec i32 and Vec f64 implementing Summable converge through one blanket impl into an automatically granted Stats trait">
<style>
.td9-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td9-fig,[data-theme="dark"] .td9-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td9-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td9-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td9-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td9-fig .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td9-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
<marker id="td9-arrowAc" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ac)"/></marker>
</defs>
<!-- inputs -->
<rect class="box" x="40" y="30" width="230" height="46" rx="6"/>
<text x="155" y="58" class="tx">impl Summable for Vec&lt;i32&gt;</text>
<rect class="box" x="40" y="110" width="230" height="46" rx="6"/>
<text x="155" y="138" class="tx">impl Summable for Vec&lt;f64&gt;</text>
<!-- Y-merge -->
<path class="ln" d="M270,53 L310,53 L310,88" marker-end="none"/>
<path class="ln" d="M270,133 L310,133 L310,88" marker-end="none"/>
<path class="lnAc" d="M310,88 L360,88" marker-end="url(#td9-arrowAc)"/>
<!-- blanket impl -->
<rect class="boxAc" x="362" y="60" width="280" height="56" rx="6"/>
<text x="502" y="83" class="tx">impl&lt;T: Summable&gt; Stats for T</text>
<text x="502" y="100" class="mut">fn mean(&amp;self) -&gt; f64</text>
<!-- arrow to result -->
<path class="ln" d="M642,88 L668,88" marker-end="url(#td9-arrow)"/>
<rect class="box" x="670" y="63" width="100" height="50" rx="6"/>
<text x="720" y="83" class="tx">.mean()</text>
<text x="720" y="99" class="mut">for free</text>
<!-- caption -->
<text x="400" y="170" class="mut">One blanket impl replaces separate Stats impls for every Summable type</text>
<text x="400" y="200" class="mut">Sealed trait guards prevent downstream coherence conflicts</text>
</svg>
</div>

## Using Blanket Implementation

**Scenario**: A library offers data processing utilities, including a `Summable` trait for types that can be summed (e.g., numbers, vectors). I want to add a `Stats` trait for computing statistics (e.g., mean) on any `Summable` type without writing repetitive implementations.

### Traits and Blanket Implementation:

```rust
use std::ops::Add;

// Trait for types that can be summed
trait Summable {
    type Output;
    fn sum(&self) -> Self::Output;
}

// Trait for statistical operations
trait Stats {
    fn mean(&self) -> f64;
}

// Blanket implementation
impl<T> Stats for T
where
    T: Summable,
    T::Output: Into<f64>,
{
    fn mean(&self) -> f64 {
        let sum = self.sum().into();
        sum / (self.len() as f64)
    }
}

// Helper trait for length (simplified)
trait Len {
    fn len(&self) -> usize;
}
impl<T> Len for Vec<T> {
    fn len(&self) -> usize { self.len() }
}

// Example implementations
impl Summable for Vec<i32> {
    type Output = i32;
    fn sum(&self) -> i32 {
        self.iter().sum()
    }
}

impl Summable for Vec<f64> {
    type Output = f64;
    fn sum(&self) -> f64 {
        self.iter().sum()
    }
}

// Usage
let numbers = vec![1, 2, 3, 4, 5];
let mean = numbers.mean(); // 3.0
let floats = vec![1.5, 2.5, 3.5];
let mean_f = floats.mean(); // 2.5
```

## How It Reduces Code Duplication

- **Single Implementation**: The blanket `impl<T: Summable>` applies `Stats` to any type implementing `Summable` (e.g., `Vec<i32>`, `Vec<f64>`). Without it, I'd need separate `impl Stats for Vec<i32>`, `impl Stats for Vec<f64>`, etc., duplicating the mean logic.
- **Scalability**: Adding a new `Summable` type (e.g., `Vec<u64>`) automatically grants `Stats` without touching the library code.
- **Clarity**: Users get `mean` for free on any `Summable` type, simplifying the API.

## Trait Coherence and Pitfalls

Trait coherence ensures no two conflicting trait implementations exist for the same type.

Rust's orphan rules enforce this: you can only implement a trait for a type if either the trait or the type is defined in your crate.

Blanket implementations amplify coherence risks:

### 1. Accidental Overlap

**Problem**: If another crate defines `impl Stats for Vec<i32>`, it conflicts with the blanket `impl<T: Summable> Stats for T` if `Vec<i32>: Summable`.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td9b-fig" viewBox="0 0 800 320" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Two candidate Stats impls for Vec i32 meeting at the coherence check and producing error E0119">
<style>
.td9b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td9b-fig,[data-theme="dark"] .td9b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td9b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td9b-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td9b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9b-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9b-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td9b-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td9b-fig .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td9b-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
<marker id="td9b-arrowAc" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ac)"/></marker>
</defs>
<!-- title -->
<text x="400" y="26" class="ti">Two impls, one (type, trait) pair</text>
<!-- your crate -->
<rect class="box" x="40" y="45" width="300" height="56" rx="6"/>
<text x="190" y="68" class="tx">your crate: impl&lt;T: Summable&gt; Stats for T</text>
<text x="190" y="87" class="mut">already covers Vec&lt;i32&gt;</text>
<!-- other crate -->
<rect class="box" x="460" y="45" width="300" height="56" rx="6"/>
<text x="610" y="68" class="tx">other crate: impl Stats for Vec&lt;i32&gt;</text>
<text x="610" y="87" class="mut">writable while Stats stays open</text>
<!-- Y-merge into the coherence check -->
<path class="ln" d="M190,101 L190,125"/>
<path class="ln" d="M610,101 L610,125"/>
<path class="ln" d="M190,125 L610,125"/>
<path class="lnAc" d="M400,125 L400,150" marker-end="url(#td9b-arrowAc)"/>
<!-- coherence check -->
<rect class="box" x="250" y="152" width="300" height="52" rx="6"/>
<text x="400" y="175" class="tx">resolving Vec&lt;i32&gt;: Stats</text>
<text x="400" y="193" class="mut">two candidates, no tie-break rule</text>
<!-- outcome -->
<path class="lnAc" d="M400,204 L400,230" marker-end="url(#td9b-arrowAc)"/>
<rect class="boxAc" x="250" y="232" width="300" height="52" rx="6"/>
<text x="400" y="255" class="tx">error[E0119]</text>
<text x="400" y="273" class="mut">conflicting implementations</text>
<!-- caption -->
<text x="400" y="308" class="mut">Sealing Stats behind a private supertrait deletes the right-hand branch: that impl becomes unwritable</text>
</svg>
</div>

**Mitigation**: Make `Stats` a sealed trait (non-public or with a private [supertrait](https://mayorana.ch/en/blog/supertraits-bounds-optimization)) to prevent external implementations:

```rust
mod private {
    pub trait Sealed {}
}
trait Stats: private::Sealed {
    fn mean(&self) -> f64;
}
impl<T: Summable + private::Sealed> Stats for T { /* ... */ }
impl<T> private::Sealed for Vec<T> {} // Only Vec<T> allowed
```

Only types I explicitly mark with `Sealed` get the blanket `Stats`.

### 2. Downstream Conflicts

**Problem**: A user's crate adds `impl Summable for Vec<String>`, expecting `Stats`, but `String` doesn't implement `Into<f64>`, causing a compile error.

**Mitigation**: Clearly document bounds (e.g., "T::Output must implement Into<f64>") and test with diverse types. Alternatively, split `Stats` into narrower traits (e.g., `NumericStats`) to constrain applicability.

### 3. Orphan Rule Violations

**Problem**: If `Stats` and `Summable` are in different crates, the blanket impl might violate orphan rules unless one is local.

**Mitigation**: Define both traits in the same crate, or use newtype wrappers for foreign types.

### 4. Performance Bloat

**Problem**: The blanket impl monomorphizes `mean` for each `T`, potentially increasing code size.

**Mitigation**: Profile with `size target/release/lib` and consider `dyn Stats` for [dynamic dispatch](https://mayorana.ch/en/blog/dispatch-performance-rust) if code size grows excessively, though this adds vtable overhead.

## Enhancing the Design

- **Flexibility**: Add [associated types](https://mayorana.ch/en/blog/associated-types-io-driver-api) or methods to `Stats` for more stats (e.g., variance), reusing `Summable`'s sum.
- **Generality**: Extend `Len` to other collections (e.g., `[T]`, `VecDeque<T>`).
- **Safety**: Use where clauses to enforce invariants (e.g., non-empty collections).

## Checking the expansion
### Tests

Ensure blanket applies correctly:

```rust
let v = vec![1, 2, 3];
assert_eq!(v.mean(), 2.0);
let f = vec![1.0, 2.0, 3.0];
assert_eq!(f.mean(), 2.0);
```

### Size Check

`cargo build --release; size target/release/lib` to monitor binary growth.

### Compile Errors

Test invalid types (e.g., `Vec<String>`) to confirm coherence.

## When a blanket impl is worth it
I'd use a blanket `impl<T: Summable> Stats for T` to give `mean` to all `Summable` types, as shown to avoid duplications. This delivers a concise, safe API with minimal performance cost, leveraging Rust's type system.
