#data-structures #array-index #variant #enums #mapping #array-backed

nightly bin+lib eka

A datastructure for an array-backed O(1) mapping between enum variants and array indexes

6 releases

new 0.1.5 Feb 1, 2025
0.1.4 Jan 31, 2025

#1 in #datastructure

Download history 51/week @ 2025-01-25

51 downloads per month

MIT/Apache

7KB
106 lines

This crate provides a datastructure for an array-backed O(1) mapping between enum variants and array indexes.

[dependencies]
eka = "0.1.5"

Currently relies on incomplete features:

#![feature(generic_const_exprs)]
#![feature(maybe_uninit_array_assume_init)]
#![allow(incomplete_features)]

This demo, as well as any project using this crate must built with cargo +nightly.

Projects using this crate also need to enable #![feature(generic_const_exprs)]

How to use:

First, define the enum you want to use as your array key.

#[derive(Debug, Idable)]
enum ExampleKey {
    A,
    B,
    C,
    D,
    E
}

Next, define the enum key array, which will allow us to associate each of the 5 variants of ExampleKey with an index in the structure below. See idable.rs if each variant does not necessarily map to each element, and a different implementation is required.

// if S implements Default + Copy, we can create the datastructure safely
let mut eka_default  = EKA::<ExampleKey, S>::new();

// Create an EKA with randomly assigned data (unsafe)
let mut eka_random_init  = unsafe { EKA::<ExampleKey, S>::uninitialized() };

// Create an EKA with all zero-byte data 
// unsafe because S not being zeroable might induce UB
let mut eka_zeroed  = unsafe { EKA::<ExampleKey, S>::zeroed() };

EKA can be indexed by keys.

eka[ExampleKey::A] = <expression>;
let val = eka[ExampleKey::B];

Dependencies

~195–630KB
~15K SLoC