Skip to content

Commit 8d645b0

Browse files
committed
feat: add ObjectId::is_empty_tree() as sybling of ::is_empty_blob().
1 parent 353d237 commit 8d645b0

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

gix-hash/src/kind.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ impl Kind {
5757
[0u8; Kind::longest().len_in_bytes()]
5858
}
5959

60-
/// Returns the amount of ascii-characters needed to encode this has in hex.
60+
/// Returns the amount of bytes needed to encode this instance as hexadecimal characters.
6161
#[inline]
6262
pub const fn len_in_hex(&self) -> usize {
6363
match self {
6464
Kind::Sha1 => 40,
6565
}
6666
}
67-
/// Returns the amount of bytes taken up by the hash of the current kind.
67+
/// Returns the amount of bytes taken up by the hash of this instance.
6868
#[inline]
6969
pub const fn len_in_bytes(&self) -> usize {
7070
match self {
@@ -73,7 +73,7 @@ impl Kind {
7373
}
7474

7575
/// Returns the kind of hash that would fit the given `hex_len`, or `None` if there is no fitting hash.
76-
/// Note that 0 as `hex_len` fits always yields Sha1.
76+
/// Note that `0` as `hex_len` up to 40 always yields `Sha1`.
7777
#[inline]
7878
pub const fn from_hex_len(hex_len: usize) -> Option<Self> {
7979
Some(match hex_len {
@@ -98,15 +98,15 @@ impl Kind {
9898
}
9999
}
100100

101-
/// Create a null-id of our hash kind.
101+
/// Create a shared null-id of our hash kind.
102102
#[inline]
103103
pub fn null_ref(&self) -> &'static oid {
104104
match self {
105105
Kind::Sha1 => oid::null_sha1(),
106106
}
107107
}
108108

109-
/// Create a null-id of our hash kind.
109+
/// Create an owned null-id of our hash kind.
110110
#[inline]
111111
pub const fn null(&self) -> ObjectId {
112112
match self {

gix-hash/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This crate provides types for identifying git objects using a hash digest.
22
//!
3-
//! These are provided in borrowed versions as well as owned ones.
3+
//! These are provided in [borrowed versions][oid] as well as an [owned one][ObjectId].
44
//! ## Feature Flags
55
#![cfg_attr(
66
feature = "document-features",
@@ -19,8 +19,10 @@ pub use object_id::{decode, ObjectId};
1919
///
2020
pub mod prefix;
2121

22-
/// An partial owned hash possibly identifying an object uniquely,
23-
/// whose non-prefix bytes are zeroed.
22+
/// An partial, owned hash possibly identifying an object uniquely, whose non-prefix bytes are zeroed.
23+
///
24+
/// An example would `0000000000000000000000000000000032bd3242`, where `32bd3242` is the prefix,
25+
/// which would be able to match all hashes that *start with* `32bd3242`.
2426
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy, Debug)]
2527
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2628
pub struct Prefix {
@@ -31,7 +33,7 @@ pub struct Prefix {
3133
/// The size of a SHA1 hash digest in bytes.
3234
const SIZE_OF_SHA1_DIGEST: usize = 20;
3335

34-
/// Denotes the kind of function to produce a `Id`.
36+
/// Denotes the kind of function to produce a [`ObjectId`].
3537
#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
3638
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
3739
pub enum Kind {

gix-hash/src/object_id.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::{
22
borrow::Borrow,
33
convert::TryInto,
4-
fmt,
54
hash::{Hash, Hasher},
65
ops::Deref,
76
};
87

98
use crate::{borrowed::oid, Kind, SIZE_OF_SHA1_DIGEST};
109

11-
/// An owned hash identifying objects, most commonly Sha1
10+
/// An owned hash identifying objects, most commonly `Sha1`
1211
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy)]
1312
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1413
pub enum ObjectId {
@@ -77,11 +76,11 @@ pub mod decode {
7776

7877
/// Access and conversion
7978
impl ObjectId {
80-
/// Returns the kind of hash used in this `Id`.
79+
/// Returns the kind of hash used in this instance.
8180
#[inline]
82-
pub fn kind(&self) -> crate::Kind {
81+
pub fn kind(&self) -> Kind {
8382
match self {
84-
ObjectId::Sha1(_) => crate::Kind::Sha1,
83+
ObjectId::Sha1(_) => Kind::Sha1,
8584
}
8685
}
8786
/// Return the raw byte slice representing this hash.
@@ -119,7 +118,15 @@ impl ObjectId {
119118
}
120119
}
121120

122-
/// Returns true if this hash consists of all null bytes.
121+
/// Returns an instances whose bytes are all zero.
122+
#[inline]
123+
pub const fn null(kind: Kind) -> ObjectId {
124+
match kind {
125+
Kind::Sha1 => Self::null_sha1(),
126+
}
127+
}
128+
129+
/// Returns `true` if this hash consists of all null bytes.
123130
#[inline]
124131
pub fn is_null(&self) -> bool {
125132
match self {
@@ -133,12 +140,10 @@ impl ObjectId {
133140
self == &Self::empty_blob(self.kind())
134141
}
135142

136-
/// Returns an Digest representing a hash with whose memory is zeroed.
143+
/// Returns `true` if this hash is equal to an empty tree.
137144
#[inline]
138-
pub const fn null(kind: crate::Kind) -> ObjectId {
139-
match kind {
140-
crate::Kind::Sha1 => Self::null_sha1(),
141-
}
145+
pub fn is_empty_tree(&self) -> bool {
146+
self == &Self::empty_tree(self.kind())
142147
}
143148
}
144149

@@ -194,10 +199,10 @@ impl From<&[u8]> for ObjectId {
194199
}
195200
}
196201

197-
impl From<&crate::oid> for ObjectId {
202+
impl From<&oid> for ObjectId {
198203
fn from(v: &oid) -> Self {
199204
match v.kind() {
200-
crate::Kind::Sha1 => ObjectId::from_20_bytes(v.as_bytes()),
205+
Kind::Sha1 => ObjectId::from_20_bytes(v.as_bytes()),
201206
}
202207
}
203208
}
@@ -210,25 +215,25 @@ impl Deref for ObjectId {
210215
}
211216
}
212217

213-
impl AsRef<crate::oid> for ObjectId {
218+
impl AsRef<oid> for ObjectId {
214219
fn as_ref(&self) -> &oid {
215220
oid::from_bytes_unchecked(self.as_slice())
216221
}
217222
}
218223

219-
impl Borrow<crate::oid> for ObjectId {
224+
impl Borrow<oid> for ObjectId {
220225
fn borrow(&self) -> &oid {
221226
self.as_ref()
222227
}
223228
}
224229

225-
impl fmt::Display for ObjectId {
226-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230+
impl std::fmt::Display for ObjectId {
231+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227232
write!(f, "{}", self.to_hex())
228233
}
229234
}
230235

231-
impl PartialEq<&crate::oid> for ObjectId {
236+
impl PartialEq<&oid> for ObjectId {
232237
fn eq(&self, other: &&oid) -> bool {
233238
self.as_ref() == *other
234239
}

gix-hash/src/oid.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{ObjectId, SIZE_OF_SHA1_DIGEST};
1111
/// internal `bytes` slice length (a fat pointer, pointing to data and its length in bytes)
1212
/// to encode additional information. Before accessing or returning the bytes, a new adjusted
1313
/// slice will be constructed, while the high bits will be used to help resolving the
14-
/// hash `[`kind()`][oid::kind()]`.
14+
/// hash [`kind()`][oid::kind()].
1515
/// We expect to have quite a few bits available for such 'conflict resolution' as most hashes aren't longer
1616
/// than 64 bytes.
1717
#[derive(PartialEq, Eq, Ord, PartialOrd)]
@@ -102,13 +102,13 @@ impl oid {
102102

103103
/// Access
104104
impl oid {
105-
/// The kind of hash used for this Digest.
105+
/// The kind of hash used for this instance.
106106
#[inline]
107107
pub fn kind(&self) -> crate::Kind {
108108
crate::Kind::from_len_in_bytes(self.bytes.len())
109109
}
110110

111-
/// The first byte of the hash, commonly used to partition a set of `Id`s.
111+
/// The first byte of the hash, commonly used to partition a set of object ids.
112112
#[inline]
113113
pub fn first_byte(&self) -> u8 {
114114
self.bytes[0]

0 commit comments

Comments
 (0)