pub enum ObjectHash {
Sha1([u8; 20]),
Sha256([u8; 32]),
Blake3([u8; 32]),
}Expand description
Concrete object ID value carrying the bytes for the selected algorithm (SHA-1, SHA-256 or BLAKE3-256). Used for Git object hashes. Supports conversion to/from hex strings, byte slices, and stream reading.
Sha256 and Blake3 carry the same 32 bytes and print as the same 64-hex
width; they are distinct variants and never compare equal, so the algorithm
is always preserved as metadata.
Variants§
Sha1([u8; 20])
Sha256([u8; 32])
Blake3([u8; 32])
BLAKE3-256 repository object ID (git-internal / Libra extension).
Implementations§
Source§impl ObjectHash
impl ObjectHash
Sourcepub fn zero_for_kind(kind: HashKind) -> ObjectHash
pub fn zero_for_kind(kind: HashKind) -> ObjectHash
Zero-filled ID (all bytes 0x00) for a given hash kind.
Sourcepub fn ensure_kind(&self, expected: HashKind) -> Result<(), HashError>
pub fn ensure_kind(&self, expected: HashKind) -> Result<(), HashError>
Fail closed unless this value carries expected.
Use this at repository boundaries (pack trailers, ref-delta bases, index entries, protocol IDs) to reject an ID of the wrong algorithm instead of accepting a same-length value of another kind.
Sourcepub fn new_for_kind(kind: HashKind, data: &[u8]) -> ObjectHash
pub fn new_for_kind(kind: HashKind, data: &[u8]) -> ObjectHash
Compute the digest of data with an explicit kind.
Streams data through HashAlgorithm::new_for_kind; no copy of the
input is made. Does not consult the thread-local HashKind.
Sourcepub fn new(data: &[u8]) -> ObjectHash
pub fn new(data: &[u8]) -> ObjectHash
Compute hash of data using current thread-local HashKind.
Compatibility wrapper around ObjectHash::new_for_kind.
Sourcepub fn from_type_and_data_for_kind(
kind: HashKind,
object_type: ObjectType,
data: &[u8],
) -> Result<ObjectHash, HashError>
pub fn from_type_and_data_for_kind( kind: HashKind, object_type: ObjectType, data: &[u8], ) -> Result<ObjectHash, HashError>
Compute the canonical Git object ID hash("<type> <size>\0" + data) with an
explicit kind.
The header and payload are streamed into the hasher, so data is never
copied (only the decimal size string is formatted). Does not consult the
thread-local HashKind. Delta object types have no loose-object header
and are rejected with HashError::UnsupportedObjectType instead of
panicking.
Sourcepub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> ObjectHash
pub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> ObjectHash
Create ObjectHash from object type and data using the thread-local HashKind.
Legacy entry point; the header assembly below is the pre-refactor code
(including its behaviour for delta types, which have no loose-object
header and were never valid input here). Only the digest step was
redirected to ObjectHash::new_for_kind. New code should call
ObjectHash::from_type_and_data_for_kind, which is fallible and
streams the header instead of copying the payload.
Sourcepub fn from_bytes_for_kind(
kind: HashKind,
bytes: &[u8],
) -> Result<ObjectHash, HashError>
pub fn from_bytes_for_kind( kind: HashKind, bytes: &[u8], ) -> Result<ObjectHash, HashError>
Create an ObjectHash of an explicit kind from raw digest bytes.
Fails closed with HashError::InvalidLength when bytes.len() is not
kind.size(); it never re-interprets the bytes as another algorithm.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<ObjectHash, String>
pub fn from_bytes(bytes: &[u8]) -> Result<ObjectHash, String>
Create ObjectHash from raw bytes matching the current hash size.
Compatibility wrapper around ObjectHash::from_bytes_for_kind using the
thread-local HashKind.
Sourcepub fn from_hex_for_kind(
kind: HashKind,
hex_str: &str,
) -> Result<ObjectHash, HashError>
pub fn from_hex_for_kind( kind: HashKind, hex_str: &str, ) -> Result<ObjectHash, HashError>
Parse a raw (untagged) hex string as an ID of an explicit kind.
The string must be exactly kind.hex_len() characters of hex; a
64-character string is only accepted as the requested 32-byte kind and is
never guessed from its length. Fails closed with HashError.
Sourcepub fn to_tagged_string(&self) -> String
pub fn to_tagged_string(&self) -> String
Format as a tagged ID <kind>:<hex> (for example sha256:ab12…).
Use this representation wherever an ID leaves its repository context (APIs, indexes, logs). It must not be written into pack, tree/commit payloads or pkt-lines, which carry raw hex only.
Sourcepub fn from_tagged_str(tagged: &str) -> Result<ObjectHash, HashError>
pub fn from_tagged_str(tagged: &str) -> Result<ObjectHash, HashError>
Parse a tagged ID <kind>:<hex> produced by ObjectHash::to_tagged_string.
The tag selects the algorithm; the hex part must match that algorithm’s
length. Unknown tags, missing separators and length/hex errors fail
closed with HashError.
Sourcepub fn from_bytes_infer_kind(bytes: &[u8]) -> Result<ObjectHash, String>
👎Deprecated since 0.9.0: infers SHA-256 from a 32-byte length and can never represent BLAKE3; use ObjectHash::from_bytes_for_kind or HashAlgorithm::finalize_object_hash
pub fn from_bytes_infer_kind(bytes: &[u8]) -> Result<ObjectHash, String>
infers SHA-256 from a 32-byte length and can never represent BLAKE3; use ObjectHash::from_bytes_for_kind or HashAlgorithm::finalize_object_hash
Create ObjectHash from raw bytes, inferring the hash kind from the
byte length (20 → SHA-1, 32 → SHA-256).
Legacy contract (frozen): 20 bytes are always SHA-1 and 32 bytes are
always SHA-256 — never BLAKE3, even though BLAKE3 digests are also
32 bytes. This helper predates the explicit-kind API and will be
marked #[deprecated] once the remaining internal callers have moved to
ObjectHash::from_bytes_for_kind / HashAlgorithm::finalize_object_hash.
It must not be used on any new repository, pack or protocol path,
because a 32-byte digest of another algorithm would be silently
mislabelled as SHA-256. The two remaining pack-encode callers
(encode/mod.rs, encode/parallel.rs) are migrated to
HashAlgorithm::finalize_object_hash in B3-05, after which the
attribute is added.
Unlike ObjectHash::from_bytes, this does not consult the
thread-local HashKind, so it is safe on threads where
set_hash_kind was never called. The pack encoder finalizes its
running checksum on whichever async worker thread happens to run the
task; the checksum bytes already carry the correct length, while the
worker’s thread-local may still hold the default SHA-1 kind — the
mismatch used to panic with “Invalid byte length: got 32, expected 20”.
Sourcepub fn from_stream_for_kind(
kind: HashKind,
data: &mut impl Read,
) -> Result<ObjectHash, HashError>
pub fn from_stream_for_kind( kind: HashKind, data: &mut impl Read, ) -> Result<ObjectHash, HashError>
Read exactly kind.size() bytes from data as an ID of an explicit kind.
Does not consult the thread-local HashKind. A short stream fails
closed with HashError::InvalidLength carrying the bytes actually
read; other reader failures surface as HashError::Io.
Sourcepub fn from_stream(data: &mut impl Read) -> Result<ObjectHash>
pub fn from_stream(data: &mut impl Read) -> Result<ObjectHash>
Read hash bytes from a stream according to current hash size.
Legacy entry point using the thread-local HashKind. The error
surface is unchanged from the pre-refactor implementation: the
io::Error returned by Read::read_exact (a short stream is
UnexpectedEof). Only the byte-to-variant step goes through
ObjectHash::from_bytes_for_kind. New code should call
ObjectHash::from_stream_for_kind.
Sourcepub fn to_color_str(self) -> String
pub fn to_color_str(self) -> String
Format hash as colored string (for terminal display).
Sourcepub fn _to_string(&self) -> String
pub fn _to_string(&self) -> String
Faster string conversion than Display.
Sourcepub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
Get mutable access to inner byte slice.
Trait Implementations§
Source§impl Archive for ObjectHash
impl Archive for ObjectHash
Source§type Archived = ArchivedObjectHash
type Archived = ArchivedObjectHash
Source§type Resolver = ObjectHashResolver
type Resolver = ObjectHashResolver
Source§fn resolve(
&self,
resolver: <Self as Archive>::Resolver,
out: Place<<Self as Archive>::Archived>,
)
fn resolve( &self, resolver: <Self as Archive>::Resolver, out: Place<<Self as Archive>::Archived>, )
Source§const COPY_OPTIMIZATION: CopyOptimization<Self> = _
const COPY_OPTIMIZATION: CopyOptimization<Self> = _
serialize. Read moreSource§impl AsRef<[u8]> for ObjectHash
impl AsRef<[u8]> for ObjectHash
Source§impl Clone for ObjectHash
impl Clone for ObjectHash
impl Copy for ObjectHash
Source§impl Debug for ObjectHash
impl Debug for ObjectHash
Source§impl Default for ObjectHash
impl Default for ObjectHash
Source§impl<'de> Deserialize<'de> for ObjectHash
impl<'de> Deserialize<'de> for ObjectHash
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<__D: Fallible + ?Sized> Deserialize<ObjectHash, __D> for Archived<ObjectHash>
impl<__D: Fallible + ?Sized> Deserialize<ObjectHash, __D> for Archived<ObjectHash>
Source§fn deserialize(
&self,
deserializer: &mut __D,
) -> Result<ObjectHash, <__D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut __D, ) -> Result<ObjectHash, <__D as Fallible>::Error>
Source§impl Display for ObjectHash
impl Display for ObjectHash
impl Eq for ObjectHash
Source§impl FromStr for ObjectHash
Parse hex (40 for SHA1, 64 for SHA-256) into ObjectHash.
impl FromStr for ObjectHash
Parse hex (40 for SHA1, 64 for SHA-256) into ObjectHash.
Legacy, length-inferring parser. The algorithm is chosen from the hex
length alone (40 → SHA-1, 64 → SHA-256); the thread-local HashKind is
not consulted and BLAKE3 is never produced (a BLAKE3 ID has the same
64-hex width and would be mislabelled as SHA-256). Any other length is
rejected. When the repository format is known, prefer
ObjectHash::from_hex_for_kind; when the ID travels with its algorithm,
prefer ObjectHash::from_tagged_str.
Source§impl Hash for ObjectHash
impl Hash for ObjectHash
Source§impl HeapSize for ObjectHash
impl HeapSize for ObjectHash
Source§fn heap_size_sum_iter<'item, Fun, Iter>(make_iter: Fun) -> usize
fn heap_size_sum_iter<'item, Fun, Iter>(make_iter: Fun) -> usize
Source§fn heap_size_sum_exact_size_iter<'item, Fun, Iter>(make_iter: Fun) -> usizewhere
Self: 'item,
Fun: Fn() -> Iter,
Iter: ExactSizeIterator<Item = &'item Self>,
fn heap_size_sum_exact_size_iter<'item, Fun, Iter>(make_iter: Fun) -> usizewhere
Self: 'item,
Fun: Fn() -> Iter,
Iter: ExactSizeIterator<Item = &'item Self>,
Source§impl Ord for ObjectHash
impl Ord for ObjectHash
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for ObjectHash
impl PartialEq for ObjectHash
Source§impl PartialOrd for ObjectHash
impl PartialOrd for ObjectHash
Source§impl Serialize for ObjectHash
impl Serialize for ObjectHash
impl StructuralPartialEq for ObjectHash
Auto Trait Implementations§
impl Freeze for ObjectHash
impl RefUnwindSafe for ObjectHash
impl Send for ObjectHash
impl Sync for ObjectHash
impl Unpin for ObjectHash
impl UnsafeUnpin for ObjectHash
impl UnwindSafe for ObjectHash
Blanket Implementations§
impl<T> ArcWrapperBounds for T
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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<T> FileLoadStore for Twhere
T: Archive + for<'a> Serialize<Strategy<Serializer<AlignedVec, ArenaHandle<'a>, Share>, Error>>,
<T as Archive>::Archived: for<'a> CheckBytes<Strategy<Validator<ArchiveValidator<'a>, SharedValidator>, Error>> + Deserialize<T, Strategy<Pool, Error>>,
impl<T> FileLoadStore for Twhere
T: Archive + for<'a> Serialize<Strategy<Serializer<AlignedVec, ArenaHandle<'a>, Share>, Error>>,
<T as Archive>::Archived: for<'a> CheckBytes<Strategy<Validator<ArchiveValidator<'a>, SharedValidator>, Error>> + Deserialize<T, Strategy<Pool, Error>>,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, S> SerializeUnsized<S> for T
impl<T, S> SerializeUnsized<S> for T
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)