| 1 | #![allow(missing_docs, dead_code, clippy::from_over_into)] |
| 2 | |
| 3 | struct MyNewTypeStruct(String); |
| 4 | |
| 5 | impl_more::impl_as_ref!(MyNewTypeStruct => String); |
| 6 | impl_more::impl_as_mut!(MyNewTypeStruct => String); |
| 7 | |
| 8 | impl_more::impl_deref!(MyNewTypeStruct => String); |
| 9 | impl_more::impl_deref_mut!(MyNewTypeStruct); |
| 10 | // or, to deref through String too: |
| 11 | // impl_more::forward_deref_and_mut!(MyNewTypeStruct, ref str); |
| 12 | |
| 13 | impl_more::impl_from!(String => MyNewTypeStruct); |
| 14 | impl_more::impl_into!(MyNewTypeStruct => String); |
| 15 | |
| 16 | enum MyEnum { |
| 17 | Bar, |
| 18 | Qux, |
| 19 | } |
| 20 | |
| 21 | impl_more::impl_display_enum!(MyEnum: Bar => "bar", Qux => "qux"); |
| 22 | |
| 23 | enum Coords { |
| 24 | Xy(i64, i64), |
| 25 | Xyz(i64, i64, i64), |
| 26 | Uv(i64, i64), |
| 27 | } |
| 28 | |
| 29 | impl_more::impl_display_enum!( |
| 30 | Coords: |
| 31 | Xy(x, y) => "{x}, {y}", |
| 32 | Xyz(x, y, z) => "{x}, {y}, {z}", |
| 33 | Uv(u, _) => "{u}", |
| 34 | ); |
| 35 | |
| 36 | #[derive(Debug)] |
| 37 | enum Err { |
| 38 | Io(std::io::Error), |
| 39 | Generic(String), |
| 40 | } |
| 41 | |
| 42 | impl_more::impl_display_enum! { |
| 43 | Err: |
| 44 | Io(err) => "{err}", |
| 45 | Generic(msg) => "{msg}", |
| 46 | } |
| 47 | #[rustversion::since(1.81)] |
| 48 | impl_more::impl_error_enum!(Err: Io(err) => err); |
| 49 | |
| 50 | #[rustversion::since(1.81)] |
| 51 | #[test] |
| 52 | fn forward_error_newtype() { |
| 53 | use std::error::Error as _; |
| 54 | |
| 55 | #[derive(Debug)] |
| 56 | struct MyError(eyre::Report); |
| 57 | |
| 58 | impl_more::forward_display!(MyError); |
| 59 | impl_more::forward_error!(MyError); |
| 60 | |
| 61 | let err = MyError(eyre::eyre!("something went wrong")); |
| 62 | assert_eq!(err.source().unwrap().to_string(), "something went wrong"); |
| 63 | } |
| 64 | |
| 65 | #[rustversion::since(1.81)] |
| 66 | #[test] |
| 67 | fn forward_error_field() { |
| 68 | use std::error::Error as _; |
| 69 | |
| 70 | #[derive(Debug)] |
| 71 | struct MyError { |
| 72 | cause: eyre::Report, |
| 73 | } |
| 74 | |
| 75 | impl_more::forward_display!(MyError => cause); |
| 76 | impl_more::forward_error!(MyError => cause); |
| 77 | |
| 78 | let err = MyError { |
| 79 | cause: eyre::eyre!("something went wrong"), |
| 80 | }; |
| 81 | assert_eq!(err.source().unwrap().to_string(), "something went wrong"); |
| 82 | } |