sim*_*nes 15 polymorphism error-handling traits rust trait-objects
我想编写一个函数来读取文件的内容,并在失败时引发错误。我想从 python 脚本调用这个函数,所以我在下面提到了一些 Python 以防它可能相关。
正如我在评论中尝试显示的那样,可能会发生更多的工作,从而引发其他类型的错误,因此,如果可能的话,我想使用通用错误(如果 Rust 中可以实现)(?)。如何返回错误,以便可以对其进行处理并将其包装在 python 错误中,如图所示do_work?不确定我导致以下错误的方法是否方向正确。
fn work_with_text() -> Result<(), dyn std::error::Error> {
let content = match std::fs::read_to_string("text.txt") {
Ok(t) => t,
Err(e) => return Err(e),
};
// do something with content that may cause another type of error (rusqlite error)
Ok(())
}
#[pyfunction]
fn do_work(_py: Python) -> PyResult<u32> {
match work_with_text() {
Ok(_) => (0),
Err(e) => {
let gil = Python::acquire_gil();
let py = gil.python();
let error_message = format!("Error happened {}", e.to_string());
PyIOError::new_err(error_message).restore(py);
return Err(PyErr::fetch(py));
}
};
// ...
}
Run Code Online (Sandbox Code Playgroud)
错误:
1 | ... fn work_with_text() -> Result<(), dyn std::error::Error> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
Run Code Online (Sandbox Code Playgroud)
kfe*_*v91 19
您当前的版本不起作用,因为特征对象没有静态已知的大小,这意味着编译器不知道在堆栈上为它们分配多少空间,因此您不能使用未大小的类型作为函数参数或返回值,除非您通过将它们放在指针后面来调整它们的大小。
固定示例:
fn work_with_text() -> Result<(), Box<dyn std::error::Error>> {
let content = std::fs::read_to_string("text.txt")?;
// do something with content that may cause another type of error (rusqlite error)
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
拥有 aBox<dyn std::error::Error>还允许您从函数中返回各种错误,因为大多数错误类型都可以Box<dyn std::error::Error>通过?运算符自动转换为 a。
如果您想更深入地了解 Rust 中的大小和错误处理,我强烈建议您阅读Rust 中的大小和Rust 中的错误处理。
| 归档时间: |
|
| 查看次数: |
12888 次 |
| 最近记录: |