macro_rules! array_refs { ( $arr:expr, $( $pre:expr ),* ; .. ; $( $post:expr ),* ) => { ... }; ( $arr:expr, $( $len:expr ),* ) => { ... }; }
Expand description
You can use array_refs
to generate a series of array references
to an input array reference. The idea is if you want to break an
array into a series of contiguous and non-overlapping arrays.
array_refs
is a bit funny in that it insists on slicing up the
entire array. 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.
Note that unlike array_ref!
, array_refs
requires that the
first argument be an array reference. The following arguments are
the lengths of each subarray you wish a reference to. The total
of these arguments must equal the size of the array itself.
#[macro_use]
extern crate arrayref;
fn read_u16(bytes: &[u8; 2]) -> u16 {
bytes[0] as u16 + ((bytes[1] as u16) << 8)
}
// ...
let data = [0,1,2,3,4,0,6,7];
let (a,b,c) = array_refs![&data,2,2,4];
assert_eq!(read_u16(a), 256);
assert_eq!(read_u16(b), 3*256+2);
assert_eq!(*c, [4,0,6,7]);