-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Like many languages, Rust's main dictionary type is the HashMap.
An unordered map, with unique keys.
What should the behavior be for decoding a dictionary with duplicate keys?
For example, if this is received across the wire: [3][(5, "hi"), (12, "he"), (5, "ha")].
The language guide is very clear on this. One of the first points we state in the dictionary docs is that each key must be unique:
https://docs.icerpc.dev/slice2/language-guide/dictionary-types
And, in C#, we strictly enforce the spec, but I suspect we only do so by accident.
Dictionary.Add throws an exception if you attempt to add a key which already exists.
Should we also strictly enforce the spec in Rust too?
By returning a decoding error if we encounter a duplicate key.
Currently, we just call HashMap::insert, which will happily update an existing key with a new value upon seeing a duplicate.
Some nuance:
The receiver still upholds the rule. Our HashMap does still have unique keys. The real problem is with the sender.
The sender sent us a 'bad' dictionary. So, is it really worth explicitly checking each insertion for uniqueness when:
- this only happens in the rare case where I was sent garbage, which is on them.
- and whether or not we return an error, we will still be upholding all the requirements of the type?