Two Keys wrapping the same string compare as not equal when one is built
from a 'static str and the other from a borrowed str:
let static_key = Key::from_str_static("a key");
let borrowed_key = Key::from_str("a key");
assert_eq!(static_key, borrowed_key); // panics
Cause: Key stores its string in a MaybeStaticStr enum and derives
PartialEq/PartialOrd/Ord. The derived impls compare the enum variant
first, so Static("a key") != Borrowed("a key") even though the contents match.
This breaks the invariant documented on the type ("these impls must only be
based on the as_str() representation").
Impact: key equality/ordering depends on how the key was constructed, so any
code that uses Key in a map/set, dedups or sorts keys can get wrong results
when keys come from a mix of 'static and borrowed sources.
Fix: implement the comparison traits by hand against as_str() so they
depend only on the string contents, not on the MaybeStaticStr variant.
PR: #733
Two
Keys wrapping the same string compare as not equal when one is builtfrom a
'staticstr and the other from a borrowed str:Cause:
Keystores its string in aMaybeStaticStrenum and derivesPartialEq/PartialOrd/Ord. The derived impls compare the enum variantfirst, so
Static("a key") != Borrowed("a key")even though the contents match.This breaks the invariant documented on the type ("these impls must only be
based on the
as_str()representation").Impact: key equality/ordering depends on how the key was constructed, so any
code that uses
Keyin a map/set, dedups or sorts keys can get wrong resultswhen keys come from a mix of
'staticand borrowed sources.Fix: implement the comparison traits by hand against
as_str()so theydepend only on the string contents, not on the
MaybeStaticStrvariant.PR: #733