Re: [PATCH 05/14] rust: add a hash algorithm abstraction
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 28, 2025, 20:00 UTC
- Message-ID
- <xmqq3472dcfl.fsf@gitster.g>
- In-Reply-To
- <20251027004404.2152927-6-sandals@crustytoothpaste.net>
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
Show 16 quoted lines
> +#[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.
Show 21 quoted lines
> + /// 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,
> + }
> + }Show 8 quoted lines
> + /// 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.