Skip to content

Commit

Permalink
fix bug where we could not accept constants as sizes in array_refs!
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Sep 20, 2015
1 parent d942c53 commit 46f25d8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arrayref"
version = "0.3.1"
version = "0.3.2"
authors = ["David Roundy <roundyd@physics.oregonstate.edu>"]
description = "Macros to take array references of slices"
license = "BSD-2-Clause"
Expand Down
13 changes: 13 additions & 0 deletions examples/array_refs.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
18 changes: 18 additions & 0 deletions examples/array_refs_with_const.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
14 changes: 2 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),* ) => {{
Expand All @@ -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
} ),* )
}
Expand All @@ -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 ),* ) => {{
Expand All @@ -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
} ),* )
}
Expand Down

0 comments on commit 46f25d8

Please sign in to comment.