Debian Codegraph @

README.md

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