多线程应用程序无法通过错误链进行编译 [英] Multithreaded application fails to compile with error-chain

查看:98
本文介绍了多线程应用程序无法通过错误链进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将error-chain引入了以前可以运行的应用程序中.错误本身很明显,std::error::Error + 'static缺少trait std::marker::Send的实现:

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

我不确定该如何解决.请注意,我正在使用更多的最新的mqttc/mqtt3 分支,而不是上游的板条箱

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.

推荐答案

mqttc::Error包含一个mqttc::netopt::Error,它依次包含一个Box<std::error::Error>(对std::boxed::Box<std::error::Error + 'static>无效).std::error::Error这里是一个特征对象.因为Error特征没有Send作为超特征,所以不需要Error的实现来实现Send.因此,Box<std::error::Error>不能实现Send,因为不是所有类型的实施Send.

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.

这可以通过将mqttc::netopt::Error类型更改为使用Box<std::error::Error + Send>而不是Box<std::error::Error>来解决(这对于库来说是一个重大更改).

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

这篇关于多线程应用程序无法通过错误链进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆