Crate inplace_it

source ·
Expand description

Inplace it!

Place small arrays on the stack with a low-cost!

The only price you should pay for this is the price of choosing a type based on the size of the requested array! This is just one match!

What?

This crate is created for one purpose: allocating small arrays on the stack. The simplest way to use it is:

use inplace_it::inplace_array;
inplace_array(
    150, // size of needed array to allocate
    4096, // limit in bytes allowed to allocate on the stack
          // if the limit is exceeded then Vec<T> will be used
    |index| index * 2, // initializer will be called for every item in the array
    |memory: &mut [usize]| { // and this is consumer of initialized memory
        assert!(memory.len() >= 150);
        // sometimes more memory may be placed on the stack than needed
        // but if Vec<T> is used that will never happen
    }
)

More details you can find in inplace_array description.

Why?

Because allocation on the stack (i.e. placing variables) is MUCH FASTER then usual allocating in the heap.

Traits

This trait is a extended copy of unstable core::array::FixedSizeArray.

Functions

alloc_array is used when inplace_array realize that the size of requested array of T is too large and should be replaced in the heap.
Places uninitialized memory for the T type on the stack and passes the reference to it into the consumer closure.
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. consumer’s result will be returned.
inplace_fixed_size_array is used when inplace_array realize that the size of requested array of T is small enough and should be replaced on the stack.