Skip to content

Commit

Permalink
Merge #165: Inline small functions
Browse files Browse the repository at this point in the history
ff54528 Inline small functions (Jamil Lambert, PhD)

Pull request description:

  Functions that just delegate or do trivial operations should be inline.

  Add `#[inline]` to all functions that fit the criteria.

ACKs for top commit:
  tcharding:
    ACK ff54528
  apoelstra:
    ACK ff54528; successfully ran local tests

Tree-SHA512: b6a3bcadd7ed981f7af3b407b13ecd7377b4213ad839e238336b2861fc484d407c3fc94c501ef11e00ed1699b06aec480ebf4f6aaa449f1a0253f693e790e04f
  • Loading branch information
apoelstra committed Mar 4, 2025
2 parents 1959c66 + ff54528 commit f10fa34
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/buf_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl<const CAP: usize> BufEncoder<CAP> {
}

impl<const CAP: usize> Default for BufEncoder<CAP> {
#[inline]
fn default() -> Self { Self::new(Case::Lower) }
}

Expand Down
12 changes: 12 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait DisplayHex: Copy + sealed::IsRef + sealed::Sealed {
///
/// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`.
#[cfg(feature = "alloc")]
#[inline]
fn to_lower_hex_string(self) -> String { self.to_hex_string(Case::Lower) }

/// Create an upper-hex-encoded string.
Expand All @@ -66,6 +67,7 @@ pub trait DisplayHex: Copy + sealed::IsRef + sealed::Sealed {
///
/// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`.
#[cfg(feature = "alloc")]
#[inline]
fn to_upper_hex_string(self) -> String { self.to_hex_string(Case::Upper) }

/// Create a hex-encoded string.
Expand Down Expand Up @@ -272,24 +274,29 @@ pub struct DisplayByteSlice<'a> {
}

impl DisplayByteSlice<'_> {
#[inline]
fn display(&self, f: &mut fmt::Formatter, case: Case) -> fmt::Result {
internal_display(self.bytes, f, case)
}
}

impl fmt::Display for DisplayByteSlice<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl fmt::Debug for DisplayByteSlice<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl fmt::LowerHex for DisplayByteSlice<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.display(f, Case::Lower) }
}

impl fmt::UpperHex for DisplayByteSlice<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.display(f, Case::Upper) }
}

Expand All @@ -312,24 +319,29 @@ impl<'a, const CAP: usize> DisplayArray<'a, CAP> {
DisplayArray { array }
}

#[inline]
fn display(&self, f: &mut fmt::Formatter, case: Case) -> fmt::Result {
internal_display(self.array, f, case)
}
}

impl<const LEN: usize> fmt::Display for DisplayArray<'_, LEN> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl<const LEN: usize> fmt::Debug for DisplayArray<'_, LEN> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl<const LEN: usize> fmt::LowerHex for DisplayArray<'_, LEN> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.display(f, Case::Lower) }
}

impl<const LEN: usize> fmt::UpperHex for DisplayArray<'_, LEN> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.display(f, Case::Upper) }
}

Expand Down
20 changes: 20 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ macro_rules! write_err {
pub struct HexToBytesError(pub(crate) ToBytesError);

impl From<Infallible> for HexToBytesError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

impl HexToBytesError {
/// Returns a [`ToBytesError`] from this [`HexToBytesError`].
// Use clone instead of reference to give use maximum forward flexibility.
#[inline]
pub fn parse_error(&self) -> ToBytesError { self.0.clone() }
}

impl fmt::Display for HexToBytesError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}

#[cfg(feature = "std")]
impl std::error::Error for HexToBytesError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

Expand All @@ -69,6 +73,7 @@ pub enum ToBytesError {
}

impl From<Infallible> for ToBytesError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

Expand Down Expand Up @@ -114,17 +119,21 @@ pub struct InvalidCharError {
}

impl From<Infallible> for InvalidCharError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

impl InvalidCharError {
/// Returns the invalid character byte.
#[inline]
pub fn invalid_char(&self) -> u8 { self.invalid }
/// Returns the position of the invalid character byte.
#[inline]
pub fn pos(&self) -> usize { self.pos }
}

impl fmt::Display for InvalidCharError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid hex char {} at pos {}", self.invalid_char(), self.pos())
}
Expand All @@ -140,15 +149,18 @@ pub struct OddLengthStringError {
}

impl From<Infallible> for OddLengthStringError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

impl OddLengthStringError {
/// Returns the odd length of the input string.
#[inline]
pub fn length(&self) -> usize { self.len }
}

impl fmt::Display for OddLengthStringError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "odd hex string length {}", self.length())
}
Expand All @@ -162,21 +174,25 @@ impl std::error::Error for OddLengthStringError {}
pub struct HexToArrayError(pub(crate) ToArrayError);

impl From<Infallible> for HexToArrayError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

impl HexToArrayError {
/// Returns a [`ToArrayError`] from this [`HexToArrayError`].
// Use clone instead of reference to give use maximum forward flexibility.
#[inline]
pub fn parse_error(&self) -> ToArrayError { self.0.clone() }
}

impl fmt::Display for HexToArrayError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}

#[cfg(feature = "std")]
impl std::error::Error for HexToArrayError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

Expand All @@ -200,6 +216,7 @@ pub enum ToArrayError {
}

impl From<Infallible> for ToArrayError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

Expand Down Expand Up @@ -246,13 +263,16 @@ pub struct InvalidLengthError {
}

impl From<Infallible> for InvalidLengthError {
#[inline]
fn from(never: Infallible) -> Self { match never {} }
}

impl InvalidLengthError {
/// Returns the expected length.
#[inline]
pub fn expected_length(&self) -> usize { self.expected }
/// Returns the position of the invalid character byte.
#[inline]
pub fn invalid_length(&self) -> usize { self.invalid }
}

Expand Down
1 change: 1 addition & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<'a> HexToBytesIter<HexDigitsIter<'a>> {
}
}

#[inline]
pub(crate) fn new_unchecked(s: &'a str) -> Self {
Self::from_pairs(HexDigitsIter::new_unchecked(s.as_bytes()))
}
Expand Down
1 change: 1 addition & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait FromHex: Sized + sealed::Sealed {
impl FromHex for Vec<u8> {
type Error = HexToBytesError;

#[inline]
fn from_hex(s: &str) -> Result<Self, Self::Error> {
Ok(HexToBytesIter::new(s)?.drain_to_vec()?)
}
Expand Down

0 comments on commit f10fa34

Please sign in to comment.