1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-05-22 18:30:33 +02:00

Invert std::error and thiserror::Error

This commit is contained in:
Alexandre Senges 2023-08-14 16:55:06 +02:00 committed by GitHub
parent 2721f979ea
commit 0ac245ee61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,13 +6,13 @@ all the different possibilities. `std::error::Error` makes this easy.
```rust,editable,compile_fail ```rust,editable,compile_fail
use std::fs; use std::fs;
use std::io::Read; use std::io::Read;
use std::error::Error; use thiserror::Error;
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)] #[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("Found no username in {0}")] #[error("Found no username in {0}")]
struct EmptyUsernameError(String); struct EmptyUsernameError(String);
fn read_username(path: &str) -> Result<String, Box<dyn Error>> { fn read_username(path: &str) -> Result<String, Box<dyn std::error::Error>> {
let mut username = String::new(); let mut username = String::new();
fs::File::open(path)?.read_to_string(&mut username)?; fs::File::open(path)?.read_to_string(&mut username)?;
if username.is_empty() { if username.is_empty() {