Doug
Doug

Reputation: 35136

Why does rust require Sendable traits to explicitly be marked as Send?

I expected this to work:

trait Foo: Send { fn ok(&self) -> int; }
struct IsFoo;
impl Foo for IsFoo { fn ok(&self) -> int { return 1; } }
struct Bar { v: Option<Box<Foo>> }

fn main() {
  let bar = Bar { v: Some(box IsFoo as Box<Foo>) };
  spawn(proc() {
    match bar.v {
      Some(ref foo) => {
        assert!(foo.ok() == 1);
      }
      _ => { fail!("Unreachable"); }
    }
  });
}

playpen: http://is.gd/ORxeRL

...but it does not.

Instead, the compiler awkwardly requires the Send trait to be explicitly set on the trait references:

struct Bar { v: Option<Box<Foo + Send> + Send> } <--- + Send + Send much?

fn main() {
  let bar = Bar { v: Some(box IsFoo as Box<Foo + Send>) }; <--- 

Foo is Foo : Send; so... I obviously want it to be sendable. Why requirement for the explicit + Send's?

ie. Is it a compiler bug?

Or is there some justification for requiring this verbosity?

Upvotes: 0

Views: 288

Answers (1)

huon
huon

Reputation: 102066

Yes, this is a bug, e.g #15155.

(For future reference, that was the first result when searching for trait object send in the rust-lang/rust repo ; it's often the case that confusing/buggy behaviour will have been encountered before, so you can save yourself some time and confusion by doing a quick search.)

Upvotes: 2

Related Questions