Macro split_array

Source
macro_rules! split_array {
    (
		let split [
			$($name:ident $( : array $size:ty)?  ),* $(,)?
		] = $data:expr $(;)?
	) => { ... };
}
Expand description

Splits an Array into multiple sub-Arrays.

Notice that each sub-Array must have a defined size, and the total size of the sub-Arrays must match the size of the Aarray on the right-hand side.

The macro expects a let-assignment style syntax with a array pattern, where each element happens to be bind a new variable with type Array.

If the size of a sub-Array cannot be inferred, you can specify it explicitly by adding : array <size> after the variable name, where <size> is a type of an typenum type-“number”.

This macro is the reversal of the crate::merge_array macro.

§Syntax

split_array!{
    let split \[
        ( <name> [ : array <typenum-size> ] , )*
    \] = <value>;
}

§Example

use hybrid_array::Array;
use array_fusion::array_from_core;
use array_fusion::array_to_core;
use array_fusion::split_array;
use hybrid_array::sizes::U4;

// An Array consisting of a 2-byte and a 4-byte sub array
let data: Array<_,_> = array_from_core([
    0, 42, // foo
    0x0, 0x0, 0x12, 0x34, // bar
]);

// Split it into a `foo` and a `bar`.
// The size of foo is inferred by the subsequent usage, while bar is given
// an explicit size.
split_array!{
	let split [
        foo,
        bar: array U4,
    ] = data;
};

// Use foo in a way to make its size inferrable, where `from_be_bytes`
// gives the size of the core array, and `array_to_core` inferres the
// Array size from the size of that core array.
let foo = u16::from_be_bytes(array_to_core(foo));
assert_eq!(foo, 42);

// Use bar, actually this could also infer the size, but we specified it
// already above.
assert_eq!(u32::from_be_bytes(array_to_core(bar)), 0x1234);