orlp / slotmap

Slotmap data structure for Rust
zlib License
1.08k stars 70 forks source link

Make sure values are unique and return same key for same values #90

Open myarcana opened 1 year ago

myarcana commented 1 year ago

Is this possible? Or is there an easy way to extend slotmap or write a new type of slotmap that supports this?

let mut thing = slotmap::SlotMap::new();
let a = thing.insert("hello");
let b = thing.insert("hello");
assert_eq!(a, b);
orlp commented 1 year ago

Hi, this is commonly known as "string interning", if that helps you. However, slotmap does not support lookups by arbitrary keys.

You can emulate this by using a HashMap<String, DefaultKey> together with a SlotMap<DefaultKey, String> though.