Subject: Re: [PATCH 05/14] rust: add a hash algorithm abstraction

From: Junio C Hamano <[email-masked]>
Date: Tue, 28 Oct 2025 13:00:14 -0700

"brian m. carlson" <[email-masked]> writes:

> +#[allow(dead_code)]
> +impl ObjectID {
> +    pub fn as_slice(&self) -> &[u8] {
> +        match HashAlgorithm::from_u32(self.algo) {
> +            Some(algo) => &self.hash[0..algo.raw_len()],
> +            None => &self.hash,
> +        }
> +    }
> +
> +    pub fn as_mut_slice(&mut self) -> &mut [u8] {
> +        match HashAlgorithm::from_u32(self.algo) {
> +            Some(algo) => &mut self.hash[0..algo.raw_len()],
> +            None => &mut self.hash,
> +        }
> +    }
> +}

These cases for "None" surprised me a bit; I would have expected us
to error out when given an algorithm we do not recognise.

> +    /// Return a hash algorithm based on the internal integer ID used by Git.
> +    ///
> +    /// Returns `None` if the algorithm doesn't indicate a valid algorithm.
> +    pub const fn from_u32(algo: u32) -> Option<HashAlgorithm> {
> +        match algo {
> +            1 => Some(HashAlgorithm::SHA1),
> +            2 => Some(HashAlgorithm::SHA256),
> +            _ => None,
> +        }
> +    }
> +
> +    /// Return a hash algorithm based on the internal integer ID used by Git.
> +    ///
> +    /// Returns `None` if the algorithm doesn't indicate a valid algorithm.
> +    pub const fn from_format_id(algo: u32) -> Option<HashAlgorithm> {
> +        match algo {
> +            0x73686131 => Some(HashAlgorithm::SHA1),
> +            0x73323536 => Some(HashAlgorithm::SHA256),
> +            _ => None,
> +        }
> +    }

> +    /// The number of bytes which is processed by one iteration of this algorithm's compression
> +    /// function.
> +    pub const fn block_size(self) -> usize {
> +        match self {
> +            HashAlgorithm::SHA1 => 64,
> +            HashAlgorithm::SHA256 => 64,
> +        }
> +    }

What we see in this patch seems to be a fairly complete rewrite of
what we have in <hash.h>.  I totally forgot that we had this "block
size" there, which is only used in receive-pack.c when we compute
the push certificate.