| 1 | # What `#[derive(TryUnwrap)]` generates |
| 2 | |
| 3 | This works almost like `Unwrap`. |
| 4 | When an enum is decorated with `#[derive(TryUnwrap)]`, for each variant `foo` in the enum, with fields `(a, b, c, ...)` a public instance method `try_unwrap_foo(self) -> Result<(a, b, c, ...), TryUnwrapError<Self>>` is generated. |
| 5 | If you don't want the `try_unwrap_foo` method generated for a variant, you can put the `#[try_unwrap(ignore)]` attribute on that variant. |
| 6 | If you want to treat a reference, you can put the `#[try_unwrap(ref)]` attribute on the enum declaration or that variant, then `try_unwrap_foo_ref(self) -> Result<(&a, &b, &c, ...), TryUnwrapError<&Self>>` will be generated. You can also use mutable references by putting `#[unwrap(ref_mut)]`. |
| 7 | However, unlike `Unwrap`, it does not panic if the conversion fails. Also, values that fail to convert are not dropped but returned as an `Err`. |
| 8 | |
| 9 | ## Example usage |
| 10 | |
| 11 | ```rust |
| 12 | # use derive_more::TryUnwrap; |
| 13 | # |
| 14 | # #[derive(Debug, PartialEq)] |
| 15 | #[derive(TryUnwrap)] |
| 16 | #[try_unwrap(ref)] |
| 17 | enum Maybe<T> { |
| 18 | Nothing, |
| 19 | Just(T), |
| 20 | } |
| 21 | |
| 22 | fn main() { |
| 23 | assert_eq!(Maybe::Just(1).try_unwrap_just(), Ok(1)); |
| 24 | |
| 25 | // Unlike `Unwrap`, it does not panic. |
| 26 | assert_eq!( |
| 27 | Maybe::<()>::Nothing.try_unwrap_just().map_err(|err| err.input), |
| 28 | Err(Maybe::<()>::Nothing), // and the value is returned! |
| 29 | ); |
| 30 | assert_eq!( |
| 31 | Maybe::Just(2).try_unwrap_nothing().map_err(|err| err.input), |
| 32 | Err(Maybe::Just(2)), |
| 33 | ); |
| 34 | assert_eq!( |
| 35 | Maybe::<()>::Nothing.try_unwrap_just().map_err(|err| err.to_string()), |
| 36 | Err("Attempt to call `Maybe::try_unwrap_just()` on a `Maybe::Nothing` value".into()), |
| 37 | ); |
| 38 | |
| 39 | assert_eq!((&Maybe::Just(42)).try_unwrap_just_ref(), Ok(&42)); |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | ### What is generated? |
| 44 | |
| 45 | The derive in the above example code generates the following code: |
| 46 | ```rust |
| 47 | # use derive_more::TryUnwrapError; |
| 48 | # |
| 49 | # enum Maybe<T> { |
| 50 | # Just(T), |
| 51 | # Nothing, |
| 52 | # } |
| 53 | # |
| 54 | impl<T> Maybe<T> { |
| 55 | pub fn try_unwrap_nothing(self) -> Result<(), TryUnwrapError<Self>> { |
| 56 | match self { |
| 57 | Maybe::Nothing => Ok(()), |
| 58 | val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")), |
| 59 | } |
| 60 | } |
| 61 | pub fn try_unwrap_nothing_ref(&self) -> Result<(), TryUnwrapError<&Self>> { |
| 62 | match self { |
| 63 | Maybe::Nothing => Ok(()), |
| 64 | val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")), |
| 65 | } |
| 66 | } |
| 67 | pub fn try_unwrap_just(self) -> Result<T, TryUnwrapError<Self>> { |
| 68 | match self { |
| 69 | Maybe::Just(field_0) => Ok(field_0), |
| 70 | val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")), |
| 71 | } |
| 72 | } |
| 73 | pub fn try_unwrap_just_ref(&self) -> Result<&T, TryUnwrapError<&Self>> { |
| 74 | match self { |
| 75 | Maybe::Just(field_0) => Ok(field_0), |
| 76 | val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")), |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | ``` |