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
/// Macro for using [`NList`](crate::NList) in patterns.
///
/// This macro uses the same syntax as array patterns, with the limitation that
/// it only supports `..` patterns at the end.
///
/// # Alternatives
///
/// The [`unlist`](crate::unlist) macro allows destructuring [`NList`]
/// by value in some contexts where this macro can't be used,
/// refer to its docs for more details.
///
/// # Example
///
/// ### Destructuring
///
/// ```rust
/// use nlist::{nlist, nlist_pat};
///
/// // destructuring by value
/// {
/// let nlist_pat![a, b, c @ ..] = nlist![3, 5, 8, 13, 21];
///
/// assert_eq!(a, 3);
/// assert_eq!(b, 5);
/// assert_eq!(c, nlist![8, 13, 21]);
/// }
///
/// // destructuring by reference
/// {
/// let nlist_pat![a, b, c @ ..] = &nlist![3, 5, 8, 13, 21];
///
/// assert_eq!(a, &3);
/// assert_eq!(b, &5);
/// assert_eq!(c, &nlist![8, 13, 21]);
/// }
///
/// // destructuring by mutable reference
/// {
/// let nlist_pat![a, b, c @ ..] = &mut nlist![3, 5, 8, 13, 21];
///
/// assert_eq!(a, &mut 3);
/// assert_eq!(b, &mut 5);
/// assert_eq!(c, &mut nlist![8, 13, 21]);
/// }
/// ```
///
/// ### Pattern matching
///
/// ```rust
/// use nlist::{NList, PeanoInt, PlusOne, nlist, nlist_pat};
///
/// assert!(starts_with_zero(&nlist![0, 1, 2]));
/// assert!(!starts_with_zero(&nlist![1]));
/// assert!(!starts_with_zero(&nlist![1, 2, 3]));
///
/// const fn starts_with_zero<L: PeanoInt>(nlist: &NList<u32, PlusOne<L>>) -> bool {
/// matches!(nlist, nlist_pat![0, ..])
/// }
/// ```
///
/// [`NList`]: crate::NList
,
..
}
}
);
=> ;
}
);
=> ;
}
/// Destructures an [`NList`](crate::NList) by value into its elements
///
/// This macro uses the same syntax as array patterns, with the limitation that
/// it only supports `..` patterns at the end.
///
/// # Motivation
///
/// This macro exists because, as of Rust 1.83,
/// destructuring generic `NList`s by value in const fns with [`nlist_pat`]
/// causes "destructor cannot be evaluated in const fn" errors.
///
/// Note: destructuring into nested non-Copy fields isn't supported in
/// the aforementioned generic const fn context,
/// you'll need to bind the NList element to a variable,
/// then destructure the variable with [`konst::destructure`](konst::destructure).
///
/// # Example
///
/// ```rust
/// use nlist::{NList, PeanoInt, Peano, nlist, peano, unlist};
///
/// assert_eq!(multisplit(nlist![3, 5]), (3, 5, nlist![]));
/// assert_eq!(multisplit(nlist![8, 13, 21]), (8, 13, nlist![21]));
/// assert_eq!(multisplit(nlist![34, 55, 89, 144]), (34, 55, nlist![89, 144]));
///
/// const fn multisplit<T, L>(list: NList<T, peano::Add<Peano!(2), L>>) -> (T, T, NList<T, L>)
/// where
/// L: PeanoInt
/// {
/// unlist!{[a, b, c @ ..] = list}
///
/// (a, b, c)
/// }
/// ```
/// If the `unlist!{...}` line is replaced with
/// ```rust,compile_fail
/// # use nlist::{NList, PeanoInt, Peano, nlist, nlist_pat, peano, unlist};
/// # const fn multisplit<T, L>(list: NList<T, peano::Add<Peano!(2), L>>) -> (T, T, NList<T, L>)
/// # where
/// # L: PeanoInt
/// # {
/// let nlist_pat![a, b, c @ ..] = list;
/// # (a, b, c)
/// # }
/// ```
/// it causes this compilation error as of Rust 1.83.0:
/// ```text
/// error[E0493]: destructor of `NList<T, nlist::PlusOne<nlist::PlusOne<L>>>` cannot be evaluated at compile-time
/// --> src/macros/destructuring_macros.rs:134:27
/// |
/// 6 | const fn multisplit<T, L>(list: NList<T, peano::Add<Peano!(2), L>>) -> (T, T, NList<T, L>)
/// | ^^^^ the destructor for this type cannot be evaluated in constant functions
/// ...
/// 12 | }
/// | - value is dropped here
///
/// ```
$crate__unlist!
);
=> ;
}