Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ksuid hashable so it can be used within HashSet & HashMap #6

Closed
aurelien-clu opened this issue Jan 3, 2024 · 3 comments · Fixed by #7
Closed

Make ksuid hashable so it can be used within HashSet & HashMap #6

aurelien-clu opened this issue Jan 3, 2024 · 3 comments · Fixed by #7

Comments

@aurelien-clu
Copy link
Contributor

Feature Request

Motivation

I would like to use ksuid within an HashMap<Ksuid/KsuidMs, T> and I would prefer not to convert to a string, i.e. HashMap<String, T>.

Right now I am wrapping it within a struct (I am not aware of a better way, like a macro to implicitly call to_string before hashing):

use std::hash::{Hash, Hasher};
use svix_ksuid::Ksuid;

pub struct Id(pub Ksuid);

impl Hash for Id {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.to_string().hash(state);
    }
}

Proposal

I believe the following would be enough:

use std::hash::{Hash, Hasher};

impl Hash for Ksuid {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.to_string().hash(state);
    }
}

impl Hash for KsuidMs {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.to_string().hash(state);
    }
}

Pros:

  • simple
  • not prone to bugs or hash collisions (not more than ksuid as far as my understanding)

Alternatives

Having an intermediate String before computing the hash is not the most efficient way to do this but I personally would prefer to have something even if it is not the most performant 😇

Next

If you don't mind my proposal, would you like me to make a PR with it?

If you do mind adding this, fair enough, I will handle this on my end. 😉

If you do mind using the intermediate string, do you already have in mind a way to do it or would you like me to look into it? (my experience is limited)


Either way, thank you for the library 😄

@tasn
Copy link
Member

tasn commented Jan 3, 2024

Thanks a lot, and yes, please open a PR!

Is it possible to use the internal bytes representation though? To save us from going through a to_string?

@aurelien-clu
Copy link
Contributor Author

aurelien-clu commented Jan 3, 2024

Sure, I have just checked your source code, the internal representation is only:

pub struct Ksuid([u8; TOTAL_BYTES]);

I thought you had both timestamp and byte array and that it could be cumbersome.

I will probably do this tomorrow. (it is late right now, where I live) 😉

ps: thank you for answering quickly :)

@tasn
Copy link
Member

tasn commented Jan 3, 2024

Yup, the internal representation is just the byte array, so it should be easy. :)

Looking forward to seeing that PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants