pub struct ObjectIdPrefix { /* private fields */ }Expand description
A parsed abbreviated object id: the leading hex digits (“nibbles”) of a digest.
Parse the user’s hex once with ObjectIdPrefix::from_hex, then test candidates
with ObjectIdPrefix::matches, which compares raw digest bytes without allocating.
Sorted backends (pack indexes, key-value stores) can instead scan the inclusive id
range ObjectIdPrefix::lowest..=ObjectIdPrefix::highest, which holds exactly
the ids that match.
§Examples
use grit_lib::objects::{HashAlgo, ObjectId, ObjectIdPrefix};
let id = ObjectId::from_hex("4b825dc642cb6eb9a060e54bf8d69288fbee4904")?;
let prefix = ObjectIdPrefix::from_hex("4B825", HashAlgo::Sha1)?;
assert_eq!(prefix.nibbles(), 5);
assert!(prefix.matches(&id));
assert!(!ObjectIdPrefix::from_hex("4b826", HashAlgo::Sha1)?.matches(&id));
assert!(prefix.lowest() <= id && id <= prefix.highest());Implementations§
Source§impl ObjectIdPrefix
impl ObjectIdPrefix
Sourcepub fn from_hex(hex: &str, algo: HashAlgo) -> Result<Self>
pub fn from_hex(hex: &str, algo: HashAlgo) -> Result<Self>
Parse hex (either case, possibly empty) as a prefix of algo object ids.
The empty prefix matches every id.
§Errors
Returns Error::InvalidObjectId when hex contains a non-hex character or is
longer than a full algo id (HashAlgo::hex_len).
Sourcepub fn matches(&self, oid: &ObjectId) -> bool
pub fn matches(&self, oid: &ObjectId) -> bool
Whether oid starts with this prefix.
Compares raw digest bytes (masking the last byte for an odd digit count), so the check never allocates. An id shorter than the prefix never matches.