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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
Destructures a constant expression into multiple constants
For destructuring into *associated* constants you can use [`associated_multiconst`]
For examples [look here](#examples)
# Syntax
This uses a macro_rules-like syntax to describe the parameters.
The input syntax for `multiconst` is
```text
$(
$(#[$attr:meta])*
$vis:vis const $pattern:pattern : $type:ty = $value:expr;
)*
```
<span id = "pattern"></span>
Where `:pattern` arguments can be any of:
- binding pattern: `$(#[$battr:meta])* $binding:ident`:
which destructures that part of the pattern into a `$binding` constant.
- ignore pattern: `_`: most useful inside other patterns
- remainder pattern: `$(#[$battr:meta])* $binding:ident @ ..` (only usable in arrays):
destructures the rest of the matched array into a `$binding` constant.
- ignore remainder pattern: `..` (usable in arrays, structs, or tuples):
ignores the rest of the elements in the matched collection.
- array pattern: `[ $($array_elem:`[`pattern`](#pattern)`),* $(,)? ]`
- tuple pattern: `( $($tuple_elem:`[`pattern`](#pattern)`),* )`:
- struct pattern:
```text
$struct_name:path {
$(
$(#[$fattr:meta])*
$field_name:field_name: $struct_elem:pattern $(: $type_annotation:ty)?
),*
$(, ..)?
$(,)?
}
```
- tuple struct pattern:
```text
$struct_name:path (
$(
$struct_elem:pattern $(: $type_annotation:ty)?
),*
$(, ..)?
$(,)?
)
```
- `( $pattern:`[`pattern`](#pattern)` )`: a parenthesized pattern
`$vis:vis` can be any visibility modifier,
it is then used as the visibility of every generated constant.
`$type:ty` can be any type (so long as it's compatible with the pattern).
`$value:expr` can be any const expression (so long as its type is `$type`).
`$field_name:field_name` can be either an untyped integer literal or an identifier.
### Attributes
Attributes used on patterns are copied to the generated constant,
which allows documenting `pub` constants.
[example of how to do that here](#attrs-example)
### Struct patterns
Structs patterns (by default) require the struct to implement
the [`FieldType`][trait@crate::FieldType] trait to query the types of destructured fields.
You can annotate the types of fields to avoid the
[`FieldType`][trait@crate::FieldType] requirement.
Struct patterns inhibit length inference of array type arguments,
so you must annotate the array's length.
[example of struct patterns](#example-struct)
# Type Inference
This macro has a limited form of type inference,
it can infer the length of array types written syntactically as an array type
(the length of array type aliases can't be inferred).
Note that `..` patterns in arrays are incompatible with inferring the length of
that array's type.
You don't need to explicitly declare the type of ignored patterns
in syntactic tuple and array types
(type aliases do require explicit types though),
which means that this is allowed:
```rust
# multiconst::multiconst!{
const (A, .., B): (u32, _, _, _, u64) = foo();
# }
#
# const fn foo() -> (u32, (), (), (), u64) {
# (0, (), (), (), 1)
# }
```
# Limitations
This macro only supports destructuring tuples, structs, and arrays.
There are no plans to support destructuring slices or enums.
# Examples
### Array destructuring
This example demonstrates array length inference
(this macro uses the amount of elements in the pattern as the length)
```rust
use multiconst::{for_range, multiconst};
multiconst! {
const [P0, P1, _, P2, P3, _, P4]: [u128; _] = powers_of_two();
}
assert_eq!(P0, 1);
assert_eq!(P1, 2);
assert_eq!(P2, 8);
assert_eq!(P3, 16);
assert_eq!(P4, 64);
const fn powers_of_two<const LEN: usize>() -> [u128; LEN] {
let mut arr = [0; LEN];
for_range!{ i in 0usize..LEN =>
arr[i] = 1 << i;
}
arr
}
```
### Remainder pattern
This example demonstrates the `FOO @ ..` pattern,
to get the rest of the elements of an array.
```
use multiconst::{for_range, multiconst};
type Arr = [u8; 8];
multiconst! {
const [ELEM0, ELEM1, REM @ .., END]: Arr = [3, 5, 7, 11, 13, 17, 19, 23];
}
assert_eq!(ELEM0, 3);
assert_eq!(ELEM1, 5);
assert_eq!(REM, [7, 11, 13, 17, 19]);
assert_eq!(END, 23);
```
### Pseudo-Random number generation
This example demonstrates tuple destructuring
```rust
use multiconst::multiconst;
multiconst! {
const ([A, B, C, D], NEXT_SEED): ([u32; _], u32) = rng(100);
}
const fn rng<const N: usize>(seed: u32) -> ([u32; N], u32) {
// implementation hidden
# let mut arr = [0u32; N];
# let mut i = 0usize;
#
# while i != N {
# arr[i] = seed.wrapping_add(i as u32);
#
# i += 1;
# }
#
# (arr, seed.wrapping_add(N as u32))
}
```
<span id = "example-struct"></span>
### Struct example, derive
This example demonstrates destructuring of structs that derive the
[`FieldType`](trait@crate::FieldType) trait.
*/
/**
use multiconst::{FieldType, multiconst};
multiconst!{
// Structs that impl/derive `FieldType` can be destructured like this
const Deriving{
foo: F,
bar: B,
}: Deriving = Deriving{foo: 10, bar: 20};
}
assert_eq!(F, 10);
assert_eq!(B, 20);
// The `FieldType` derive requires the `"derive"` feature.
#[derive(FieldType)]
struct Deriving {
foo: u32,
bar: u64,
}
```
<span id = "example-struct-ty-annot"></span>
### Struct example, type annotation
This example demonstrates how
non-[`FieldType`](trait@crate::FieldType)-implementing structs can be destructured.
```rust
use multiconst::multiconst;
multiconst!{
// Structs that don't impl `FieldType` can be destructured by annotating field types
const NonDeriving{
baz: B: u32,
qux: Q: u64,
}: NonDeriving = NonDeriving{baz: 10, qux: 20};
}
assert_eq!(B, 10);
assert_eq!(Q, 20);
struct NonDeriving {
baz: u32,
qux: u64,
}
```
<span id = "attrs-example"></span>
### Attributes example
This example demonstrates how attributes (mostly documentation comments)
can be used in multiconst.
```rust
use multiconst::multiconst;
use std::ops::Range;
multiconst! {
/// Attributes (like documentation attributes) can go here,
/// they are copied below the attributes used on inner patterns.
pub const (
/// documentation on the pattern are copied to the generated constants.
MAJOR,
MINOR,
PATCH,
): (u32, u32, u32) = VERSIONS;
}
assert_eq!(MAJOR, 1);
assert_eq!(MINOR, 2);
assert_eq!(PATCH, 3);
# const VERSIONS: (u32, u32, u32) = (1, 2, 3);
multiconst! {
pub const Range {
/// Documentation on struct field patterns looks like this,
/// these doc attributes are for the `S` constant.
start: S,
..
}: Range<u32> = 3..5;
}
assert_eq!(S, 3);
```
*/