diff --git a/Cargo.toml b/Cargo.toml index eab2c7b..b0e69eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "arrayref" -version = "0.3.1" +version = "0.3.2" authors = ["David Roundy "] description = "Macros to take array references of slices" license = "BSD-2-Clause" diff --git a/examples/array_refs.rs b/examples/array_refs.rs new file mode 100644 index 0000000..a3ff081 --- /dev/null +++ b/examples/array_refs.rs @@ -0,0 +1,13 @@ +#[macro_use] +extern crate arrayref; + +fn main() { + let x = [0,1,2,3,4,5,6,7,8,9]; + let (a,b,c) = array_refs!(&x, 2, 3, 5); + assert_eq!(2, a.len()); + assert_eq!(3, b.len()); + assert_eq!(5, c.len()); + assert_eq!(*a, [0,1]); + assert_eq!(*b, [2,3,4]); + assert_eq!(*c, [5,6,7,8,9]); +} diff --git a/examples/array_refs_with_const.rs b/examples/array_refs_with_const.rs new file mode 100644 index 0000000..734ef46 --- /dev/null +++ b/examples/array_refs_with_const.rs @@ -0,0 +1,18 @@ +#[macro_use] +extern crate arrayref; + +const SIZE: usize = 10; +const SIZE_A: usize = 2; +const SIZE_B: usize = 3; +const SIZE_C: usize = 5; + +fn main() { + let x: [u8; SIZE] = [0,1,2,3,4,5,6,7,8,9]; + let (a,b,c) = array_refs!(&x, SIZE_A, SIZE_B, SIZE_C); + assert_eq!(SIZE_A, a.len()); + assert_eq!(SIZE_B, b.len()); + assert_eq!(SIZE_C, c.len()); + assert_eq!(*a, [0,1]); + assert_eq!(*b, [2,3,4]); + assert_eq!(*c, [5,6,7,8,9]); +} diff --git a/src/lib.rs b/src/lib.rs index 4a519ee..a1767da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,11 +45,6 @@ macro_rules! array_ref { /// me ensure that my sub-arrays add up to the entire array. This /// macro will *never* panic, since the sizes are all checked at /// compile time. -/// -/// I believe there are some interesting and exciting bugs that show -/// up when you use lengths that are constants rather than numeric -/// literals. This should be fixable, but it will all be nicer when I -/// can create actual doc tests. #[macro_export] macro_rules! array_refs { ( $arr:expr, $( $len:expr ),* ) => {{ @@ -60,7 +55,7 @@ macro_rules! array_refs { let mut p = a.as_ptr(); ( $( { let aref = &*(p as *const [T; $len]); - p = p.offset($len); + p = p.offset($len as isize); aref } ),* ) } @@ -81,11 +76,6 @@ macro_rules! array_refs { /// This is intentional, as I find it handy to make me ensure that my /// sub-arrays add up to the entire array. This macro will *never* /// panic, since the sizes are all checked at compile time. -/// -/// I believe there are some interesting and exciting bugs that show -/// up when you use lengths that are constants rather than numeric -/// literals. This should be fixable, but it will all be nicer when I -/// can create actual doc tests. #[macro_export] macro_rules! mut_array_refs { ( $arr:expr, $( $len:expr ),* ) => {{ @@ -96,7 +86,7 @@ macro_rules! mut_array_refs { let mut p = a.as_mut_ptr(); ( $( { let aref = &mut *(p as *mut [T; $len]); - p = p.offset($len); + p = p.offset($len as isize); aref } ),* ) }