Crate arraylib

Source
Expand description

This crate provides API for working with arrays, e.g.:

  1. Abstraction over arrays (you can use Array trait as bound on generics)
  2. Creation of arrays (see Array trait)
  3. Doing operations on arrays that produce arrays (see ArrayMap and ArrayAsRef traits)
  4. By-value iterating on array (see IterMove)
  5. Iterator adapter that yield fixed sized chunks of inner iterator (see ArrayChunks)

§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 of TrustedLen 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):

§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 unsafes were checked with care and have a “Safety” comment.

If you see that some unsafes 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 with alloc feature.

Structs§

ArrayWrapper
Wrapper over array types. It implements the same traits as array, but not for only arrays of sizes 0..=32 but for all arrays, those sizes are supported by the crate:
SizeError
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].
ArrayAsRef
Trait for conversation between &[T; N] and [&T; N] (or &mut [T; N] and [&mut T; N])
ArrayExt
Extension on arrays that provide additional functions.
ArrayMap
Represent array which elements can be mapped (actually any array)
ArrayShorthand
Shorthand methods those just refer to self.as_slice().smt()
MaybeUninitSlice
Extension for maybe uninitialized slices ([MaybeUninit<_>])
Slice
Extension for slice