Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Aug 28, 2023
1 parent 3c6c7d4 commit f222aa6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
49 changes: 26 additions & 23 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,43 @@ impl<const N: usize> FixedBytes<N> {
/// 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<Self, getrandom::Error> {
let mut bytes: [_; N] = crate::impl_core::uninit_array();
Expand Down Expand Up @@ -348,19 +363,7 @@ impl<const N: usize> FixedBytes<N> {
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
///
Expand All @@ -371,8 +374,8 @@ impl<const N: usize> FixedBytes<N> {
/// 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[..]`.
Expand All @@ -391,7 +394,7 @@ impl<const N: usize> FixedBytes<N> {
/// 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.
Expand All @@ -410,7 +413,6 @@ impl<const N: usize> FixedBytes<N> {
}
i += 1;
}

true
}

Expand Down Expand Up @@ -460,7 +462,8 @@ impl<const N: usize> FixedBytes<N> {
} 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..) })
}
}

Expand Down
14 changes: 9 additions & 5 deletions crates/primitives/src/signed/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 => '+',
Expand Down

0 comments on commit f222aa6

Please sign in to comment.