title: Sending trait objects between threads in Rust
body: <p>I'd like to send a trait object between threads, but can't figure out if it's possible. It seems like it might not be, as they apparently do not fulfill the <code>Send</code> trait.</p>
  
  <p>The following code demonstrates what I'm trying to do:</p>
  
  <pre><code>use std::{
      sync::mpsc::{channel, Receiver, Sender},
      thread,
  };
  
  trait Bar {
      fn bar(&amp;self);
  }
  
  struct Foo {
      foo: i32,
  }
  
  impl Bar for Foo {
      fn bar(&amp;self) {
          println!("foo: {}", self.foo);
      }
  }
  
  fn main() {
      let foo = Box::new(Foo { foo: 1 }) as Box&lt;dyn Bar&gt;;
  
      let (tx, rx): (Sender&lt;Box&lt;dyn Bar&gt;&gt;, Receiver&lt;Box&lt;dyn Bar&gt;&gt;) = channel();
  
      thread::spawn(move || {
          tx.send(foo).unwrap();
      });
  
      let sent = rx.recv().unwrap();
  
      sent.bar();
  }
  </code></pre>
  
  <p>This fails with the following message:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0277]: `dyn Bar` cannot be sent between threads safely
     --&gt; src/main.rs:25:5
      |
  25  |     thread::spawn(move || {
      |     ^^^^^^^^^^^^^ `dyn Bar` cannot be sent between threads safely
      |
      = help: the trait `std::marker::Send` is not implemented for `dyn Bar`
      = note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique&lt;dyn Bar&gt;`
      = note: required because it appears within the type `std::boxed::Box&lt;dyn Bar&gt;`
      = note: required because it appears within the type `[closure@src/main.rs:25:19: 27:6 tx:std::sync::mpsc::Sender&lt;std::boxed::Box&lt;dyn Bar&gt;&gt;, foo:std::boxed::Box&lt;dyn Bar&gt;]`
  </code></pre>
  
  <p>Trying to send a plain, unboxed trait object results in a bunch of other errors, mostly complaining about not fulfilling <code>Send + Sized</code>.</p>
  
  <p>I'm still fairly new to Rust, so I'm not sure if there's something I'm missing, but I get the impression that there isn't a way to convince the compiler to make the trait object <code>Send</code>.</p>
  
  <p>If it isn't currently possible, is there any work currently under way that may allow for this in the future?</p>