computed_map

Crates.iocomputed_map
lib.rscomputed_map
version0.1.0
created_at2025-08-25 08:19:34.541904+00
updated_at2025-08-25 08:19:34.541904+00
descriptionA Rust proc-macro crate for generating indexed maps with computed fields and indicies.
homepagehttps://github.com/JeffDownie/computed_map
repository
max_upload_size
id1809202
size39,479
Jeff Downie (JeffDownie)

documentation

https://docs.rs/computed_map

README

computed_map

Crates.io Docs.rs

A Rust proc-macro crate for generating indexed maps with computed fields and indices.

Features

  • Procedural macro to generate map types with computed indices.
  • Supports multiple computed indices per map.
  • Automatically maintains indices as values are inserted or removed.

Example

use computed_map::*;

generate_table! {
    TestTable, u64, u64, // The name of the map struct, the key type, and the value type
    PlusOne, |x: u64| -> u64 { x + 1 }, true
}

fn main() {
    let mut table = TestTable::new();
    let mut locked_table = table.lock();
    locked_table.insert(42, 100);
    locked_table.insert(43, 100);
    table = locked_table.unlock();
    table.len(); // 2
    table.get(&42) // Some(&(42, MappedValuesStructTestTable { plus_one: 101 } ));
    table.get_plus_one(&101); // Some(&BTreeSet::from([42, 43]));
}

Usage

Add to your Cargo.toml:

[dependencies]
computed_map = "0.1.0"

Then use the generate_table! macro to define your indexed map.

Macro Syntax

generate_table! {
    TableName, KeyType, ValueType,
    [ComputedName, ComputedFunction, AddIndex]*
}
  • TableName: Name of the generated table struct.
  • KeyType: Type of the key.
  • ValueType: Type of the value.
  • ComputedName: Name of the computed value.
  • ComputedFunction: Function to compute a value dependant on the original type. Must be a typed closure
  • AddIndex: Whether to add an index over the computed field (true or false).

You can specify multiple indices by repeating the triple: ComputedName, ComputedFunction, AddIndex.

License

Licensed under MIT.

Links

Commit count: 0

cargo fmt