-
Notifications
You must be signed in to change notification settings - Fork 3
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
Consider supporting multiple hash functions #5
Comments
I really like the idea. @michaelwoerister What's the reason for using a const generic I'm thinking of something like this, which is less verbose and more flexible. /// Trait for retrieving the result of the stable hashing operation.
pub trait StableHasherResult<H: HashImpl>: Sized {
fn finish(hash: H::Output) -> Self;
}
/// The hash algorithm used
pub trait HashImpl: Hasher + Debug + Default {
type Output;
fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]);
fn finish(&self) -> Self::Output;
}
#[derive(Debug)]
pub struct StableHasher<H: HashImpl> {
state: H,
} |
Yes, an associated type makes more sense than a generic parameter. That |
And it doesn't even need to be the case. /// Trait for retrieving the result of the stable hashing operation.
pub trait StableHasherResult<H>: Sized {
fn finish(hash: H) -> Self;
}
/// The hash algorithm used
pub trait HashImpl: Hasher + Debug + Default {
type Output;
fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]);
fn finish(self) -> Self::Output;
}
#[derive(Debug)]
pub struct StableHasher<H: HashImpl> {
state: H,
}
impl<H: HashImpl> StableHasher<H> {
pub fn finish<W: StableHasherResult<H::Output>>(self) -> W {
W::finish(self.state.finish())
}
} |
The current
StableHasher
implementation unconditionally uses SipHash13, which is the algorithm of choice for incremental compilation fingerprinting in the Rust compiler. However, for other use cases different hash functions with stronger guarantees, such as BLAKE3, are often preferable.StableHasher
could therefore be generic of the hash algorithm used, e.g.:See here for a quick and dirty POC impl: https://github.com/michaelwoerister/rustc-stable-hash/tree/generic_hash
I've been using
hash: [u64; W]
just for simplicity. It might make more sense to make thathash: [u8; W]
to get rid of any endianess ambiguities.Now that
StableHasher
is available outside the compiler, it makes sense to provide different hash algorithms and to make it clear thatSipHash13
is not a generically good choice.The text was updated successfully, but these errors were encountered: