The trait bound io::Error: Clone is not satisfied when implementing a custom error enum
The trait bound io::Error: Clone is not satisfied when implementing a custom error enum
我在实施自定义错误类型时收到以下错误:
the trait bound `std::io::Error: std::clone::Clone` is not satisfied
这是我的自定义错误枚举:
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),
}
}
我做错了什么?我尝试按照这些示例进行操作:
发生错误是因为您试图为 MemtableError
派生 Clone
实现,但是 std::io::Error
(您的 MemtableError
可以存储)不实现 Clone
本身。如果不需要克隆,我会将其更改为 #[derive(Debug)]
。否则,我们将需要更多关于您的用例的上下文来建议更具体的修复。
在您的 MemtableError 枚举中,您使用的 std::io::error
未实现克隆。这就是错误消息所说的。你也应该得到关于 std::string::FromUtf8Error
.
的相同错误
要解决此问题,您可以从派生宏中删除 Clone
。或者你需要在你的错误类型上显式地实现 Clone 。然而,这在当前设置中不起作用,因为 io::Error 在内部使用特征对象 (Box<dyn Error + Send + Sync>
)。而且这个特征对象不能被克隆。看到这个 issue。解决方法是将 std::io::Error
和 std::string::FromUtf8Error
放在 Rc
或 Arc
:
中
#[derive(Debug, Clone)]
pub enum MemtableError {
Io(std::rc::Rc<io::Error>),
FromUTF8(std::rc::Rc<std::string::FromUtf8Error>),
NotFound,
}
要了解这是否是解决此问题的合理方法,我们需要更多地了解您的其余代码。
因此,最简单的修复方法是删除 Clone
。否则使用 Rc
/Arc
.
我在实施自定义错误类型时收到以下错误:
the trait bound `std::io::Error: std::clone::Clone` is not satisfied
这是我的自定义错误枚举:
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),
}
}
我做错了什么?我尝试按照这些示例进行操作:
发生错误是因为您试图为 MemtableError
派生 Clone
实现,但是 std::io::Error
(您的 MemtableError
可以存储)不实现 Clone
本身。如果不需要克隆,我会将其更改为 #[derive(Debug)]
。否则,我们将需要更多关于您的用例的上下文来建议更具体的修复。
在您的 MemtableError 枚举中,您使用的 std::io::error
未实现克隆。这就是错误消息所说的。你也应该得到关于 std::string::FromUtf8Error
.
要解决此问题,您可以从派生宏中删除 Clone
。或者你需要在你的错误类型上显式地实现 Clone 。然而,这在当前设置中不起作用,因为 io::Error 在内部使用特征对象 (Box<dyn Error + Send + Sync>
)。而且这个特征对象不能被克隆。看到这个 issue。解决方法是将 std::io::Error
和 std::string::FromUtf8Error
放在 Rc
或 Arc
:
#[derive(Debug, Clone)]
pub enum MemtableError {
Io(std::rc::Rc<io::Error>),
FromUTF8(std::rc::Rc<std::string::FromUtf8Error>),
NotFound,
}
要了解这是否是解决此问题的合理方法,我们需要更多地了解您的其余代码。
因此,最简单的修复方法是删除 Clone
。否则使用 Rc
/Arc
.