ustr icon indicating copy to clipboard operation
ustr copied to clipboard

Better support with newtype?

Open gen-xu opened this issue 7 months ago • 0 comments

In the following case, the NewType wrapped Ustr doesn't work ergonomically with HashMap though with Borrow being implemented because the hash value of &str is different from the hash value of Ustr, while we are not able to specify the hasher used for the Ustr, it would be nice to add support of specifying the hasher here so the hash value used for the hashmap is the same as Ustr used internally.

This is tangential to the this issue https://github.com/anderslanglands/ustr/issues/39 but closed.

#[derive(Hash, Eq, PartialEq, Debug, Clone)]
struct Name(ustr::Ustr);
impl std::borrow::Borrow<str> for Name {
    fn borrow(&self) -> &str {
        self.0.as_str()
    }
}
fn main() {
    let mut map = std::collections::HashMap::new();
    map.insert(Name(ustr::Ustr::from("John")), 32);
    let result = map.contains_key("John");
    assert_eq!(result, true); // panic as result is false
}

gen-xu avatar Jul 23 '24 02:07 gen-xu