anderslanglands / ustr

Fast, FFI-friendly string interning for Rust
Other
151 stars 26 forks source link

Use in HashMap #1

Closed luke-titley closed 4 years ago

luke-titley commented 5 years ago

Hey, Thanks again for writing ustr, I'm using it for all object names at the moment in a hobby project I'm working on. Seems to be working okay at the moment, but I have as yet to profile and measue the project properly before I'll be able to give any good feedback.

A quick question.

I need to use this type as a key in a HashMap. Is this how you'd recommend implementing the hash trait for it? Its a bit funny re-hashing the hash but...

impl Hash for Ustr {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.precomputed_hash().hash(hasher)
    }
}

Just wondering what your thoughts are on this.

anderslanglands commented 5 years ago

Hi Luke, sorry I missed this issue earlier! There are actually dedicated HashMap and HashSet type aliases in https://github.com/anderslanglands/ustr/blob/master/src/hash.rs so that the precomputed hash can be used directly. This seemed to be the easiest way to do it. I still need to look into allowing to configure the hash function as a feature as well (which would just be selected one from the fastshash crate, or using Rust' default).

anderslanglands commented 5 years ago

Incidentally I just pushed my latest changes which tweaks the API a little - u! macro is replaced by ustr() short function being the main one.

luke-titley commented 5 years ago

Ah great. I must have missed the hash.rs file.