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
|
//! This crate provides a single macro called `if_chain!`.
//!
//! `if_chain!` lets you write long chains of nested `if` and `if let`
//! statements without the associated rightward drift. It also supports multiple
//! patterns (e.g. `if let Foo(a) | Bar(a) = b`) in places where Rust would
//! normally not allow them.
//!
//! See the associated [blog post] for the background behind this crate.
//!
//! [blog post]: https://lambda.xyz/blog/if-chain
//!
//! # Note about recursion limits
//!
//! If you run into "recursion limit reached" errors while using this macro, try
//! adding
//!
//! ```rust,ignore
//! #![recursion_limit = "1000"]
//! ```
//!
//! to the top of your crate.
//!
//! # Examples
//!
//! ## Quick start
//!
//! ```rust,ignore
//! if_chain! {
//! if let Some(y) = x;
//! if y.len() == 2;
//! if let Some(z) = y;
//! then {
//! do_stuff_with(z);
//! }
//! }
//! ```
//!
//! becomes
//!
//! ```rust,ignore
//! if let Some(y) = x {
//! if y.len() == 2 {
//! if let Some(z) = y {
//! do_stuff_with(z);
//! }
//! }
//! }
//! ```
//!
//! ## Fallback values with `else`
//!
//! ```rust,ignore
//! if_chain! {
//! if let Some(y) = x;
//! if let Some(z) = y;
//! then {
//! do_stuff_with(z)
//! } else {
//! do_something_else()
//! }
//! }
//! ```
//!
//! becomes
//!
//! ```rust,ignore
//! if let Some(y) = x {
//! if let Some(z) = y {
//! do_stuff_with(z)
//! } else {
//! do_something_else()
//! }
//! } else {
//! do_something_else()
//! }
//! ```
//!
//! ## Intermediate variables with `let`
//!
//! ```rust,ignore
//! if_chain! {
//! if let Some(y) = x;
//! let z = y.some().complicated().expression();
//! if z == 42;
//! then {
//! do_stuff_with(y);
//! }
//! }
//! ```
//!
//! becomes
//!
//! ```rust,ignore
//! if let Some(y) = x {
//! let z = y.some().complicated().expression();
//! if z == 42 {
//! do_stuff_with(y);
//! }
//! }
//! ```
//!
//! ## Type ascription
//!
//! ```rust,ignore
//! let mut x = some_generic_computation();
//! if_chain! {
//! if x > 7;
//! let y: u32 = another_generic_computation();
//! then { x += y }
//! else { x += 1 }
//! }
//! ```
//!
//! becomes
//!
//! ```rust,ignore
//! let mut x = some_generic_computation();
//! if x > 7 {
//! let y: u32 = another_generic_computation();
//! x += y
//! } else {
//! x += 1
//! }
//! ```
//!
//! ## Multiple patterns
//!
//! ```rust,ignore
//! if_chain! {
//! if let Foo(y) | Bar(y) | Baz(y) = x;
//! let Bubbles(z) | Buttercup(z) | Blossom(z) = y;
//! then { do_stuff_with(z) }
//! }
//! ```
//!
//! becomes
//!
//! ```rust,ignore
//! match x {
//! Foo(y) | Bar(y) | Baz(y) => match y {
//! Bubbles(z) | Buttercup(z) | Blossom(z) => do_stuff_with(z)
//! },
//! _ => {}
//! }
//! ```
//!
//! Note that if you use a plain `let`, then `if_chain!` assumes that the
//! pattern is *irrefutable* (always matches) and doesn't add a fallback branch.
#![cfg_attr(not(test), no_std)]
/// Macro for writing nested `if let` expressions.
///
/// See the crate documentation for information on how to use this macro.
#[macro_export(local_inner_macros)]
macro_rules! if_chain {
($($tt:tt)*) => {
__if_chain! { @init () $($tt)* }
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! __if_chain {
// Expand with both a successful case and a fallback
(@init ($($tt:tt)*) then { $($then:tt)* } else { $($other:tt)* }) => {
__if_chain! { @expand { $($other)* } $($tt)* then { $($then)* } }
};
// Expand with no fallback
(@init ($($tt:tt)*) then { $($then:tt)* }) => {
__if_chain! { @expand {} $($tt)* then { $($then)* } }
};
// Munch everything until either of the arms above can be matched.
// Munched tokens are placed into `$($tt)*`
(@init ($($tt:tt)*) $head:tt $($tail:tt)*) => {
__if_chain! { @init ($($tt)* $head) $($tail)* }
};
// `let` with a single pattern
(@expand { $($other:tt)* } let $pat:pat = $expr:expr; $($tt:tt)+) => {
{
let $pat = $expr;
__if_chain! { @expand { $($other)* } $($tt)+ }
}
};
// `let` with a single identifier and a type hint
(@expand { $($other:tt)* } let $ident:ident: $ty:ty = $expr:expr; $($tt:tt)+) => {
{
let $ident: $ty = $expr;
__if_chain! { @expand { $($other)* } $($tt)+ }
}
};
// `let` with multiple patterns
(@expand { $($other:tt)* } let $pat1:pat | $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
match $expr {
$pat1 | $($pat)|+ => { __if_chain! { @expand { $($other)* } $($tt)+ } }
}
};
// `if let` with a single pattern
(@expand {} if let $pat:pat = $expr:expr; $($tt:tt)+) => {
if let $pat = $expr {
__if_chain! { @expand {} $($tt)+ }
}
};
// `if let` with a single pattern and a fallback
(@expand { $($other:tt)+ } if let $pat:pat = $expr:expr; $($tt:tt)+) => {
if let $pat = $expr {
__if_chain! { @expand { $($other)+ } $($tt)+ }
} else {
$($other)+
}
};
// `if let` with multiple matterns and a fallback (if present)
(@expand { $($other:tt)* } if let $pat1:pat | $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
match $expr {
$pat1 | $($pat)|+ => { __if_chain! { @expand { $($other)* } $($tt)+ } },
_ => { $($other)* }
}
};
// `if` with a successful case
(@expand {} if $expr:expr; $($tt:tt)+) => {
if $expr {
__if_chain! { @expand {} $($tt)+ }
}
};
// `if` with both a successful case and a fallback
(@expand { $($other:tt)+ } if $expr:expr; $($tt:tt)+) => {
if $expr {
__if_chain! { @expand { $($other)+ } $($tt)+ }
} else {
$($other)+
}
};
// Final macro call
(@expand { $($other:tt)* } then { $($then:tt)* }) => {
$($then)*
};
}
#[cfg(test)]
mod tests {
#[test]
fn simple() {
let x: Option<Result<Option<String>, (u32, u32)>> = Some(Err((41, 42)));
let mut success = false;
if_chain! {
if let Some(y) = x;
if let Err(z) = y;
let (_, b) = z;
if b == 42;
then { success = true; }
}
assert!(success);
}
#[test]
fn empty() {
let success;
if_chain! {
then { success = true; }
}
assert!(success);
}
#[test]
fn empty_with_else() {
let success;
if_chain! {
then { success = true; }
else { unreachable!(); }
}
assert!(success);
}
#[test]
fn if_let_multiple_patterns() {
#[derive(Copy, Clone)]
enum Robot { Nano, Biscuit1, Biscuit2 }
for &(robot, expected) in &[
(Robot::Nano, false),
(Robot::Biscuit1, true),
(Robot::Biscuit2, true),
] {
let is_biscuit = if_chain! {
if let Robot::Biscuit1 | Robot::Biscuit2 = robot;
then { true } else { false }
};
assert_eq!(is_biscuit, expected);
}
}
#[test]
fn let_multiple_patterns() {
let x: Result<u32, u32> = Ok(42);
if_chain! {
let Ok(x) | Err(x) = x;
then { assert_eq!(x, 42); }
else { panic!(); }
}
}
#[test]
fn let_type_annotation_patterns() {
let mut x = 1;
if_chain! {
if x > 0;
let y: u32 = 2;
then { x += y; }
else { x += 1; }
}
assert_eq!(x, 3);
}
}
|