Rust `dyn Trait` vs `impl Trait`: what to do when E0277 and E0038 show up
I've been slowly rebuilding a log shipper at work in Rust (1.79.0, edition 2021, rustup on a Debian 12 box). It reads from files, a Kafka topic, and the odd HTTP endpoint, then feeds everything into one pipeline. The obvious design was a Source trait with a read() method. The less obvious bit was whether to pass sources around as generics or as trait objects. I got that wrong first and lost a decent chunk of an afternoon to compiler errors.
## The version that compiled
Generics were what I reached for, mostly out of habit:
rust
trait Source {
fn read(&mut self) -> Vec<u8>;
}fn run<S: Source>(mut source: S) -> Vec<u8> {
source.read()
}
Fine, as long as run is called from one place with one concrete type. It stops being fine the moment you want a Vec of mixed sources, because every element has to have the same size and type. Our config file lets people list several inputs, so I needed heterogeneity.
## Where dyn bit me
So I swapped to Box<dyn Source>:
rust
fn run(mut source: Box<dyn Source>) -> Vec<u8> {
source.read()
}
and immediately got this:
error[E0277]: the size for values of type dyn Source cannot be known at compilation time
--> src/pipeline.rs:14:14
|
14 | fn run(mut source: Box<dyn Source>) -> Vec<u8> {
| ^^^^^^^ doesn't have a known size
|
= help: the trait Sized is not implemented for dyn Source
dyn Source is unsized by definition, so it always lives behind a pointer: Box, Arc, or a reference. I was already doing that. The real problem was a second call site that still had an impl Source signature and expected coercion from a bare value. Making both signatures agree cleared the E0277. Compiler was right, I just read the first line and got annoyed.
## Object safety is the part that matters
The worse one came when I tried to add a generic method:
error[E0038]: the trait Source cannot be made into an object
--> src/source.rs:7:8
|
7 | pub trait Source {
| ^^^^^^ Source cannot be made into an object
|
note: a trait with a generic method cannot be made into a trait object
I had fn parse<T: DeserializeOwned>(&self) -> T hanging off it. A trait object erases the concrete type, so there's nothing for a generic method to dispatch against. I moved parse out to a free function. The enum_dispatch crate is an option if you own every implementation and want static dispatch over a closed set, but it won't help with plugins you don't control.
Async was the other trap. async fn in traits is object-safe from Rust 1.75, but the lifetime story is easy to mess up. On 1.79 I still use async-trait = "0.1.80" when the trait has to be a dyn object and just eat the allocation.
## What I use now
If there's one concrete type at the call site, impl Trait in the argument and let monomorphization handle it. If I need a collection, a plugin boundary, or config-driven types, Box<dyn Trait> or Arc<dyn Trait>. Keep the trait object-safe: no generic methods, no Self: Sized surprises, no associated constants you'd have to restate everywhere.
The shipper has been up for three months. The plugin boundary is Arc<dyn Source + Send + Sync> and the inner loop is still generic, which turned out to be the split I wanted. Send + Sync only showed up because tokio refused the future. Different error, different afternoon. If you know a cleaner way to keep a hot loop generic over a dynamic set of sources, I'm all ears.