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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use TokenStream as CompilerTokenStream;
use TokenStream;
use ;
/// Stores an item's tokens to a macro store for later use.
/// - Can be applied to any item (functions, structs, enums, traits, etc.).
/// - Performs no transformations or checks on the item.
/// ```
/// # use enumtrait;
/// #[enumtrait::store(foo_macro_store)]
/// enum Foo {
/// Bar,
/// Bing,
/// }
/// ```
/// Transforms an enum definition such that all variants are single field tuple structs.
/// - variants of kind `StructName,` are converted to `StructName(StructName)`
/// - variants of kind `VariantName(Type)` are not changed
/// - other variant kinds throw an error
/// ```
/// # use enumtrait;
/// struct Bar { bar_val: usize }
/// struct Bing { bing_val: usize }
///
/// #[enumtrait::quick_enum]
/// enum Foo<'a> {
/// Bar,
/// Bing,
/// Bosh(&'a str)
/// }
///
/// fn check(f: Foo) -> usize {
/// match f {
/// Foo::Bar(b) => b.bar_val,
/// Foo::Bing(b) => b.bing_val,
/// Foo::Bosh(s) => s.len(),
/// }
/// }
/// ```
/// Generates a from implementation for every tuple variant of an enum with a single member.
/// - All other variants are ignored.
/// ```
/// # use enumtrait;
///
/// struct Bop { x: i32 };
/// struct Bing {j: i32};
/// struct Baz<T> {val: usize, assoc: T};
/// struct Bar;
///
/// #[enumtrait::quick_enum]
/// #[enumtrait::quick_from]
/// enum Foo<T> {
/// Bop,
/// Bing,
/// Baz(Baz<T>),
/// Bar,
/// }
///
/// fn check() {
/// let foo_0: Foo<i32> = Bop{x: 7}.into();
/// let foo_1: Foo<i32> = Bing{j: -32}.into();
/// let foo_2: Foo<i32> = Baz{val: 42, assoc: 3}.into();
/// let foo_3: Foo<i32> = Bar.into();
/// }
/// ```
/// Generates a match expression for an enumeration
/// - The enum should be of the form generated by [`macro@quick_enum`]
/// - due to an unresolved issue (TODO) with macros in an expression context,
/// and trailing `;`, the macro_store provided must be an identifier (no `path::style::names`)
/// ```
/// # use enumtrait;
/// struct Bar { common_field: usize }
/// struct Bing { common_field: usize, other_field: String }
///
/// #[enumtrait::quick_enum]
/// #[enumtrait::store(foo_macro_store)]
/// enum Foo {
/// Bar,
/// Bing,
/// }
///
/// fn check(f: Foo) -> usize {
/// enumtrait::gen_match!(foo_macro_store as f for it => it.common_field)
/// }
/// ```
/// Generates a trait implementation for an enumeration to allow for polymorphism
/// without `dyn`
/// - The enum should be of the form generated by [`macro@quick_enum`]
/// - Only function items with a reciever (e.g. `fn foo (self) -> T;` in the trait are created, associated types and constants
/// can be supplied manually
/// - Generics are unmodified, so the generic arguments for the trait need to be named exactly
/// as they are for the trait's definition. Be careful here - this macro does not check.
/// ```
/// # use enumtrait;
/// # struct Bar { common_field: usize }
/// # struct Bing { common_field: usize, other_field: String }
/// // given some structs `Foo` and `Bing`
/// #[enumtrait::quick_enum]
/// #[enumtrait::store(foo_macro_store)]
/// enum Foo {
/// Bar,
/// Bing,
/// }
///
/// #[enumtrait::store(foo_trait_store)]
/// trait FooTrait {
/// const baz: usize;
/// fn foo(&self) -> usize;
/// }
///
/// # impl FooTrait for Bar {
/// # const baz: usize = 2;
/// # fn foo(&self) -> usize { self.common_field }
/// # }
/// # impl FooTrait for Bing {
/// # const baz: usize = 2;
/// # fn foo(&self) -> usize { self.common_field }
/// # }
/// // ...Implement `FooTrait` for `Bar` and `Bing`
///
/// #[enumtrait::impl_trait(foo_trait_store for foo_macro_store)]
/// impl FooTrait for Foo {
/// const baz: usize = 42;
/// }
///
/// fn check(f: Foo) -> usize {
/// f.foo()
/// }
/// ```