bluss/maplit

Consider using `.into()` on keys and values

H2CO3 opened this issue · 0 comments

H2CO3 commented

Maps that do not contain the types exactly matching those specified by the arguments could compile this way. E.g., the following fails to compile currently:

let map: HashMap<String, usize> = hashmap!{
    "foo" => 42,
    "bar" => 1337,
};

because the keys are expected by the type annotation to be Strings but they are &strs instead.

If inside the macro definitions, $key and $value arguments were inserted like this:

_map.insert($key.into(), $value.into());
_set.insert($key.into());

then the code above would compile. This is not a dangerous change I think (because it preserves all the safety and typing guarantees of From / Into conversions), and it would make the use of these macros a lot more ergonomic.