README.md
1# enum-as-inner
2
3A deriving proc-macro for generating functions to automatically give access to the inner members of enum.
4
5## Basic unnamed field case
6
7The basic case is meant for single item enums, like:
8
9```rust
10use enum_as_inner::EnumAsInner;
11
12#[derive(Debug, EnumAsInner)]
13enum OneEnum {
14 One(u32),
15}
16```
17
18where the inner item can be retrieved with the `as_*()`/`as_*_mut()` or with the `into_*()` functions:
19
20```rust
21let one = OneEnum::One(1);
22
23assert_eq!(*one.as_one().unwrap(), 1);
24assert_eq!(one.into_one().unwrap(), 1);
25
26let mut one = OneEnum::One(2);
27
28assert_eq!(*one.as_one().unwrap(), 1);
29assert_eq!(*one.as_one_mut().unwrap(), 1);
30assert_eq!(one.into_one().unwrap(), 1);
31```
32
33where the result is either a reference for inner items or a tuple containing the inner items.
34
35## Unit case
36
37This will return true if enum's variant matches the expected type
38
39```rust
40use enum_as_inner::EnumAsInner;
41
42#[derive(EnumAsInner)]
43enum UnitVariants {
44 Zero,
45 One,
46 Two,
47}
48
49let unit = UnitVariants::Two;
50
51assert!(unit.is_two());
52```
53
54## Mutliple, unnamed field case
55
56This will return a tuple of the inner types:
57
58```rust
59use enum_as_inner::EnumAsInner;
60
61#[derive(Debug, EnumAsInner)]
62enum ManyVariants {
63 One(u32),
64 Two(u32, i32),
65 Three(bool, u32, i64),
66}
67```
68
69And can be accessed like:
70
71```rust
72let mut many = ManyVariants::Three(true, 1, 2);
73
74assert_eq!(many.as_three().unwrap(), (&true, &1_u32, &2_i64));
75assert_eq!(many.as_three_mut().unwrap(), (&mut true, &mut 1_u32, &mut 2_i64));
76assert_eq!(many.into_three().unwrap(), (true, 1_u32, 2_i64));
77```
78
79## Multiple, named field case
80
81This will return a tuple of the inner types, like the unnamed option:
82
83```rust
84use enum_as_inner::EnumAsInner;
85
86#[derive(Debug, EnumAsInner)]
87enum ManyVariants {
88 One{ one: u32 },
89 Two{ one: u32, two: i32 },
90 Three{ one: bool, two: u32, three: i64 },
91}
92```
93
94And can be accessed like:
95
96```rust
97let mut many = ManyVariants::Three{ one: true, two: 1, three: 2 };
98
99assert_eq!(many.as_three().unwrap(), (&true, &1_u32, &2_i64));
100assert_eq!(many.as_three_mut().unwrap(), (&mut true, &mut 1_u32, &mut 2_i64));
101assert_eq!(many.into_three().unwrap(), (true, 1_u32, 2_i64));
102```
103