实现自定义错误枚举时,特征绑定 io::Error: Clone 不满足

Nim*_*shn 5 enums traits rust

我在实现自定义错误类型时收到以下错误:

the trait bound `std::io::Error: std::clone::Clone` is not satisfied
Run Code Online (Sandbox Code Playgroud)

这是我的自定义错误枚举:

use std::fmt;
use std::io;
use crate::memtable::Memtable;

// Define our error types. These may be customized for our error handling cases.
// Now we will be able to write our own errors, defer to an underlying error
// implementation, or do something in between.
#[derive(Debug, Clone)]
pub enum MemtableError {
    Io(io::Error),
    FromUTF8(std::string::FromUtf8Error),
    NotFound,
}

// Generation of an error is completely separate from how it is displayed.
// There's no need to be concerned about cluttering complex logic with the display style.
//
// Note that we don't store any extra info about the errors. This means we can't state
// which string failed to parse without modifying our types to carry that information.
impl fmt::Display for MemtableError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Some error occurred!");
        Ok(())
    }
}

// a test function that returns our error result
fn raises_my_error(memtable: Memtable, key: String) -> Result<(),MemtableError> {
    match memtable.read(key) {
        Ok(v) => Ok(()),
        Err(e) => Err(e),
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我尝试遵循这些示例:

  1. https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/define_error_type.html
  2. https://learning-rust.github.io/docs/e7.custom_error_types.html
  3. https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/wrap_error.html

小智 6

在您的 MemtableError-enum 中,您使用std::io::error 的未实现Clone. 这就是错误消息所说的内容。您也应该得到关于 的相同错误std::string::FromUtf8Error

要解决此问题,您可以Clone从派生宏中删除。或者您需要在错误类型上显式实现 Clone。然而,这在当前设置中不起作用,因为 io::ErrorBox<dyn Error + Send + Sync>在内部使用了一个特征对象()。并且这个特质对象无法被克隆。看到这个问题。解决方法是将std::io::Errorand放入orstd::string::FromUtf8Error中:RcArc

#[derive(Debug, Clone)]
pub enum MemtableError {
    Io(std::rc::Rc<io::Error>),
    FromUTF8(std::rc::Rc<std::string::FromUtf8Error>),
    NotFound,
}
Run Code Online (Sandbox Code Playgroud)

要了解这是否是解决此问题的合理方法,我们需要更多地了解其余代码。

因此,最简单的修复方法是删除Clone. 否则使用Rc/ Arc