Fix removal order for dictionary

Switches to `shift_remove` for now. In the future, we should look into a more efficient implementation. Fixes #1258.
This commit is contained in:
Laurenz 2023-05-22 13:10:27 +02:00
parent 08870d4a4c
commit 3d83960322
2 changed files with 7 additions and 1 deletions

View File

@ -88,7 +88,7 @@ impl Dict {
/// Remove a mapping by `key` and return the value.
pub fn remove(&mut self, key: &str) -> StrResult<Value> {
match Arc::make_mut(&mut self.0).remove(key) {
match Arc::make_mut(&mut self.0).shift_remove(key) {
Some(value) => Ok(value),
None => Err(missing_key(key)),
}

View File

@ -60,6 +60,12 @@
#test("c" in dict, false)
#test(dict, (a: 3, b: 1))
---
// Test that removal keeps order.
#let dict = (a: 1, b: 2, c: 3, d: 4)
#dict.remove("b")
#test(dict.keys(), ("a", "c", "d"))
---
// Error: 24-29 duplicate key: first
#(first: 1, second: 2, first: 3)