Re: [PATCH 05/14] rust: add a hash algorithm abstraction
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Oct 28, 2025, 09:18 UTC
- Message-ID
- <aQCKTFrmtlp9UEL6@pks.im>
- In-Reply-To
- <20251027004404.2152927-6-sandals@crustytoothpaste.net>
On Mon, Oct 27, 2025 at 12:43:55AM +0000, brian m. carlson wrote:
Show 34 quoted lines
> diff --git a/src/hash.rs b/src/hash.rs
> index 0219391820..1b9f07489e 100644
> --- a/src/hash.rs
> +++ b/src/hash.rs
> @@ -19,3 +19,145 @@ pub struct ObjectID {
> pub hash: [u8; GIT_MAX_RAWSZ],
> pub algo: u32,
> }
> +
> +#[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,
> + }
> + }
> +}
> +
> +/// A hash algorithm,
> +#[repr(C)]
> +#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
> +pub enum HashAlgorithm {
> + SHA1 = 1,
> + SHA256 = 2,
> +}
> +Seeing all the `match` statements: we could alternatively implement this as a trait. This would have the added benefit that we cannot miss updating any of the functions if we ever were to add another hash function.
Patrick