| 1 | # `impl-more` |
| 2 | |
| 3 | > Concise trait implementations. |
| 4 | |
| 5 | <!-- prettier-ignore-start --> |
| 6 | |
| 7 | [](https://crates.io/crates/impl-more) |
| 8 | [](https://docs.rs/impl-more/0.3.1) |
| 9 |  |
| 10 | <br /> |
| 11 | [](https://deps.rs/crate/impl-more/0.3.1) |
| 12 | [](https://crates.io/crates/impl-more) |
| 13 | [](https://circleci.com/gh/robjtede/impl-more/tree/main) |
| 14 | |
| 15 | <!-- prettier-ignore-end --> |
| 16 | |
| 17 | # Usage |
| 18 | |
| 19 | ```rust |
| 20 | struct MyNewTypeStruct(String); |
| 21 | |
| 22 | impl_more::impl_as_ref!(MyNewTypeStruct => String); |
| 23 | impl_more::impl_as_mut!(MyNewTypeStruct => String); |
| 24 | |
| 25 | impl_more::impl_deref!(MyNewTypeStruct => String); |
| 26 | impl_more::impl_deref_mut!(MyNewTypeStruct); |
| 27 | // or, to deref through String too: |
| 28 | // impl_more::forward_deref_and_mut!(MyNewTypeStruct, ref str); |
| 29 | |
| 30 | impl_more::impl_from!(String => MyNewTypeStruct); |
| 31 | impl_more::impl_into!(MyNewTypeStruct => String); |
| 32 | |
| 33 | enum MyEnum { |
| 34 | Bar, |
| 35 | Qux, |
| 36 | } |
| 37 | |
| 38 | impl_more::impl_display_enum!(MyEnum, Bar => "bar", Qux => "qux"); |
| 39 | |
| 40 | enum Coords { |
| 41 | Xy(i64, i64), |
| 42 | Xyz(i64, i64, i64), |
| 43 | } |
| 44 | |
| 45 | impl_more::impl_display_enum!( |
| 46 | Coords, |
| 47 | Xy(x, y) => "{x}, {y}", |
| 48 | Xyz(x, y, z) => "{x}, {y}, {z}", |
| 49 | ); |
| 50 | |
| 51 | #[derive(Debug)] |
| 52 | struct MyError(eyre::Report); |
| 53 | |
| 54 | impl_more::forward_display!(MyError); |
| 55 | impl_more::forward_error!(MyError); |
| 56 | |
| 57 | let err = MyError(eyre::eyre!("something went wrong")); |
| 58 | assert_eq!(err.source().unwrap().to_string(), "something went wrong"); |
| 59 | |
| 60 | #[derive(Debug)] |
| 61 | enum Err { |
| 62 | Io(std::io::Error), |
| 63 | Generic(String), |
| 64 | } |
| 65 | |
| 66 | impl_more::impl_display_enum!(Err, Io(err) => "{err}", Generic(msg) => "{msg}"); |
| 67 | impl_more::impl_error_enum!(Err, Io(err) => err); |
| 68 | ``` |