macro_rules! array_mut_ref { ($arr:expr, $offset:expr, $len:expr) => { ... }; }
Expand description
You can use array_mut_ref
to generate a mutable array reference
to a subset of a sliceable bit of data (which could be an array,
or a slice, or a Vec).
Panics if the slice is out of bounds.
#[macro_use]
extern crate arrayref;
fn write_u16(bytes: &mut [u8; 2], num: u16) {
bytes[0] = num as u8;
bytes[1] = (num >> 8) as u8;
}
// ...
let mut data = [0,1,2,3,4,0,6,7,8,9];
write_u16(array_mut_ref![data,0,2], 1);
write_u16(array_mut_ref![data,2,2], 5);
assert_eq!(*array_ref![data,0,4], [1,0,5,0]);
*array_mut_ref![data,4,5] = [4,3,2,1,0];
assert_eq!(data, [1,0,5,0,4,3,2,1,0,9]);