macro_rules! unroll {
(
// Base case for ranges with no iterations.
for $v:ident in 0..0 $c:block) => { ... };
(
// Handles ranges with a step value.
for $v:ident < $max:tt in ($start:tt..$end:tt).step_by($val:expr) {$($c:tt)*}) => { ... };
(
// Redirects stepped ranges.
for $v:ident in ($start:tt..$end:tt).step_by($val:expr) {$($c:tt)*}) => { ... };
(
// Simplifies parentheses in ranges.
for $v:ident in ($start:tt..$end:tt) {$($c:tt)*}) => { ... };
(
// Main handler for unrolling a range.
for $v:ident in $start:tt..$end:tt {$($c:tt)*}) => { ... };
(
// Validates the range and redirects to internal recursive unrolling with bounds checking.
for $v:ident < $max:tt in $start:tt..$end:tt $c:block) => { ... };
(
// Special case for ranges starting at zero.
for $v:ident in 0..$end:tt {$($statement:tt)*}) => { ... };
(
/* private, recursive unrolling cases */
@$v:ident, $a:expr, 0, $c:block) => { ... };
(@$v:ident, $a:expr, 1, $c:block) => { ... };
(@$v:ident, $a:expr, 2, $c:block) => { ... };
(@$v:ident, $a:expr, 3, $c:block) => { ... };
(@$v:ident, $a:expr, 4, $c:block) => { ... };
(@$v:ident, $a:expr, 5, $c:block) => { ... };
(@$v:ident, $a:expr, 6, $c:block) => { ... };
(@$v:ident, $a:expr, 7, $c:block) => { ... };
(@$v:ident, $a:expr, 8, $c:block) => { ... };
(@$v:ident, $a:expr, 9, $c:block) => { ... };
(@$v:ident, $a:expr, 10, $c:block) => { ... };
(@$v:ident, $a:expr, 11, $c:block) => { ... };
(@$v:ident, $a:expr, 12, $c:block) => { ... };
(@$v:ident, $a:expr, 13, $c:block) => { ... };
(@$v:ident, $a:expr, 14, $c:block) => { ... };
(@$v:ident, $a:expr, 15, $c:block) => { ... };
(@$v:ident, $a:expr, 16, $c:block) => { ... };
(@$v:ident, $a:expr, 17, $c:block) => { ... };
(@$v:ident, $a:expr, 18, $c:block) => { ... };
(@$v:ident, $a:expr, 19, $c:block) => { ... };
(@$v:ident, $a:expr, 20, $c:block) => { ... };
(@$v:ident, $a:expr, 21, $c:block) => { ... };
(@$v:ident, $a:expr, 22, $c:block) => { ... };
(@$v:ident, $a:expr, 23, $c:block) => { ... };
(@$v:ident, $a:expr, 24, $c:block) => { ... };
(@$v:ident, $a:expr, 25, $c:block) => { ... };
(@$v:ident, $a:expr, 26, $c:block) => { ... };
(@$v:ident, $a:expr, 27, $c:block) => { ... };
(@$v:ident, $a:expr, 28, $c:block) => { ... };
(@$v:ident, $a:expr, 29, $c:block) => { ... };
(@$v:ident, $a:expr, 30, $c:block) => { ... };
(@$v:ident, $a:expr, 31, $c:block) => { ... };
(@$v:ident, $a:expr, 32, $c:block) => { ... };
(@$v:ident, $a:expr, 33, $c:block) => { ... };
(@$v:ident, $a:expr, 34, $c:block) => { ... };
(@$v:ident, $a:expr, 35, $c:block) => { ... };
(@$v:ident, $a:expr, 36, $c:block) => { ... };
(@$v:ident, $a:expr, 37, $c:block) => { ... };
(@$v:ident, $a:expr, 38, $c:block) => { ... };
(@$v:ident, $a:expr, 39, $c:block) => { ... };
(@$v:ident, $a:expr, 40, $c:block) => { ... };
(@$v:ident, $a:expr, 41, $c:block) => { ... };
(@$v:ident, $a:expr, 42, $c:block) => { ... };
(@$v:ident, $a:expr, 43, $c:block) => { ... };
(@$v:ident, $a:expr, 44, $c:block) => { ... };
(@$v:ident, $a:expr, 45, $c:block) => { ... };
(@$v:ident, $a:expr, 46, $c:block) => { ... };
(@$v:ident, $a:expr, 47, $c:block) => { ... };
(@$v:ident, $a:expr, 48, $c:block) => { ... };
(@$v:ident, $a:expr, 49, $c:block) => { ... };
(@$v:ident, $a:expr, 50, $c:block) => { ... };
(@$v:ident, $a:expr, 51, $c:block) => { ... };
(@$v:ident, $a:expr, 52, $c:block) => { ... };
(@$v:ident, $a:expr, 53, $c:block) => { ... };
(@$v:ident, $a:expr, 54, $c:block) => { ... };
(@$v:ident, $a:expr, 55, $c:block) => { ... };
(@$v:ident, $a:expr, 56, $c:block) => { ... };
(@$v:ident, $a:expr, 57, $c:block) => { ... };
(@$v:ident, $a:expr, 58, $c:block) => { ... };
(@$v:ident, $a:expr, 59, $c:block) => { ... };
(@$v:ident, $a:expr, 60, $c:block) => { ... };
(@$v:ident, $a:expr, 61, $c:block) => { ... };
(@$v:ident, $a:expr, 62, $c:block) => { ... };
(@$v:ident, $a:expr, 63, $c:block) => { ... };
(@$v:ident, $a:expr, 64, $c:block) => { ... };
}
Available on crate feature
_unroll
only.Expand description
Unrolls the given for loop.
§Example
ⓘ
unroll! {
for i in 0..5 {
println!("Iteration {}", i);
}
}
will expand into:
ⓘ
{ println!("Iteration {}", 0); }
{ println!("Iteration {}", 1); }
{ println!("Iteration {}", 2); }
{ println!("Iteration {}", 3); }
{ println!("Iteration {}", 4); }
§Features
By default it’s implemented for a maximum recusion of 64 iterations.
It supports increased limits of 128, 256, 512, 1024 and 2048 by enabling the
corresponding capability feature: _unroll_[128|256|512|1024|2048]
.
§Derived work
This is derived work from the
crunchy
crate,
including the following modifications:
- use
wrapping_sub
to avoid overflow check. - integrate with the codegen build system.
- make 64 the default recursion limit.
- update docs and comments.
- misc. refactors.