| 1 |
use ref_cast::RefCast; |
| 2 |
use std::fmt::Display; |
| 3 |
use std::path::{Path, PathBuf}; |
| 4 |
use thiserror::Error; |
| 5 |
|
| 6 |
#[derive(Error, Debug)] |
| 7 |
#[error("failed to read '{file}'")] |
| 8 |
struct StructPathBuf { |
| 9 |
file: PathBuf, |
| 10 |
} |
| 11 |
|
| 12 |
#[derive(Error, Debug, RefCast)] |
| 13 |
#[repr(C)] |
| 14 |
#[error("failed to read '{file}'")] |
| 15 |
struct StructPath { |
| 16 |
file: Path, |
| 17 |
} |
| 18 |
|
| 19 |
#[derive(Error, Debug)] |
| 20 |
enum EnumPathBuf { |
| 21 |
#[error("failed to read '{0}'")] |
| 22 |
Read(PathBuf), |
| 23 |
} |
| 24 |
|
| 25 |
fn assert<T: Display>(expected: &str, value: T) { |
| 26 |
assert_eq!(expected, value.to_string()); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_display() { |
| 31 |
let path = Path::new("/thiserror"); |
| 32 |
let file = path.to_owned(); |
| 33 |
assert("failed to read '/thiserror'", StructPathBuf { file }); |
| 34 |
let file = path.to_owned(); |
| 35 |
assert("failed to read '/thiserror'", EnumPathBuf::Read(file)); |
| 36 |
assert("failed to read '/thiserror'", StructPath::ref_cast(path)); |
| 37 |
} |