Friendica
rain 🌦️
rain 🌦️

rain 🌦️

rain@hachyderm.io

rain 🌦️

rain@hachyderm.io

(gender)queer transsexual, atheist and scientific humanist, rust person 🦀 and eng @oxidecomputer with computer opinions.

they/them (she ok), content under CC BY 4.0

ActivityPub
2025-05-22 02:07:13 2025-05-22 02:07:11 2025-05-21 21:21:30 7794285

rain 🌦️
rain 🌦️
mastodon - Link to source

rain 🌦️

3 months ago • •

rain 🌦️

3 months ago • •


new #rustlang crate drop: iddqd! ID-based maps where keys are borrowed from values. Four maps are included: IdOrdMap, IdHashMap, a bijective (1:1) BiHashMap and a trijective (1:1:1) TriHashMap.

At Oxide we've found this pattern to be extraordinarily useful. iddqd is no-std compatible, too!

docs.rs/iddqd

use iddqd::{IdOrdMap, IdOrdItem, id_upcast};

#[derive(Debug)]
struct User {
    name: String,
    age: u8,
}

// Implement IdOrdItem so the map knows how to get the key from the value.
impl IdOrdItem for User {
    // The key type can borrow from the value.
    type Key<'a> = &'a str;

    fn key(&self) -> Self::Key<'_> {
        &self.name
    }

    id_upcast!();
}

let mut users = IdOrdMap::<User>::new();

// You must pick an insertion behavior. insert_unique returns an error if
// the key already exists.
users.insert_unique(User { name: "Alice".to_string(), age: 30 }).unwrap();
users.insert_unique(User { name: "Bob".to_string(), age: 35 }).unwrap();

// Lookup by name:
assert_eq!(users.get("Alice").unwrap().age, 30);
assert_eq!(users.get("Bob").unwrap().age, 35);

// Iterate over users:
for user in &users {
    println!("User {}: {}", user.name, user.age);
}

iddqd - Rust

Maps where keys are borrowed from values.
docs.rs
#RustLang
  •  Languages
  •  Search Text
  •  Share via ...
⇧