# Sending trait objects between threads

**URL:** <https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374>\
**Category:** help\
**Created:** [August 10, 2015, 6:03am UTC](https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374 "2015-08-10T06:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirma](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@amirma](https://users.rust-lang.org/u/amirma)\
**Post date:** [August 10, 2015, 6:03am UTC](https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374/1 "2015-08-10T06:03:18Z")

</div>

I tried to send a trait object like the following:

```
trait Foo
{
    fn foo(&self);
}

struct Baz 
{
    pub data : Box<Foo>
}

fn Bar(baz : Baz) {
    thread::spawn(move || {baz.data.foo()});
}

```

Which gave me this error

```
'error: the trait `core::marker::Send` is not implemented for the type `TFoo` [E0277]

```

This is not surprising because (and please correct me if I'm wrong), the compiler does not know if the actual underlying object of the trait object Foo is Send or not. So the question is, is there a way to send a trait object between threads?

---

<div class="post-metadata">

**Author:** ![gkoz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gkoz/32/319_2.png) [@gkoz](https://users.rust-lang.org/u/gkoz)\
**Post date:** [August 10, 2015, 6:13am UTC](https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374/2 "2015-08-10T06:13:38Z")

</div>

```rust
pub data : Box<Foo + Send>

```

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gankra/32/154_2.png) [@Gankra](https://users.rust-lang.org/u/Gankra)\
**Post date:** [August 10, 2015, 6:16am UTC](https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374/3 "2015-08-10T06:16:24Z")

</div>

You can require trait objects implement Send or Sync via `+ Send` or `+ Sync`. In this case, `pub data: Box<Foo + Send>`.

This is sadly a pervasive decision: everywhere you expect a trait object you need to choose if you require it to be Send or Sync upfront, and once it's been trait-ified you can't upgrade to `+ Send`.

```rust
use std::thread;

trait Foo {
    fn foo(&self);
}

struct Baz {
    pub data : Box<Foo + Send>
}

fn Bar(baz : Baz) {
    thread::spawn(move || {baz.data.foo()});
}

fn main() {}

```

> **[Rust Playground](https://play.rust-lang.org/?code=use%20std%3A%3Athread%3B%0A%0Atrait%20Foo%20%7B%0A%20%20%20%20fn%20foo%28%26self%29%3B%0A%7D%0A%0Astruct%20Baz%20%7B%0A%20%20%20%20pub%20data%20%3A%20Box%3CFoo%20%2B%20Send%3E%0A%7D%0A%0Afn%20Bar%28baz%20%3A%20Baz%29%20%7B%0A%20%20%20%20thread%3A%3Aspawn%28move%20%7C%7C%20%7Bbaz.data.foo%28%29%7D%29%3B%0A%7D%0A%0Afn%20main%28%29%20%7B%7D&version=stable)**
>
> A browser interface to the Rust compiler to experiment with the language

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [July 3, 2022, 2:39pm UTC](https://users.rust-lang.org/t/sending-trait-objects-between-threads/2374/4 "2022-07-03T14:39:08Z")

</div>

Closed on July 3, 2022, 2:39pm UTC


