Hiding + Send + Sync + 'static noise

Probably like a lot of people, I structure Rust apps with "services" that are trait implementations (which I can swap mock implementations for for testing).

Because anytime you introduce dyn Trait, or any form of threading, your traits must be Send + Sync + 'static, we start to see that boilerplate everywhere.

So I'd like to encapsulate it.

It seems that creating a new type (for each trait) to do this - any alias - isn't possible.

I came up with this pattern (ignore the 'FF' name):

pub trait FF: FileFetcher + Send + Sync + 'static {}

pub fn new<F: FF>() ...

is there a better way?

Creating a subtrait with a blanket implementation is the best way I know on stable.

Trait alias is the unstable feature.

3 Likes

We need Trait alias! Looking forward to its graduation.