slotmap icon indicating copy to clipboard operation
slotmap copied to clipboard

Add API to allow reassignment of old keys and values

Open kkngsm opened this issue 2 years ago • 0 comments

Hello, First of all, I would like to thank you for developing this great library.

Now, I am in need of a function that allows me to reassign old keys and values. This is needed to implement Undo and other state reverting functions. Is it possible to implement this?

Example

# use slotmap::*;
let mut sm = SlotMap::new();
let foo = sm.insert("foo");
assert_eq!(sm[foo], "foo");

// bar is inserted in the place of foo
sm.remove(foo);
let bar = sm.insert("bar");
assert_eq!(sm[bar], "bar");

// Inserting the foo key and value returns the bar that was already there
let prev_foo = sm.insert_key_value(foo, "foo");
assert_eq!(sm[foo], "foo");
assert_eq!(prev_foo, Some((bar, "bar")));

// If empty, None is returned.
sm.remove(foo);
let prev_bar = sm.insert_key_value(bar, "bar");
assert_eq!(sm[bar], "bar");
assert_eq!(prev_bar, None);

kkngsm avatar Dec 14 '22 12:12 kkngsm