#no-std #no-std #data #structure #map

no-std array_map

Map backed array for fixed size keys with O(1) performance

10 unstable releases (3 breaking)

Uses new Rust 2021

0.4.0 Mar 24, 2022
0.3.4 Sep 27, 2021
0.3.2 Aug 31, 2021
0.3.0 Jul 7, 2021
0.1.0 May 19, 2021

#287 in Data structures

Download history 41/week @ 2022-04-20 86/week @ 2022-04-27 132/week @ 2022-05-04 183/week @ 2022-05-11 78/week @ 2022-05-18 114/week @ 2022-05-25 109/week @ 2022-06-01 63/week @ 2022-06-08 41/week @ 2022-06-15 71/week @ 2022-06-22 54/week @ 2022-06-29 80/week @ 2022-07-06 67/week @ 2022-07-13 47/week @ 2022-07-20 37/week @ 2022-07-27 54/week @ 2022-08-03

215 downloads per month

MIT/Apache

59KB
1.5K SLoC

array_map

no_std compatible Map and Set backed by arrays.

This crate will evolve as more const-generic features become available.

This is especially useful if you have a bare enum where you want to treat each key as a field

use array_map::*;
#[repr(u8)]
#[derive(Indexable)]
enum DetectionType {
  Person,
  Vehicle,
  Bicycle,
}
let thresholds = ArrayMap::<DetectionType, f32, {DetectionType::count()}>::from_closure(|dt| match dt {
    DetectionType::Person => 0.8,
    DetectionType::Vehicle => 0.9,
    DetectionType::Bicycle => 0.7,
  });
let person_threshold = thresholds[DetectionType::Person];

This can also be used to memoize some common computations. (this is 2x as fast as doing the computation on aarch64)

use array_map::*;
let u8_to_f32_cache = ArrayMap::<u8, f32, {u8::SIZE}>::from_closure(|u|f32::from(*u) / 255.0);
let bytes = vec![0_u8; 1024];
// take some bytes and convert them to f32
let floats = bytes.iter().copied().map(|b|u8_to_f32_cache[b]).collect::<Vec<_>>();

Dependencies

~160KB