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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*!
This crate contains a procedural macro attribute that can be placed on an `impl` block. It will generate an `enum`
based on the functions defined in the `impl` block. The generated `enum` will have a variant for each function, and a
new function `map` will be added to the `impl` block that will call the appropriate function based on the variant.
An example:
```
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl Enum {
async fn foo() -> &'static str {
"Foo"
}
unsafe fn bar(baz: i32) -> &'static str {
"Bar"
}
}
# fn main() {
# futures::executor::block_on(
# async {
# unsafe {
# assert_eq!(Enum::map(Enum::Foo).await, "Foo");
# assert_eq!(Enum::map(Enum::Bar { baz: 1337 }).await, "Bar");
# }
# }
# )
# }
```
expands to:
```ignore
enum Enum {
Foo,
Bar {
baz: i32
},
}
impl Enum {
async fn foo() -> &'static str {
"Foo"
}
unsafe fn bar(baz: i32) -> &'static str {
"Bar"
}
async unsafe fn map(&self) -> &'static str {
match self {
Enum::Foo => Enum::foo().await,
Enum::Bar(baz) => Enum::bar(baz),
}
}
}
```
The signatures of functions in the `impl` block may be different, so long as they all have the same return type.
Note that `fn f() -> T` and `async fn f() -> T` are considered to return the same type, even though the latter
technically returns a `impl Future<Output = T>`. See
[the `async` keyword documentation](https://doc.rust-lang.org/std/keyword.async.html) for more information.
```
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl Enum {
fn foo(baz: i32) -> &'static str {
"Foo"
}
async fn bar(&self, baz: bool) -> &'static str {
"Bar"
}
}
```
```compile_fail
# use enum_from_functions::enum_from_functions;
// Causes a compile error because the return types don't match.
#[enum_from_functions]
impl Enum {
fn foo() -> &'static str {
"Foo"
}
fn bar() -> String {
"Bar".to_owned()
}
}
```
`async`, `const` and `unsafe` functions are supported. The presence of any of these keywords will result in the
generated `map` function having the same keyword. For this reason, `async` and `const` functions cannot be present in
the same `impl` block (though `unsafe` functions can be present with either of the other two).
```compile_fail
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl Enum {
async fn foo() -> &'static str {
"Foo"
}
const fn bar() -> &'static str {
"Bar"
}
// This would result in `async const map(...` which is not supported in Rust.
}
```
You can also create an empty `enum` by not providing any functions in the `impl` block (though I'm not sure why you
would want to do this).
```
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl EmptyEnum {}
```
If you need to export the generated `enum` type out of its parent module, provide the `pub` argument to the macro
attribute.
```
mod internal {
# use enum_from_functions::enum_from_functions;
#[enum_from_functions(pub)]
impl Visible {
fn example() -> bool {
true
}
}
}
// Will compile because the generated `enum` is visible outside of the `internal` module.
use internal::Visible;
```
```compile_fail
mod internal {
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl NotVisible {
fn example() -> bool {
false
}
}
}
// Causes a compile error because the generated `enum` is not visible outside of the `internal` module.
use internal::NotVisible;
```
Items in the `impl` block that are not functions will be ignored and passed through to the output unchanged.
Similarly, any attributes applied before *or* after the macro attribute will be applied to the generated `enum`
declaration.
```
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
##[derive(Debug)]
impl Enum {
const FOO: &'static str = "Foo";
fn foo() -> &'static str {
Self::FOO
}
const BAR: &'static str = "Bar";
fn bar() -> &'static str {
Self::BAR
}
const BAZ: &'static str = "Baz";
fn baz() -> &'static str {
Self::BAZ
}
}
# fn main() {
# assert_eq!(Enum::map(Enum::Foo), "Foo");
# assert_eq!(Enum::map(Enum::Bar), "Bar");
# assert_eq!(Enum::map(Enum::Baz), "Baz");
# let _ = format!("{:?}", Enum::Foo);
# }
```
*/
use WithoutTypes;
use TokenStream;
use ;
use quote;
use ;
/**
A procedural macro attribute that generates an `enum` based on the functions defined in the `impl` block it annotates.
See the crate documentation for more information.
*/