简体   繁体   English

Result<(), Box<(dyn SomeTrait + 'static)>> 不满足 Trait Bound

[英]Trait Bound is not satisfied for Result<(), Box<(dyn SomeTrait + 'static)>>

use once_cell::sync::OnceCell;

pub trait SomeTrait {}
pub struct Impl1 {}

impl SomeTrait for Impl1 {}

pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait>> = OnceCell::new();

pub fn main() {
    GLOBAL_THING.set(Box::new(Impl1 {})).unwrap();
}

I'm getting this error and not sure how to interpret it's meaning or fix it.我收到此错误,但不确定如何解释它的含义或修复它。

error[E0599]: the method `unwrap` exists for enum `Result<(), Box<(dyn SomeTrait + 'static)>>`, but its trait bounds were not satisfied
   --> src/main.rs:11:42
    |
11  |       GLOBAL_THING.set(Box::new(Impl1 {})).unwrap();
    |                                            ^^^^^^ method cannot be called on `Result<(), Box<(dyn SomeTrait + 'static)>>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `Box<dyn SomeTrait>: Debug`

An easy workaround is to just do if let Err() = GLOBAL_THING.set() {panic!(...)} instead of using unwrap() , but I'd like to understand what's going on here and fix if possible.一个简单的解决方法是if let Err() = GLOBAL_THING.set() {panic!(...)}而不是使用unwrap() ,但我想了解这里发生了什么并尽可能修复。

If you take a look at the unwrap method documentation you'll see that it's not defined for all Result s, but only ones where E: Debug :如果您查看unwrap方法文档,您会发现它不是为所有Result定义的,而是只为E: Debug

impl<T, E> Result<T, E>
where
    E: Debug, 
{
    pub fn unwrap(self) -> T;
}

It needs the error type E to implement Debug so that the error can be printed if unwrapping fails.它需要错误类型E来实现Debug以便在解包失败时可以打印错误。

The error message shows that the result type is Result<(), Box<(dyn SomeTrait + 'static)>> , so E = Box<(dyn SomeTrait + 'static)> .错误消息显示结果类型为Result<(), Box<(dyn SomeTrait + 'static)>> ,所以E = Box<(dyn SomeTrait + 'static)> You can make this error type debuggable by having SomeTrait: Debug , which requires that any type that implements SomeTrait must also implement Debug .您可以通过SomeTrait: Debug使此错误类型可SomeTrait: Debug ,这要求实现SomeTrait任何类型也必须实现Debug

pub trait SomeTrait: Debug {}

#[derive(Debug)]
pub struct Impl1 {}

Once you do this you'll hit the next error:执行此操作后,您将遇到下一个错误:

error[E0277]: `dyn SomeTrait` cannot be shared between threads safely
  --> src/main.rs:11:1
   |
11 | pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait>> = OnceCell::new();
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `dyn SomeTrait` cannot be shared between threads safely
   |
   = help: the trait `Sync` is not implemented for `dyn SomeTrait`
   = note: required because of the requirements on the impl of `Sync` for `Unique<dyn SomeTrait>`
   = note: required because it appears within the type `Box<dyn SomeTrait>`
   = note: required because of the requirements on the impl of `Sync` for `once_cell::imp::OnceCell<Box<dyn SomeTrait>>`
   = note: required because it appears within the type `once_cell::sync::OnceCell<Box<dyn SomeTrait>>`
   = note: shared static variables must have a type that implements `Sync`

To fix this you'll also want to make the boxed trait object Send + Sync so that it can be shared across threads.要解决此问题,您还需要使装箱的 trait 对象Send + Sync以便它可以跨线程共享。

pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait + Send + Sync>> = OnceCell::new();

You can see the final result on the Playground .您可以在Playground上看到最终结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM