1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# What `#[derive(TryUnwrap)]` generates
This works almost like `Unwrap`.
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.
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.
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)]`.
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`.
## Example usage
```rust
# use derive_more::TryUnwrap;
#
# #[derive(Debug, PartialEq)]
#[derive(TryUnwrap)]
#[try_unwrap(ref)]
enum Maybe<T> {
Nothing,
Just(T),
}
fn main() {
assert_eq!(Maybe::Just(1).try_unwrap_just(), Ok(1));
// Unlike `Unwrap`, it does not panic.
assert_eq!(
Maybe::<()>::Nothing.try_unwrap_just().map_err(|err| err.input),
Err(Maybe::<()>::Nothing), // and the value is returned!
);
assert_eq!(
Maybe::Just(2).try_unwrap_nothing().map_err(|err| err.input),
Err(Maybe::Just(2)),
);
assert_eq!(
Maybe::<()>::Nothing.try_unwrap_just().map_err(|err| err.to_string()),
Err("Attempt to call `Maybe::try_unwrap_just()` on a `Maybe::Nothing` value".into()),
);
assert_eq!((&Maybe::Just(42)).try_unwrap_just_ref(), Ok(&42));
}
```
### What is generated?
The derive in the above example code generates the following code:
```rust
# use derive_more::TryUnwrapError;
#
# enum Maybe<T> {
# Just(T),
# Nothing,
# }
#
impl<T> Maybe<T> {
pub fn try_unwrap_nothing(self) -> Result<(), TryUnwrapError<Self>> {
match self {
Maybe::Nothing => Ok(()),
val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")),
}
}
pub fn try_unwrap_nothing_ref(&self) -> Result<(), TryUnwrapError<&Self>> {
match self {
Maybe::Nothing => Ok(()),
val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")),
}
}
pub fn try_unwrap_just(self) -> Result<T, TryUnwrapError<Self>> {
match self {
Maybe::Just(field_0) => Ok(field_0),
val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")),
}
}
pub fn try_unwrap_just_ref(&self) -> Result<&T, TryUnwrapError<&Self>> {
match self {
Maybe::Just(field_0) => Ok(field_0),
val @ _ => Err(todo!("TryUnwrapError::new(val, /* omitted */)")),
}
}
}
```
|