diff options
Diffstat (limited to 'vendor/gix-hash/src')
-rw-r--r-- | vendor/gix-hash/src/kind.rs | 14 | ||||
-rw-r--r-- | vendor/gix-hash/src/lib.rs | 11 | ||||
-rw-r--r-- | vendor/gix-hash/src/object_id.rs | 22 | ||||
-rw-r--r-- | vendor/gix-hash/src/oid.rs | 32 |
4 files changed, 46 insertions, 33 deletions
diff --git a/vendor/gix-hash/src/kind.rs b/vendor/gix-hash/src/kind.rs index 86faddda2..902cf705d 100644 --- a/vendor/gix-hash/src/kind.rs +++ b/vendor/gix-hash/src/kind.rs @@ -2,12 +2,6 @@ use std::{convert::TryFrom, str::FromStr}; use crate::{oid, Kind, ObjectId}; -impl Default for Kind { - fn default() -> Self { - Kind::Sha1 - } -} - impl TryFrom<u8> for Kind { type Error = u8; @@ -39,13 +33,13 @@ impl std::fmt::Display for Kind { } impl Kind { - /// Returns the shortest hash we support + /// Returns the shortest hash we support. #[inline] pub const fn shortest() -> Self { Self::Sha1 } - /// Returns the longest hash we support + /// Returns the longest hash we support. #[inline] pub const fn longest() -> Self { Self::Sha1 @@ -63,14 +57,14 @@ impl Kind { [0u8; Kind::longest().len_in_bytes()] } - /// Returns the amount of ascii-characters needed to encode this has in hex + /// Returns the amount of ascii-characters needed to encode this has in hex. #[inline] pub const fn len_in_hex(&self) -> usize { match self { Kind::Sha1 => 40, } } - /// Returns the amount of bytes taken up by the hash of the current kind + /// Returns the amount of bytes taken up by the hash of the current kind. #[inline] pub const fn len_in_bytes(&self) -> usize { match self { diff --git a/vendor/gix-hash/src/lib.rs b/vendor/gix-hash/src/lib.rs index c40c4b8f0..49940f48e 100644 --- a/vendor/gix-hash/src/lib.rs +++ b/vendor/gix-hash/src/lib.rs @@ -22,20 +22,21 @@ pub mod prefix; /// An partial owned hash possibly identifying an object uniquely, /// whose non-prefix bytes are zeroed. #[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy, Debug)] -#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Prefix { bytes: ObjectId, hex_len: usize, } -/// The size of a SHA1 hash digest in bytes +/// The size of a SHA1 hash digest in bytes. const SIZE_OF_SHA1_DIGEST: usize = 20; -/// Denotes the kind of function to produce a `Id` -#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] -#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] +/// Denotes the kind of function to produce a `Id`. +#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Kind { /// The Sha1 hash with 160 bits. + #[default] Sha1 = 1, } diff --git a/vendor/gix-hash/src/object_id.rs b/vendor/gix-hash/src/object_id.rs index d295fc555..2d9bcef33 100644 --- a/vendor/gix-hash/src/object_id.rs +++ b/vendor/gix-hash/src/object_id.rs @@ -10,7 +10,7 @@ use crate::{borrowed::oid, Kind, SIZE_OF_SHA1_DIGEST}; /// An owned hash identifying objects, most commonly Sha1 #[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy)] -#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum ObjectId { /// A SHA 1 hash digest Sha1([u8; SIZE_OF_SHA1_DIGEST]), @@ -22,7 +22,7 @@ pub enum ObjectId { // extremely unlikely to begin with so it doesn't matter. // This implementation matches the `Hash` implementation for `oid` // and allows the usage of custom Hashers that only copy a truncated ShaHash -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] impl Hash for ObjectId { fn hash<H: Hasher>(&self, state: &mut H) { state.write(self.as_slice()) @@ -77,21 +77,21 @@ pub mod decode { /// Access and conversion impl ObjectId { - /// Returns the kind of hash used in this `Id` + /// Returns the kind of hash used in this `Id`. #[inline] pub fn kind(&self) -> crate::Kind { match self { ObjectId::Sha1(_) => crate::Kind::Sha1, } } - /// Return the raw byte slice representing this hash + /// Return the raw byte slice representing this hash. #[inline] pub fn as_slice(&self) -> &[u8] { match self { Self::Sha1(b) => b.as_ref(), } } - /// Return the raw mutable byte slice representing this hash + /// Return the raw mutable byte slice representing this hash. #[inline] pub fn as_mut_slice(&mut self) -> &mut [u8] { match self { @@ -99,7 +99,7 @@ impl ObjectId { } } - /// The hash of an empty blob + /// The hash of an empty blob. #[inline] pub const fn empty_blob(hash: Kind) -> ObjectId { match hash { @@ -109,7 +109,7 @@ impl ObjectId { } } - /// The hash of an empty tree + /// The hash of an empty tree. #[inline] pub const fn empty_tree(hash: Kind) -> ObjectId { match hash { @@ -119,7 +119,7 @@ impl ObjectId { } } - /// Returns true if this hash consists of all null bytes + /// Returns true if this hash consists of all null bytes. #[inline] pub fn is_null(&self) -> bool { match self { @@ -127,6 +127,12 @@ impl ObjectId { } } + /// Returns `true` if this hash is equal to an empty blob. + #[inline] + pub fn is_empty_blob(&self) -> bool { + self == &Self::empty_blob(self.kind()) + } + /// Returns an Digest representing a hash with whose memory is zeroed. #[inline] pub const fn null(kind: crate::Kind) -> ObjectId { diff --git a/vendor/gix-hash/src/oid.rs b/vendor/gix-hash/src/oid.rs index 92ded0f87..20d4fb4b6 100644 --- a/vendor/gix-hash/src/oid.rs +++ b/vendor/gix-hash/src/oid.rs @@ -1,4 +1,4 @@ -use std::{convert::TryInto, fmt}; +use std::{convert::TryInto, fmt, hash}; use crate::{ObjectId, SIZE_OF_SHA1_DIGEST}; @@ -14,15 +14,27 @@ use crate::{ObjectId, SIZE_OF_SHA1_DIGEST}; /// hash `[`kind()`][oid::kind()]`. /// We expect to have quite a few bits available for such 'conflict resolution' as most hashes aren't longer /// than 64 bytes. -#[derive(PartialEq, Eq, Hash, Ord, PartialOrd)] +#[derive(PartialEq, Eq, Ord, PartialOrd)] #[repr(transparent)] #[allow(non_camel_case_types)] -#[cfg_attr(feature = "serde1", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct oid { bytes: [u8], } -/// A utility able to format itself with the given amount of characters in hex +// False positive: +// Using an automatic implementation of `Hash` for `oid` would lead to +// it attempting to hash the length of the slice first. On 32 bit systems +// this can lead to issues with the custom `gix_hashtable` `Hasher` implementation, +// and it currently ends up being discarded there anyway. +#[allow(clippy::derived_hash_with_manual_eq)] +impl hash::Hash for oid { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + state.write(self.as_bytes()) + } +} + +/// A utility able to format itself with the given amount of characters in hex. #[derive(PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct HexDisplay<'a> { inner: &'a oid, @@ -79,7 +91,7 @@ impl oid { Self::from_bytes(value) } - /// Only from code that statically assures correct sizes using array conversions + /// Only from code that statically assures correct sizes using array conversions. pub(crate) fn from_bytes(value: &[u8]) -> &Self { #[allow(unsafe_code)] unsafe { @@ -90,13 +102,13 @@ impl oid { /// Access impl oid { - /// The kind of hash used for this Digest + /// The kind of hash used for this Digest. #[inline] pub fn kind(&self) -> crate::Kind { crate::Kind::from_len_in_bytes(self.bytes.len()) } - /// The first byte of the hash, commonly used to partition a set of `Id`s + /// The first byte of the hash, commonly used to partition a set of `Id`s. #[inline] pub fn first_byte(&self) -> u8 { self.bytes[0] @@ -140,7 +152,7 @@ impl oid { num_hex_bytes } - /// Write ourselves to `out` in hexadecimal notation + /// Write ourselves to `out` in hexadecimal notation. #[inline] pub fn write_hex_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> { let mut hex = crate::Kind::hex_buf(); @@ -193,9 +205,9 @@ impl PartialEq<crate::ObjectId> for &oid { } /// Manually created from a version that uses a slice, and we forcefully try to convert it into a borrowed array of the desired size -/// Could be improved by fitting this into serde +/// Could be improved by fitting this into serde. /// Unfortunately the serde::Deserialize derive wouldn't work for borrowed arrays. -#[cfg(feature = "serde1")] +#[cfg(feature = "serde")] impl<'de: 'a, 'a> serde::Deserialize<'de> for &'a oid { fn deserialize<D>(deserializer: D) -> Result<Self, <D as serde::Deserializer<'de>>::Error> where |