Skip to content

Commit b03446d

Browse files
committed
FIX: Simplify azip!() trailing comma rule - use new macro feature $()?
1 parent 0446d5b commit b03446d

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/zip/zipmacro.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,25 @@
101101
/// ```
102102
#[macro_export]
103103
macro_rules! azip {
104-
// Indexed with a single producer and no trailing comma.
105-
((index $index:pat, $first_pat:pat in $first_prod:expr) $body:expr) => {
104+
// Indexed with a single producer
105+
// we allow an optional trailing comma after the producers in each rule.
106+
((index $index:pat, $first_pat:pat in $first_prod:expr $(,)?) $body:expr) => {
106107
$crate::Zip::indexed($first_prod).apply(|$index, $first_pat| $body)
107108
};
108-
// Indexed with more than one producer and no trailing comma.
109-
((index $index:pat, $first_pat:pat in $first_prod:expr, $($pat:pat in $prod:expr),*) $body:expr) => {
109+
// Indexed with more than one producer
110+
((index $index:pat, $first_pat:pat in $first_prod:expr, $($pat:pat in $prod:expr),* $(,)?) $body:expr) => {
110111
$crate::Zip::indexed($first_prod)
111112
$(.and($prod))*
112113
.apply(|$index, $first_pat, $($pat),*| $body)
113114
};
114-
// Indexed with trailing comma.
115-
((index $index:pat, $($pat:pat in $prod:expr),+,) $body:expr) => {
116-
azip!((index $index, $($pat in $prod),+) $body)
117-
};
118-
// Unindexed with a single producer and no trailing comma.
119-
(($first_pat:pat in $first_prod:expr) $body:expr) => {
115+
// Unindexed with a single producer
116+
(($first_pat:pat in $first_prod:expr $(,)?) $body:expr) => {
120117
$crate::Zip::from($first_prod).apply(|$first_pat| $body)
121118
};
122-
// Unindexed with more than one producer and no trailing comma.
123-
(($first_pat:pat in $first_prod:expr, $($pat:pat in $prod:expr),*) $body:expr) => {
119+
// Unindexed with more than one producer
120+
(($first_pat:pat in $first_prod:expr, $($pat:pat in $prod:expr),* $(,)?) $body:expr) => {
124121
$crate::Zip::from($first_prod)
125122
$(.and($prod))*
126123
.apply(|$first_pat, $($pat),*| $body)
127124
};
128-
// Unindexed with trailing comma.
129-
(($($pat:pat in $prod:expr),+,) $body:expr) => {
130-
azip!(($($pat in $prod),+) $body)
131-
};
132125
}

tests/azip.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ fn test_azip2_3() {
4949
assert!(a != b);
5050
}
5151

52+
#[test]
53+
fn test_azip_syntax_trailing_comma() {
54+
let mut b = Array::<i32, _>::zeros((5, 5));
55+
let mut c = Array::<i32, _>::ones((5, 5));
56+
let a = b.clone();
57+
azip!((b in &mut b, c in &mut c, ) swap(b, c));
58+
assert_eq!(a, c);
59+
assert!(a != b);
60+
}
61+
5262
#[test]
5363
#[cfg(feature = "approx")]
5464
fn test_azip2_sum() {

0 commit comments

Comments
 (0)