eka

Crates.ioeka
lib.rseka
version0.1.5
created_at2025-01-30 23:10:13.242282+00
updated_at2025-02-01 05:48:21.904375+00
descriptionA datastructure for an array-backed O(1) mapping between enum variants and array indexes.
homepagehttps://github.com/Sabin-Pla/rs-enum-key-array
repositoryhttps://github.com/Sabin-Pla/rs-enum-key-array
max_upload_size
id1536953
size7,677
Sabin (Sabin-Pla)

documentation

README

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];
Commit count: 20

cargo fmt