Crates.io | eka |
lib.rs | eka |
version | |
source | src |
created_at | 2025-01-30 23:10:13.242282+00 |
updated_at | 2025-02-01 05:48:21.904375+00 |
description | A datastructure for an array-backed O(1) mapping between enum variants and array indexes. |
homepage | https://github.com/Sabin-Pla/rs-enum-key-array |
repository | https://github.com/Sabin-Pla/rs-enum-key-array |
max_upload_size | |
id | 1536953 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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)]
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];