[][src]Function inplace_it::inplace_array

pub fn inplace_array<T, R, Init: Fn(usize) -> T, Consumer: FnOnce(&mut [T]) -> R>(
    size: usize,
    limit: usize,
    init: Init,
    consumer: Consumer
) -> R

inplace_array trying to place an array of T on the stack, then initialize it using the init closure and finally pass the reference to it into the consumer closure. size argument sets the requested size of an array. consumer's result will be returned.

If the result of array of T is more than limit (or it's size is more than 4096) then the vector will be allocated in the heap and will be initialized and passed as a reference instead of stack-based fixed-size array.

It's shrink placed/allocated memory by inplace_array_uninitialized to requested size so you don't need to worry about extra memory, just use it.

Examples

use inplace_it::inplace_array;

for i in (0..500).step_by(25) {
    inplace_array(
        i, //size of array
        1024, // limit of allowed stack allocation in bytes
        |index| index, // initializer which will be called for every array item,
        |memory: &mut [usize]| { // consumer which will use our allocated array
            assert_eq!(memory.len(), i);
        }
    );
}