Skip to content

Better support with newtype? #47

@gen-xu

Description

@gen-xu

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 #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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions