From f9c47ef7fd1a12f929cd1f19e4b10f846e48b88c Mon Sep 17 00:00:00 2001 From: James Millard Date: Tue, 20 Dec 2016 19:24:43 +1000 Subject: [PATCH] Fix Issue #30 (Empty arr![] compiler error) Added example to arr![] doc Return a helpful error when empty macro is invoked. Syntax error hack. Added some test cases to make sure actual zero and unit length arrays weren't broken. --- src/arr.rs | 4 +++- tests/mod.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/arr.rs b/src/arr.rs index 2c84db485c..5655d3c5f0 100644 --- a/src/arr.rs +++ b/src/arr.rs @@ -42,10 +42,12 @@ macro_rules! arr_impl { } /// Macro allowing for easy generation of Generic Arrays. +/// Example: `let test = arr![u32; 1, 2, 3];` #[macro_export] macro_rules! arr { ($T:ty; $($x:expr),*) => ( arr_impl!($T; U0, [], [$($x),*]) ); - ($($x:expr,)*) => (arr![$($x),*]) + ($($x:expr,)+) => (arr![$($x),*]); + () => ("""Macro requires a type, e.g. `let array = arr![u32; 1, 2, 3];`") } diff --git a/tests/mod.rs b/tests/mod.rs index 1138de1b65..01a425b9cb 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -64,6 +64,23 @@ fn test_iter_flat_map() { assert!((0..5).flat_map(|i| arr![i32; 2 * i, 2 * i + 1]).eq(0..10)); } +#[test] +fn test_unit_macro(){ + let arr = arr![f32; 3.14]; + assert_eq!(arr[0], 3.14); +} + +#[test] +fn test_empty_macro(){ + let arr = arr![f32;]; +} + +/// This test should cause a helpful compile error if uncommented. +// #[test] +// fn test_empty_macro2(){ +// let arr = arr![]; +// } + #[cfg(feature="serde")] mod impl_serde { extern crate serde_json;