Skip to content
Update rust error authored by umaumax's avatar umaumax
......@@ -121,10 +121,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
[Error in std::io - Rust]( https://doc.rust-lang.org/std/io/struct.Error.html )
``` rust
use std::io::{Error, ErrorKind};
// errors can be created from strings
let custom_error = Error::new(ErrorKind::Other, "oh no!");
let custom_error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!");
```
### 1つの関数に複数の種類のエラーが入り交じる場合の返り値のResultの型はどうする?
......@@ -140,7 +137,7 @@ let custom_error = Error::new(ErrorKind::Other, "oh no!");
0. 単純に、`Result<(), Box<dyn std::error::Error + 'a>>`でOKであることが判明
1. `Result<(), failure::Error>`を利用すると解決するが、failureはメンテナンスされていないので、別の方法を考える必要がある
2. `anyhow::Result<()>`に対して、`sender.send(frame).or_else(|e| anyhow::bail!("{}", e))?;`とすると問題ない
2. `anyhow::Result<()>`に対して、`sender.send(frame).map_err(|e| anyhow::anyhow!(e))?;`とすると問題ない
* これは結局、`crossbeam_channel::SendError<Hoge<'a>>`のエラー含まれているlifetimeが原因なので、これをそのまま返すのではなく、このエラー型を使って新しいエラー型を構築したものを返すことでlifetimeの制約から逃れている
e.g.
......@@ -166,7 +163,7 @@ impl<'a> HogeSender<'a> {
Ok(())
}
fn send_anyhow_result(&self, hoge: Hoge<'a>) -> anyhow::Result<()> {
self.sender.send(hoge).or_else(|e| anyhow::bail!("{}", e))?;
self.sender.send(hoge).map_err(|e| anyhow::anyhow!(e))?;
Ok(())
}
}
......
......