3 releases

0.1.2 Feb 1, 2025
0.1.1 Jan 31, 2025
0.1.0 Jan 30, 2025

#16 in #array-index

44 downloads per month
Used in eka

MIT/Apache

4KB
53 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

~205–640KB
~15K SLoC