diff --git a/crates/primitives/src/bits/fixed.rs b/crates/primitives/src/bits/fixed.rs index 2fa20abe2..66eceeae2 100644 --- a/crates/primitives/src/bits/fixed.rs +++ b/crates/primitives/src/bits/fixed.rs @@ -292,28 +292,43 @@ impl FixedBytes { /// Array of Zero bytes. pub const ZERO: Self = Self([0u8; N]); - /// Instantiates a new fixed hash from the given bytes array. + /// Instantiates a new [`FixedBytes`] from the given bytes array. #[inline] pub const fn new(bytes: [u8; N]) -> Self { Self(bytes) } - /// Utility function to create a fixed hash with the last byte set to `x`. + /// Utility function to create a [`FixedBytes`] with the last byte set to + /// `x`. #[inline] pub const fn with_last_byte(x: u8) -> Self { let mut bytes = [0u8; N]; - bytes[N - 1] = x; + if N > 0 { + bytes[N - 1] = x; + } Self(bytes) } - /// Instantiates a new fixed hash with cryptographically random content. + /// Returns a new [`FixedBytes`] where all bits are set to the given byte. + #[inline] + pub const fn repeat_byte(byte: u8) -> Self { + Self([byte; N]) + } + + /// Returns the size of this byte array (`N`). + #[inline(always)] + pub const fn len_bytes() -> usize { + N + } + + /// Instantiates a new [`FixedBytes`] with cryptographically random content. #[cfg(feature = "getrandom")] #[inline] pub fn random() -> Self { Self::try_random().unwrap() } - /// Instantiates a new fixed hash with cryptographically random content. + /// Instantiates a new [`FixedBytes`] with cryptographically random content. #[cfg(feature = "getrandom")] pub fn try_random() -> Result { let mut bytes: [_; N] = crate::impl_core::uninit_array(); @@ -348,19 +363,7 @@ impl FixedBytes { FixedBytes(result) } - /// Returns a new fixed hash where all bits are set to the given byte. - #[inline] - pub const fn repeat_byte(byte: u8) -> Self { - Self([byte; N]) - } - - /// Returns the size of this hash in bytes. - #[inline] - pub const fn len_bytes() -> usize { - N - } - - /// Create a new fixed-hash from the given slice `src`. + /// Create a new [`FixedBytes`] from the given slice `src`. /// /// # Note /// @@ -371,8 +374,8 @@ impl FixedBytes { /// If the length of `src` and the number of bytes in `Self` do not match. #[track_caller] #[inline] - pub fn from_slice(src: &[u8]) -> Self { - Self(src.try_into().unwrap()) + pub fn from_slice(value: &[u8]) -> Self { + Self::try_from(value).unwrap() } /// Returns a slice containing the entire array. Equivalent to `&s[..]`. @@ -391,7 +394,7 @@ impl FixedBytes { /// Returns `true` if all bits set in `b` are also set in `self`. #[inline] pub fn covers(&self, b: &Self) -> bool { - &(*b & *self) == b + (*b & *self) == *b } /// Returns `true` if no bits are set. @@ -410,7 +413,6 @@ impl FixedBytes { } i += 1; } - true } @@ -460,7 +462,8 @@ impl FixedBytes { } else { buf.format(self) }; - f.write_str(&s[(!prefix as usize) * 2..]) + // SAFETY: The buffer is guaranteed to be at least 2 bytes in length. + f.write_str(unsafe { s.get_unchecked((!prefix as usize) * 2..) }) } } diff --git a/crates/primitives/src/signed/sign.rs b/crates/primitives/src/signed/sign.rs index 83be7f233..90c3d2d2b 100644 --- a/crates/primitives/src/signed/sign.rs +++ b/crates/primitives/src/signed/sign.rs @@ -7,15 +7,16 @@ use core::{ #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(i8)] pub enum Sign { - /// Greater than or equal to zero. - Positive = 1, /// Less than zero. Negative = -1, + /// Greater than or equal to zero. + Positive = 1, } impl ops::Mul for Sign { type Output = Self; + #[inline] fn mul(self, rhs: Self) -> Self::Output { match (self, rhs) { (Self::Positive, Self::Positive) => Self::Positive, @@ -29,6 +30,7 @@ impl ops::Mul for Sign { impl ops::Neg for Sign { type Output = Self; + #[inline] fn neg(self) -> Self::Output { match self { Self::Positive => Self::Negative, @@ -40,6 +42,7 @@ impl ops::Neg for Sign { impl ops::Not for Sign { type Output = Self; + #[inline] fn not(self) -> Self::Output { match self { Self::Positive => Self::Negative, @@ -59,24 +62,25 @@ impl fmt::Display for Sign { impl Sign { /// Equality at compile-time. + #[inline] pub const fn const_eq(self, other: Self) -> bool { self as i8 == other as i8 } /// Returns whether the sign is positive. - #[inline(always)] + #[inline] pub const fn is_positive(&self) -> bool { matches!(self, Self::Positive) } /// Returns whether the sign is negative. - #[inline(always)] + #[inline] pub const fn is_negative(&self) -> bool { matches!(self, Self::Negative) } /// Returns the sign character. - #[inline(always)] + #[inline] pub const fn as_char(&self) -> char { match self { Self::Positive => '+',