Expand description
This crate provides API for working with arrays, e.g.:
- Abstraction over arrays (you can use
Array
trait as bound on generics) - Creation of arrays (see
Array
trait) - Doing operations on arrays that produce arrays (see
ArrayMap
andArrayAsRef
traits) - By-value iterating on array (see
IterMove
) Iterator
adapter that yield fixed sized chunks of inner iterator (seeArrayChunks
)
§Example
use arraylib::{Array, ArrayExt, ArrayMap};
// Array creation
let arr = <[_; 11]>::unfold(1, |it| {
let res = *it;
*it *= -2;
res
});
// Mapping
let arr = arr.map(|it| it * 2);
assert_eq!(arr, [2, -4, 8, -16, 32, -64, 128, -256, 512, -1024, 2048]);
// By-value iterator
arr.iter_move().for_each(|i: i32| {})
§Sizes Limitations
Because of lack of const generics
it’s impossible to implement traits on
arrays of all sizes (see std note about that), so this crate implements
traits only for these sizes:
[0; 32]
8 * [5; 12]
(40, 48, …, 96)100 * [1; 10]
(100, 200, …, 1000)2 ** [7; 16]
(128, 256, …, 65536)[33; 128]
(If array-impls-33-128 feature enabled)[129; 256]
(If array-impls-129-256 feature enabled)
§no_std
This lib doesn’t depend on std
, so it can be used in crates with the
#![no_std]
attribute.
§Features
This crate provide next features:
- alloc — enables API that depend on
alloc
crate - nightly — enable features that require nightly features:
trusted_len
(tracking issue) (Adds impl ofTrustedLen
for iterators)exact_size_is_empty
(tracking issue) (Implement<{Chunks,IterMove} as ExactSizeIterator>::is_empty
more effective)
- array-impls-33-128 — adds impl of the
Array
trait for arrays of sizes 33-128 (inclusive) - array-impls-129-256 — adds impl of the
Array
trait for arrays of sizes 129-256 (inclusive)
§Alternatives
Crates those provide similar API (or part of it):
generic_array
array_init
(analogs toArray::from_fn
andArray::from_iter
)array_ext
slice_as_array
(analogs toArrayExt::from_slice
andArray::from_iter
)arraytools
core::array::FixedSizeArray
stackvec
array_iterator
arraymap
§Safety
To achieve good performance and support so many array sizes, this
crate uses a alot of unsafe code (by commit 079871cc
there are 17 unsafe {}
blocks). All unsafe
s were checked with care and have a “Safety”
comment.
If you see that some unsafe
s could be removed without performance loss (we
need benchmarks, oh) please fill an issue.
Modules§
- iter
- Iterator related things
Macros§
- if_
alloc - Conditional compilation depending on whether
arraylib
is built withalloc
feature.
Structs§
- Array
Wrapper - Wrapper over array types. It implements the same traits as
array
, but not for only arrays of sizes0..=32
but for all arrays, those sizes are supported by the crate: - Size
Error - Error that is caused by wrong sizes of slices/arrays
Traits§
- Array
- Represent array of some size. E.g.:
[u8; 32]
,[&str; 8]
,[T; N]
. - Array
AsRef - Trait for conversation between
&[T; N]
and[&T; N]
(or&mut [T; N]
and[&mut T; N]
) - Array
Ext - Extension on arrays that provide additional functions.
- Array
Map - Represent array which elements can be mapped (actually any array)
- Array
Shorthand - Shorthand methods those just refer to
self.as_slice().smt()
- Maybe
Uninit Slice - Extension for maybe uninitialized slices (
[MaybeUninit<_>]
) - Slice
- Extension for
slice