Skip to content

Commit 3b7540d

Browse files
committed
Added support for trailing comma in the arr! macro
1 parent 30f0cc9 commit 3b7540d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/arr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,30 @@ pub type Inc<T, U> = <U as AddLength<T, U1>>::Output;
2626
#[macro_export]
2727
macro_rules! arr_impl {
2828
($T:ty; $N:ty, [$($x:expr),*], []) => ({
29-
unsafe { $crate::transmute::<_, $crate::GenericArray<$T, $N>>([$($x),*]) }
29+
unsafe { $crate::transmute::<_, $crate::GenericArray<$T, $N>>([$($x),*]) }
3030
});
3131
($T:ty; $N:ty, [], [$x1:expr]) => (
3232
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1 as $T], [])
3333
);
3434
($T:ty; $N:ty, [], [$x1:expr, $($x:expr),+]) => (
35-
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1 as $T], [$($x),*])
35+
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1 as $T], [$($x),+])
3636
);
3737
($T:ty; $N:ty, [$($y:expr),+], [$x1:expr]) => (
38-
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),*, $x1 as $T], [])
38+
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1 as $T], [])
3939
);
4040
($T:ty; $N:ty, [$($y:expr),+], [$x1:expr, $($x:expr),+]) => (
41-
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),*, $x1 as $T], [$($x),*])
41+
arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1 as $T], [$($x),+])
4242
);
4343
}
4444

4545
/// Macro allowing for easy generation of Generic Arrays.
4646
/// Example: `let test = arr![u32; 1, 2, 3];`
4747
#[macro_export]
4848
macro_rules! arr {
49-
($T:ty;) => ({
49+
($T:ty; $(,)*) => ({
5050
unsafe { $crate::transmute::<[$T; 0], $crate::GenericArray<$T, $crate::typenum::U0>>([]) }
5151
});
52-
($T:ty; $($x:expr),*) => (
52+
($T:ty; $($x:expr),* $(,)*) => (
5353
arr_impl!($T; $crate::typenum::U0, [], [$($x),*])
5454
);
5555
($($x:expr,)+) => (arr![$($x),*]);

tests/arr.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[macro_use]
2+
extern crate generic_array;
3+
extern crate typenum;
4+
5+
#[test]
6+
fn empty_without_trailing_comma() {
7+
let ar = arr![u8; ];
8+
assert_eq!(format!("{:x}", ar), "");
9+
}
10+
11+
#[test]
12+
fn empty_with_trailing_comma() {
13+
let ar = arr![u8; , ];
14+
assert_eq!(format!("{:x}", ar), "");
15+
}
16+
17+
#[test]
18+
fn without_trailing_comma() {
19+
let ar = arr![u8; 10, 20, 30];
20+
assert_eq!(format!("{:x}", ar), "0a141e");
21+
}
22+
23+
#[test]
24+
fn with_trailing_comma() {
25+
let ar = arr![u8; 10, 20, 30, ];
26+
assert_eq!(format!("{:x}", ar), "0a141e");
27+
}

0 commit comments

Comments
 (0)