multithreading - 多线程应用程序无法使用错误链进行编译

标签 multithreading rust

我将 error-chain 引入了一个以前工作的应用程序。错误本身很明显,std::error::Error + 'static 缺少 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

我不知道如何解决这个问题。请注意,我使用的是更多 up to date fork of mqttc/mqtt3而不是上游箱子。

最佳答案

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 因为不是所有类型 T: std::error::Error 都实现 Send

这可以通过更改 mqttc::netopt::Error 来解决。要使用的类型 Box<std::error::Error + Send>而不是 Box<std::error::Error> (这对图书馆来说是一个重大改变)。

关于multithreading - 多线程应用程序无法使用错误链进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491293/

相关文章:

java - AtomicInteger 的嵌套方法调用在 java 中也是原子的吗

c# 事件执行是线程安全的吗?

java - 线程调度器注册?

rust - 为什么通过提取方法进行重构会触发借用检查器错误?

rust - 如何解析基于 LLVM MCJIT 的 JIT 中的当前进程符号?

rust - 为什么 Cow 的定义在 Owned(<B as ToOwned>::Owned) 中使用 "as"?

rust - 如何在 Rust 的稳定版本上使用 serde

c# - ObjectPool 实现死锁

rust - 为什么当我推送的元素多于 Vec 的容量时没有错误?

c# - 线程亲和性