3

I introduced error-chain into a previously working application. The error itself is clear, std::error::Error + 'static lacks an implementation of the trait std::marker::Send:

error[E0277]: the trait bound `std::error::Error + 'static: std::marker::Send` is not satisfied
  --> src/main.rs:35:5
   |
35 | /     error_chain!{
36 | |
37 | |         foreign_links {
38 | |             Mqttc(::mqttc::Error);
...  |
53 | |         }
54 | |     }
   | |_____^ `std::error::Error + 'static` cannot be sent between threads safely
   |
   = help: the trait `std::marker::Send` is not implemented for `std::error::Error + 'static`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<std::error::Error + 'static>`
   = note: required because it appears within the type `std::boxed::Box<std::error::Error + 'static>`
   = note: required because it appears within the type `mqttc::netopt::Error`
   = note: required because it appears within the type `mqttc::Error`
   = note: required because it appears within the type `errors::ErrorKind`
   = note: required because it appears within the type `errors::Error`
   = note: required by `error_chain::ChainedError`
   = note: this error originates in a macro outside of the current crate

I am not sure how to resolve this. Note that I am using the more up to date fork of mqttc/mqtt3 instead of the upstream crates.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
drahnr
  • 6,782
  • 5
  • 48
  • 75
  • You cannot share any object between threads. You must use a Mutex. – Boiethios Sep 29 '17 at 14:45
  • So essentially error-chain declares some static entities which I want to use in multpile threads. How do I handle this use case with error-chain? – drahnr Sep 29 '17 at 14:52
  • I do not know this crate, but maybe you should ask in an github issue? – Boiethios Sep 29 '17 at 15:50
  • @Boiethios The OP is not trying to share an object between multiple threads, but (according to the error message) to **send** (move) an object between threads. `Send` supported by almost any object in Rust, with the exception of the few specifically blacklisted ones, such as `Rc`. – user4815162342 Sep 30 '17 at 19:19

1 Answers1

2

mqttc::Error contains a mqttc::netopt::Error, which in turns contains a Box<std::error::Error> (which desugars to std::boxed::Box<std::error::Error + 'static>. std::error::Error here is a trait object. Because the Error trait doesn't have Send as a supertrait, implementations of Error are not required to implement Send. Therefore, Box<std::error::Error> doesn't implement Send because not all types T: std::error::Error implement Send.

This could be fixed by changing the mqttc::netopt::Error type to use Box<std::error::Error + Send> instead of Box<std::error::Error> (which would be a breaking change for the library).

Francis Gagné
  • 60,274
  • 7
  • 180
  • 155