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.
This commit is contained in:
Lorenz Leutgeb 2026-04-03 10:42:54 +02:00 committed by Lorenz Leutgeb
parent aa177b0400
commit f65175397f
4 changed files with 10 additions and 12 deletions

View File

@ -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()))?;

View File

@ -118,7 +118,7 @@ impl radicle::node::Handle for Handle {
Ok(RefsAt {
remote: self.nid()?,
at: Oid::sha1_zero(),
at: Oid::ZERO_SHA1,
})
}

View File

@ -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::<Oid>()
.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());
}
}
}

View File

@ -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());