---
title: "Associated types vs. generics in a low-level I/O driver API"
description: "Utilizing associated types in Rust traits to design flexible, type-safe APIs for low-level I/O drivers and comparing advantages over generic type parameters"
url: "https://mayorana.ch/en/blog/associated-types-io-driver-api"
language: "en"
date: "2025-08-16"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "associated-types"
  - "traits"
  - "io-drivers"
  - "type-safety"
  - "embedded"
fr_version: "https://mayorana.ch/fr/blog/associated-types-io-driver-api.md"
---

# Associated types vs. generics in a low-level I/O driver API

In a low-level I/O driver for an embedded system, I'd use associated types in a Rust trait to define a flexible, type-safe API that ties specific input/output types to each driver implementation. Unlike generic type parameters, associated types provide a cleaner, more constrained design, enhancing clarity and maintaining performance. Here's how I'd do it with an example.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td8-fig" viewBox="0 0 800 280" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Associated types locking UartDriver Input and Output to u8 versus generic methods that monomorphize per call type">
<style>
.td8-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td8-fig,[data-theme="dark"] .td8-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td8-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td8-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td8-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td8-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>
</defs>
<!-- column titles -->
<text x="200" y="26" class="ti">Associated types</text>
<text x="600" y="26" class="ti">Generic parameters</text>
<!-- left column -->
<rect class="boxAc" x="60" y="45" width="280" height="50" rx="6"/>
<text x="200" y="67" class="tx">type Input = u8; type Output = u8;</text>
<text x="200" y="84" class="mut">fixed per driver</text>
<path class="ln" d="M200,95 L200,121" marker-end="url(#td8-arrow)"/>
<rect class="box" x="60" y="122" width="280" height="46" rx="6"/>
<text x="200" y="145" class="tx">write(&amp;mut self, u8)</text>
<text x="200" y="160" class="mut">one impl, no bounds needed</text>
<path class="ln" d="M200,168 L200,194" marker-end="url(#td8-arrow)"/>
<rect class="box" x="60" y="195" width="280" height="46" rx="6"/>
<text x="200" y="218" class="tx">inlined mov to register</text>
<text x="200" y="233" class="mut">zero conversion cost</text>
<!-- right column -->
<rect class="box" x="460" y="45" width="280" height="50" rx="6"/>
<text x="600" y="67" class="tx">write&lt;T&gt;(&amp;mut self, data: T)</text>
<text x="600" y="84" class="mut">T could be anything</text>
<path class="ln" d="M600,95 L600,121" marker-end="url(#td8-arrow)"/>
<rect class="box" x="460" y="122" width="280" height="46" rx="6"/>
<text x="600" y="145" class="tx">T: Into&lt;u8&gt; bound needed</text>
<text x="600" y="160" class="mut">conversion overhead</text>
<path class="ln" d="M600,168 L600,194" marker-end="url(#td8-arrow)"/>
<rect class="box" x="460" y="195" width="280" height="46" rx="6"/>
<text x="600" y="218" class="tx">write&lt;u8&gt;, write&lt;i32&gt;, ...</text>
<text x="600" y="233" class="mut">monomorphization bloat</text>
<!-- captions -->
<text x="400" y="265" class="mut">UartDriver locks Input/Output to u8 — incompatible types rejected at compile time</text>
</svg>
</div>

## Designing the Trait with Associated Types

For an I/O driver handling hardware interfaces (e.g., UART, SPI), I'd define a trait like this:

```rust
trait IoDriver {
    type Input;  // Data type to write
    type Output; // Data type to read

    fn write(&mut self, data: Self::Input) -> Result<(), ()>;
    fn read(&mut self) -> Result<Self::Output, ()>;
}
```

### Associated Types:
- **Input**: The type the driver accepts for writing (e.g., `u8` for bytes, `[u8]` for buffers).
- **Output**: The type returned from reading (e.g., `u8`, `Option<u8>`).

**Why**: Each driver fixes its I/O types, ensuring type safety and a clear contract without per-call flexibility.

## Implementation: UART Driver

For a UART (serial) driver that sends and receives single bytes:

```rust
struct UartDriver {
    // Hardware state (simplified)
    buffer: u8,
}

impl IoDriver for UartDriver {
    type Input = u8;   // Writes single bytes
    type Output = u8;  // Reads single bytes

    fn write(&mut self, data: u8) -> Result<(), ()> {
        self.buffer = data;
        Ok(()) // Simulate hardware write
    }

    fn read(&mut self) -> Result<u8, ()> {
        Ok(self.buffer) // Simulate hardware read
    }
}

// Usage
let mut uart = UartDriver { buffer: 0 };
uart.write(42).unwrap();
assert_eq!(uart.read(), Ok(42));
```

## Comparison with Generic Type Parameters

Here's how it might look with generics instead:

```rust
trait GenericIoDriver {
    fn write<T>(&mut self, data: T) -> Result<(), ()>;
    fn read<T>(&mut self) -> Result<T, ()>;
}

impl GenericIoDriver for UartDriver {
    fn write<T>(&mut self, data: T) -> Result<(), ()> {
        // Problem: T could be anything—how to handle it?
        // Maybe restrict with a bound, but still unclear
        unimplemented!()
    }
    fn read<T>(&mut self) -> Result<T, ()> {
        unimplemented!()
    }
}
```

### Issues:
- **T is too flexible**—`write` might get a `String` or `i32`, but UART expects `u8`. Bounds like `T: Into<u8>` add conversion overhead and complexity.
- **Monomorphization** generates code for each `T`, bloating the binary unnecessarily.

## Advantages of Associated Types

### Type Safety

**Associated Types**: `UartDriver` locks `Input` and `Output` to `u8`. Callers can't pass incompatible types:

```rust
uart.write("hello"); // Compile error: expected u8, got &str
```

**Generics**: Requires runtime checks or complex bounds, risking errors or overhead.

### Design Clarity

**Associated Types**: The trait declares "this driver works with these specific types," making intent explicit. `UartDriver` is byte-oriented, while an `SpiDriver` might use `[u8]`:

```rust
struct SpiDriver;
impl IoDriver for SpiDriver {
    type Input = [u8];  // Buffer writes
    type Output = [u8]; // Buffer reads
    fn write(&mut self, _data: [u8]) -> Result<(), ()> { Ok(()) }
    fn read(&mut self) -> Result<[u8], ()> { Ok([0; 4]) }
}
```

**Generics**: Intent is muddled—`T` could be anything per call, forcing implementors to handle or reject types dynamically.

### Performance

**Associated Types**: [Static dispatch](https://mayorana.ch/en/blog/dispatch-performance-rust) with one implementation per driver. `write` and `read` inline directly to hardware ops (e.g., `mov` to a register), no conversion or dispatch overhead.

**Generics**: Monomorphizes for each `T` used, increasing code size (e.g., `write<u8>`, `write<i32>`), even if the driver only supports one type. Bounds like `T: Into<u8>` add runtime calls.

## Enhancing the System

### Generic Usage

Wrap in a generic function for convenience:

```rust
fn process_io<D: IoDriver>(driver: &mut D, input: D::Input) -> D::Output {
    driver.write(input).unwrap();
    driver.read().unwrap()
}
let mut uart = UartDriver { buffer: 0 };
let result = process_io(&mut uart, 42); // Works with u8
```

<div class="svg-container" style="margin:2rem 0;">
<svg class="td8b-fig" viewBox="0 0 800 295" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Each driver impl fills the Input and Output holes of IoDriver, and process_io resolves D::Input differently per driver at the call site">
<style>
.td8b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td8b-fig,[data-theme="dark"] .td8b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td8b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td8b-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td8b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8b-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8b-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td8b-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td8b-fig .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td8b-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="td8b-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>
<!-- the trait, with two holes -->
<rect class="boxAc" x="250" y="30" width="300" height="50" rx="6"/>
<text x="400" y="52" class="tx">trait IoDriver { type Input; type Output }</text>
<text x="400" y="70" class="mut">two holes, filled once per driver</text>
<!-- split to the two impls -->
<path class="ln" d="M400,80 L400,96"/>
<path class="ln" d="M400,96 L210,96 L210,113" marker-end="url(#td8b-arrow)"/>
<path class="ln" d="M400,96 L590,96 L590,113" marker-end="url(#td8b-arrow)"/>
<!-- impl 1 -->
<rect class="box" x="60" y="115" width="300" height="52" rx="6"/>
<text x="210" y="137" class="tx">impl IoDriver for UartDriver</text>
<text x="210" y="155" class="mut">Input = u8, Output = u8</text>
<path class="lnAc" d="M210,167 L210,193" marker-end="url(#td8b-arrowAc)"/>
<!-- impl 2 -->
<rect class="box" x="440" y="115" width="300" height="52" rx="6"/>
<text x="590" y="137" class="tx">impl IoDriver for SpiDriver</text>
<text x="590" y="155" class="mut">Input = [u8], Output = [u8]</text>
<path class="ln" d="M590,167 L590,193" marker-end="url(#td8b-arrow)"/>
<!-- call site 1 -->
<rect class="boxAc" x="60" y="195" width="300" height="52" rx="6"/>
<text x="210" y="217" class="tx">process_io(&amp;mut uart, 42)</text>
<text x="210" y="235" class="mut">D::Input reads as u8 here</text>
<!-- call site 2 -->
<rect class="box" x="440" y="195" width="300" height="52" rx="6"/>
<text x="590" y="217" class="tx">process_io(&amp;mut spi, buf)</text>
<text x="590" y="235" class="mut">D::Input reads as [u8] here</text>
<!-- caption -->
<text x="400" y="277" class="mut">One signature, two meanings of D::Input — the driver picks the type, never the caller</text>
</svg>
</div>

### Flexibility

Add associated types for errors or configs if needed (e.g., `type Error`).

## Checking it compiles as intended
### Compile Check

Ensure type mismatches fail:

```rust
uart.write([1, 2, 3]); // Error: expected u8, got [i32; 3]
```

### Benchmark

Use criterion to confirm no overhead:

```rust
use criterion::{black_box, Criterion};
fn bench(c: &mut Criterion) {
    let mut uart = UartDriver { buffer: 0 };
    c.bench_function("uart_write", |b| b.iter(|| uart.write(black_box(42))));
}
```

Expect minimal cycles, matching raw hardware access.

I'd use associated types in `IoDriver` to fix `Input` and `Output` per driver, as with `UartDriver`, ensuring type safety and a clear API over generics' over-flexibility. This avoids monomorphization bloat and runtime conversions, delivering efficient, inlined code for an embedded I/O system. This design balances usability and performance, leveraging Rust's type system for robust drivers.
