Cla*_*nda 2 abstraction traits dynamic-dispatch rust
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();
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误,但不知道如何解释它的含义或修复它。
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`
Run Code Online (Sandbox Code Playgroud)
一个简单的解决方法是直接执行if let Err() = GLOBAL_THING.set() {panic!(...)}而不是使用unwrap(),但我想了解这里发生的情况并在可能的情况下进行修复。
如果您查看unwrap方法文档,您会发现它并未针对所有Results 进行定义,而仅针对以下情况进行定义E: Debug:
impl<T, E> Result<T, E>
where
    E: Debug, 
{
    pub fn unwrap(self) -> T;
}
Run Code Online (Sandbox Code Playgroud)
E它需要实现错误类型Debug,以便在解包失败时可以打印错误。
错误消息显示结果类型为Result<(), Box<(dyn SomeTrait + 'static)>>,所以E = Box<(dyn SomeTrait + 'static)>。您可以通过具有来使此错误类型可调试SomeTrait: Debug,这要求任何实现的类型SomeTrait也必须实现Debug。
impl<T, E> Result<T, E>
where
    E: Debug, 
{
    pub fn unwrap(self) -> T;
}
Run Code Online (Sandbox Code Playgroud)
执行此操作后,您将遇到下一个错误:
pub trait SomeTrait: Debug {}
#[derive(Debug)]
pub struct Impl1 {}
Run Code Online (Sandbox Code Playgroud)
要解决此问题,您还需要创建装箱特征对象Send + Sync,以便可以在线程之间共享它。
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`
Run Code Online (Sandbox Code Playgroud)
您可以在Playground上看到最终结果。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1212 次  |  
        
|   最近记录:  |