drahnr
drahnr

Reputation: 6886

Multithreaded application fails to compile with error-chain

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.

Upvotes: 3

Views: 169

Answers (1)

Francis Gagn&#233;
Francis Gagn&#233;

Reputation: 65927

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).

Upvotes: 2

Related Questions