简体   繁体   English

如何克隆盒装特征错误(框<dyn error> )?</dyn>

[英]How to clone a boxed trait Error ( Box<dyn Error> )?

I'm new to rust, I am using using clonable futures using the shared() method on a future that returns Result<T, Box<dyn Error>> .我是 rust 的新手,我在返回Result<T, Box<dyn Error>>的未来上使用 shared() 方法使用可克隆期货。 However, Clone is not implemented for dyn Error.但是,克隆并未针对 dyn 错误实现。 I tried creating my own custom error type ( CustomError ) that wraps Box<dyn Error> instead and tried to implement Clone on that but it still doesn't work.我尝试创建自己的自定义错误类型( CustomError )来包装Box<dyn Error>并尝试在其上实现 Clone 但它仍然不起作用。 I feel like I am missing something trivial here.我觉得我在这里遗漏了一些微不足道的东西。

Here's what I tried这是我尝试过的

use std::error::Error;  

#[derive(Debug)]  
pub struct CustomError(pub Box<dyn Error>);  

impl Clone for CustomError{  
    fn clone(&self) -> Self {  
        CustomError(Box::*new*(self.0.clone())) // doesn't work due to unsatisfied trait bounds, what else can I do?
    }  
}

Here's the error I get if I try to use Box<dyn Error> as my return type when calling shared() on the future.这是我在将来调用 shared() 时尝试使用Box<dyn Error>作为返回类型时得到的错误。 Any solutions to this?有什么解决办法吗?

error[E0277]: the trait bound `(dyn StdError + 'static): Clone` is not satisfied
  --> src/scrapers/form_4_xml_scraper.rs:56:52
   |
56 |             let document_fut = self.scrape(filing).shared();
   |                                                    ^^^^^^ the trait `Clone` is not implemented for `(dyn StdError + 'static)`
   |
   = note: required because of the requirements on the impl of `Clone` for `Box<(dyn StdError + 'static)>`
   = note: 1 redundant requirements hidden
   = note: required because of the requirements on the impl of `Clone` for `std::result::Result<(Filing, Form4XMLDocument), Box<(dyn StdError + 'static)>>

For anyone else wondering, I used Arc instead of Box and it worked perfectly.对于其他想知道的人,我使用Arc而不是Box ,并且效果很好。

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

相关问题 如何克隆 Vec <Box<dyn Trait> &gt;? - How can I clone a Vec<Box<dyn Trait>>? 你如何创建一个盒子<dyn Trait> ,还是一般的盒装未大小值? - How do you create a Box<dyn Trait>, or a boxed unsized value in general? 尝试将 Boxed dyn Trait 传递到 Function 时出现“借来的值不够长”的错误 - Getting “borrowed value does not live long enough” Error While Trying to Pass a Boxed dyn Trait into a Function 如何转换盒子<dyn Error + Sync + Send>到盒子<dyn Error> - How to convert Box<dyn Error + Sync + Send> to Box<dyn Error> 你如何转换一个盒子<dyn trait>到 Rc<dyn trait> ?</dyn></dyn> - How do you convert a Box<dyn Trait> to a Rc<dyn Trait>? 创建 Box 时如何转换为 dyn Trait<dyn trait></dyn> - How to cast to dyn Trait when creating a Box<dyn Trait> 如何测试 Box 中错误的类型<dyn Error> ? - How to test the type of an error in Box<dyn Error>? 如何克隆存储装箱特征对象的结构? - How to clone a struct storing a boxed trait object? 为什么特征类型为`Box<dyn error> ` 错误“未实现大小”但 `async fn() -&gt; Result&lt;(), Box<dyn error> &gt;` 有效吗?</dyn></dyn> - Why does a trait type `Box<dyn Error>` error with "Sized is not implemented" but `async fn() -> Result<(), Box<dyn Error>>` works? 盒子如何<dyn trait>解构自己?</dyn> - How does Box<dyn Trait> deconstruct itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM