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.
Trait Implementations§
Source§impl Clone for ObjectIdPrefix
impl Clone for ObjectIdPrefix
impl Copy for ObjectIdPrefix
Source§impl Debug for ObjectIdPrefix
impl Debug for ObjectIdPrefix
Source§impl Display for ObjectIdPrefix
impl Display for ObjectIdPrefix
impl Eq for ObjectIdPrefix
Source§impl PartialEq for ObjectIdPrefix
impl PartialEq for ObjectIdPrefix
impl StructuralPartialEq for ObjectIdPrefix
Auto Trait Implementations§
impl Freeze for ObjectIdPrefix
impl RefUnwindSafe for ObjectIdPrefix
impl Send for ObjectIdPrefix
impl Sync for ObjectIdPrefix
impl Unpin for ObjectIdPrefix
impl UnsafeUnpin for ObjectIdPrefix
impl UnwindSafe for ObjectIdPrefix
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.