boxarray

Function boxarray 

Source
pub fn boxarray<E: Clone, L: CUList, A: Arrays<E, L>>(e: E) -> Box<A>
Expand description

The boxarray function allow to allocate nested arrays directly on the heap inside a Box and initialize it with a constant value of type E.

§Examples

Zero-size array (i.e. a simple value)

fn signle_array() {
    let a: Box<u32> = boxarray::boxarray(1);
    assert_eq!(*a, 1u32);
}

Single array.

fn signle_array() {
    let a: Box<[u32; 10]> = boxarray::boxarray(1);
    assert_eq!(*a, [1u32; 10]);
}

Nested array.

fn nested_array() {
    let a: Box<[[[f64; 10]; 2]; 4]> = boxarray::boxarray(7.0);
    assert_eq!(*a, [[[7f64; 10]; 2]; 4]);
}

Zero sized type.

fn zero_sized_type() {
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    struct ZST;
    let a: Box<[[[ZST; 10]; 2]; 4]> = boxarray::boxarray(ZST);
    assert_eq!(*a, [[[ZST; 10]; 2]; 4]);
}

If the type of the value to initialize with does not correspond, a compiler will be raised.

fn nested_array_wrong_type() {
    let a: Box<[[[f64; 10]; 2]; 4]> = boxarray::boxarray(7.0f32);
}

If the type to initialize is not only composed of nested arrays, a compiler will be raised.

fn nested_array_wrong_type() {
    let a: Box<[[([f64; 10], [f64; 10]); 2]; 4]> = boxarray::boxarray(7.0);
}