Skip to main content

ObjectIdPrefix

Struct ObjectIdPrefix 

Source
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

Source

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).

Source

pub fn nibbles(&self) -> usize

Number of hex digits in the prefix.

Source

pub fn is_empty(&self) -> bool

Whether the prefix has no digits (and so matches every id).

Source

pub fn algo(&self) -> HashAlgo

Hash algorithm the prefix was parsed for.

Source

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.

Source

pub fn lowest(&self) -> ObjectId

The smallest algo id that starts with this prefix (remaining digits all 0).

Source

pub fn highest(&self) -> ObjectId

The largest algo id that starts with this prefix (remaining digits all f).

Trait Implementations§

Source§

impl Clone for ObjectIdPrefix

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ObjectIdPrefix

Source§

impl Debug for ObjectIdPrefix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ObjectIdPrefix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ObjectIdPrefix

Source§

impl PartialEq for ObjectIdPrefix

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ObjectIdPrefix

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.