From f65175397f12b1c5626862e450a324b6361b4a31 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Fri, 3 Apr 2026 10:42:54 +0200 Subject: [PATCH] oid: `const ZERO_SHA1` instead of `fn sha1_zero` `fn sha1_zero` can be `const fn`, and since it does not take arguments, it can just be a `const`. There is no good reason to not have a constant. Justification for changing the order to first specify "zero" and then "SHA1": In the future we will support multiple object formats. A function that produces a zero OID would be parameterized by the object format then. Imagine `fn zero(format: ObjectFormat)` which would be called as `Oid::zero(ObjectForma::Sha1)`. In anticipation, we change the ordering to match. --- crates/radicle-cli/src/git/unified_diff.rs | 4 ++-- crates/radicle-node/src/test/handle.rs | 2 +- crates/radicle-oid/src/lib.rs | 14 ++++++-------- crates/radicle/src/storage/refs.rs | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/radicle-cli/src/git/unified_diff.rs b/crates/radicle-cli/src/git/unified_diff.rs index e4686841..c8b91af2 100644 --- a/crates/radicle-cli/src/git/unified_diff.rs +++ b/crates/radicle-cli/src/git/unified_diff.rs @@ -336,7 +336,7 @@ impl Encode for FileHeader { w.meta(format!("new file mode {:o}", u32::from(new.mode.clone())))?; w.meta(format!( "index {}..{}", - term::format::oid(git::Oid::sha1_zero()), + term::format::oid(git::Oid::ZERO_SHA1), term::format::oid(*new.oid), ))?; @@ -358,7 +358,7 @@ impl Encode for FileHeader { w.meta(format!( "index {}..{}", term::format::oid(*old.oid), - term::format::oid(git::Oid::sha1_zero()) + term::format::oid(git::Oid::ZERO_SHA1) ))?; w.meta(format!("--- a/{}", path.display()))?; diff --git a/crates/radicle-node/src/test/handle.rs b/crates/radicle-node/src/test/handle.rs index c7f43e49..8c1ab590 100644 --- a/crates/radicle-node/src/test/handle.rs +++ b/crates/radicle-node/src/test/handle.rs @@ -118,7 +118,7 @@ impl radicle::node::Handle for Handle { Ok(RefsAt { remote: self.nid()?, - at: Oid::sha1_zero(), + at: Oid::ZERO_SHA1, }) } diff --git a/crates/radicle-oid/src/lib.rs b/crates/radicle-oid/src/lib.rs index be747b0c..3568ffa5 100644 --- a/crates/radicle-oid/src/lib.rs +++ b/crates/radicle-oid/src/lib.rs @@ -87,6 +87,8 @@ pub enum Oid { // for forwards compatibility: What if another hash with digests of the same // length becomes popular? impl Oid { + pub const ZERO_SHA1: Self = Self::Sha1([0u8; SHA1_DIGEST_LEN]); + pub fn from_sha1(digest: [u8; SHA1_DIGEST_LEN]) -> Self { Self::Sha1(digest) } @@ -96,10 +98,6 @@ impl Oid { Oid::Sha1(digest) => Some(*digest), } } - - pub fn sha1_zero() -> Self { - Self::Sha1([0u8; SHA1_DIGEST_LEN]) - } } /// Interaction with zero. @@ -230,7 +228,7 @@ pub mod str { "0000000000000000000000000000000000000000" .parse::() .unwrap(), - Oid::sha1_zero() + Oid::ZERO_SHA1 ); } @@ -319,7 +317,7 @@ mod fmt { #[test] fn zero() { assert_eq!( - Oid::sha1_zero().to_string(), + Oid::ZERO_SHA1.to_string(), "0000000000000000000000000000000000000000" ); } @@ -404,7 +402,7 @@ mod gix { #[test] fn zero() { - assert!(Oid::sha1_zero() == Other::null(Kind::Sha1)); + assert!(Oid::ZERO_SHA1 == Other::null(Kind::Sha1)); } } } @@ -451,7 +449,7 @@ mod git2 { #[test] fn zero() { - assert!(Oid::sha1_zero() == Other::zero()); + assert!(Oid::ZERO_SHA1 == Other::zero()); } } } diff --git a/crates/radicle/src/storage/refs.rs b/crates/radicle/src/storage/refs.rs index c7b6b3e9..08d69ce8 100644 --- a/crates/radicle/src/storage/refs.rs +++ b/crates/radicle/src/storage/refs.rs @@ -177,7 +177,7 @@ impl Refs { let mut buf = String::new(); for (name, oid) in self.0.iter() { - debug_assert_ne!(oid, &Oid::sha1_zero()); + debug_assert_ne!(oid, &Oid::ZERO_SHA1); debug_assert_ne!(name, &SIGREFS_BRANCH.to_ref_string()); buf.push_str(&oid.to_string());