Using Arc with Traits


I have two structs implementing the same trait and I want to use these structs inside a thread, for example by utilizing Arc. The code is something similar to:

use std::thread;
use std::sync::Arc;

pub trait DoesSomething {

pub struct DoerA;
pub struct DoerB;

impl DoesSomething for DoerA {

impl DoesSomething for DoerB {

fn main() {
    let data: Arc<DoesSomething> = Arc::new(DoerA);
    let data_clone = data.clone();
    thread::spawn(move || {
        data_clone;  // do something useful

This fails because Send and Sync are not implemented for the type DoesSomething.

Now, if I replace let data: Arc<DoesSomething> = Arc::new(DoerA) with let data: Arc<DoerA> = Arc::new(DoerA) everything starts working fine. The problem is, at this point the actual struct (whether it’s DoerA or DoerB) is decided earlier and I don’t want to tie myself to a particular implementation of the DoesSomething trait just for concurrency.

What would be the recommended approach here? Is there any nice way to abstract away from the implementation of the trait inside threads?


I haven’t tested, but does telling DoesSomething to require a type implementation that impls Sync and Send work?

trait DoesSomething: Sync + Send {}


That does the trick, thanks!