Expand description
no_std
compatible Map and Set backed by arrays.
This crate will evolve as more const-generic features become available.
Features:
derive
: Includes theIndexable
derive macroserde
: Includes serde impls forArrayMap
andArraySet
std
: IncludesFrom
implementations betweenArrayMap<K, AtomicBool, M>
andArraySet<K, S>
This is especially useful if you have a bare enum where you want to treat each key as a field. This also has the benefit that adding a new enum variant forces you to update your code.
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);
// take some bytes and convert them to f32
let floats = bytes.iter().copied().map(|b|u8_to_f32_cache[b]).collect::<Vec<_>>();
Structs§
- Array
Map - A Map backed by an array. The keys must be known statically.
- Array
Set - A set backed by an array. All possible keys must be known statically.
- IndexU8
- This struct implements
Indexable
and allows all values in0..N
- Index
U16 - This struct implements
Indexable
and allows all values in0..N
- Into
Iter - Returned when calling
core::iter::IntoIterator::into_iter()
- Three
Tuple Iter - An efficient iterator implementation over three Indexable types
- TwoTuple
Iter - An efficient iterator implementation over two Indexable types
Traits§
- Indexable
- Allows mapping from a type to an index
- Reverse
Indexable - Allows mapping from an index to a type
Functions§
- set_
size - Convenience function used to determine the underlying size needed for an
ArraySet