Crate atom_table

source ·
Expand description

This crate provides a generic “Atom Table” style data structure. “Atom Table” conventionally refers to a table of strings where each has an ID, and you can look up the ID by the string, or the string by the ID. The AtomTable<V, I> struct in this crate is more generic, allowing nearly any type as the “value” type, and using custom type-safe ID types you provide as the ID.

Examples

Example usage might look like:

use derive_more::{From, Into};
use atom_table::AtomTable;

/// The ID type for the table
/// Using `derive_more` to save having to manually implement traits
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, From, Into)]
struct Id(usize);

let mut table: AtomTable<String, Id> = vec!["a", "b", "c"]
    .into_iter()
    .map(str::to_string)
    .collect();

let a = table.get_id("a").unwrap();
let b = table.get_id("b").unwrap();
let d = table.get_or_create_id_for_owned_value("d".to_string());

let b_try_again = table.get_or_create_id("b");
assert_eq!(b, b_try_again);

Structs

  • A data structure that lets you use strongly typed indices/IDs instead of bulky values, performing a lookup in both directions.
  • Indicates that your transform function for AtomTable did not return unique outputs, so the “lookup ID by value” feature of the data structure could not be maintained in the transformed result.

Enums