Crates.io | array-utils |
lib.rs | array-utils |
version | 0.1.1 |
source | src |
created_at | 2021-03-23 09:01:27.300839 |
updated_at | 2021-03-23 14:55:43.644748 |
description | A no-std heapless set of array utils making use of const generics |
homepage | https://github.com/coastalwhite/array-utils |
repository | https://github.com/coastalwhite/array-utils |
max_upload_size | |
id | 372525 |
size | 29,653 |
A no_std heapless set of utilities for handling const generic arrays. Sized arrays sacrifice speed and convenience for simplicity and predictability. This is mostly used at very low levels and/or on platforms without heaps. This crate will greatly improve the readability and maintainability of code on those platforms.
This crate provides functions to initialize
, drift
, slice
, resize
, splice
, join
and
superimpose
sized arrays. All of which are features enabled by default, but can therefore
also be used separately. Let us go all of the features one by one.
This crate only contains functions which should never panic. Every invalid value given will either result in data truncating or an array being fill up with extra data. The bounds, specific behaviors and other important details are documented in each function's page.
This feature provides 4 utility functions. These are
initialize_from
, initialize_till
,
initialize_from_option
and
initialize_from_result
. All these functions provide an
simpler ways to initialize sized array using closures, as can be seen in their documentation.
The 2 drifting functions, which are drift_to_begin
and drift_to_end
, provide a way to
float a part of the sized array to the beginning or the end of the array. This provides a
solution to the common problem of prefixing or suffixing some elements to an array. It can
be seen as a more optimized shortcut for the the combination of
sized_slice
and
superimpose
. There is also the more general form of superimpose
, which allows for one
sized array to be superimposed upon another.
Ordinary slices of sized array have the disadvantage of either losing size metadata or needing
a .try_into().unwrap()
appended, which can panic. The sized_slice
utility function
provides a way to deal with slicing into sized arrays which never panics. In a similar way to
slicing dealing scaling arrays is rather cumbersome. array_resize
provide a simple way to
deal with all the truncating or expanding of data without the possibility for panics.
The splice
and join
utilities are basically more optimized combinations of
sized_slice
and superimpose
. Making splicing and joining arrays at specific indices can
be very handy for dealing with packet and data streams.
Since we are using sized arrays, all utilities heavily rely on const generics. Furthermore, all
functions are only implemented for types with the Copy
trait. Some
utilities, namely the functions without additional fill
parameter, also depend on the
Default
trait.
Here are some examples or the usage of this crate.
use array_utils::initialize_from;
// Use a closure which doubles the index
let even_numbers: [usize; 5] = initialize_from(|index| index * 2);
assert_eq!(even_numbers, [0, 2, 4, 6, 8]);
use array_utils::{ drift_to_begin, drift_to_end };
let array = [1, 2, 3, 0, 0, 0, 0];
// Float the elements with indices `0..` to the beginning with a margin of `1` elements,
// filling in `0x00` for all new elements.
assert_eq!(drift_to_begin(array, 0, 1, 0x00), [0, 1, 2, 3, 0, 0, 0]);
// Float the elements with indices `..3` to the end with a margin of `0` elements,
// filling in `42` for all new elements.
assert_eq!(drift_to_end(array, 3, 0, 42), [42, 42, 42, 42, 1, 2, 3]);
Licensed with a MIT license.