Skip to content

Commit

Permalink
Fix Issue fizyk20#30 (Empty arr![] compiler error)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
millardjn committed Dec 20, 2016
1 parent 55d8bc9 commit f9c47ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/arr.rs
Expand Up @@ -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];`")
}
17 changes: 17 additions & 0 deletions tests/mod.rs
Expand Up @@ -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;
Expand Down

0 comments on commit f9c47ef

Please sign in to comment.