Skip to content

Commit

Permalink
Merge #30: Inline public methods
Browse files Browse the repository at this point in the history
501a957 Inline public methods (Tobin C. Harding)

Pull request description:

  Audit the whole crate and add `#[inline]` as needed.

ACKs for top commit:
  apoelstra:
    ACK 501a957

Tree-SHA512: c42d621d5e65107dae5c4c11d49c8e6ba98a8d023c5c84b664bb7658fea060f9abaab4250a963e61c4bd2d971cf2f1e697107130c65761513f88bcd6d3c2987f
  • Loading branch information
apoelstra committed Aug 10, 2023
2 parents df03e23 + 501a957 commit ea40b56
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ where
A::Item: Borrow<u8>,
{
/// Creates the wrapper.
#[inline]
pub fn new(array: A) -> Self { DisplayArray { array, _buffer_marker: Default::default() } }

fn display(&self, f: &mut fmt::Formatter, case: Case) -> fmt::Result {
Expand Down
10 changes: 10 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'a> HexToBytesIter<'a> {
/// # Errors
///
/// If the input string is of odd length.
#[inline]
pub fn new(s: &'a str) -> Result<HexToBytesIter<'a>, HexToBytesError> {
if s.len() % 2 != 0 {
Err(HexToBytesError::OddLengthString(s.len()))
Expand All @@ -42,19 +43,22 @@ impl<'a> HexToBytesIter<'a> {
impl<'a> Iterator for HexToBytesIter<'a> {
type Item = Result<u8, HexToBytesError>;

#[inline]
fn next(&mut self) -> Option<Result<u8, HexToBytesError>> {
let hi = self.iter.next()?;
let lo = self.iter.next().expect("iter length invariant violated, this is a bug");
Some(hex_chars_to_byte(hi, lo))
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (min, max) = self.iter.size_hint();
(min / 2, max.map(|x| x / 2))
}
}

impl<'a> DoubleEndedIterator for HexToBytesIter<'a> {
#[inline]
fn next_back(&mut self) -> Option<Result<u8, HexToBytesError>> {
let lo = self.iter.next_back()?;
let hi = self.iter.next_back().expect("iter length invariant violated, this is a bug");
Expand All @@ -63,13 +67,15 @@ impl<'a> DoubleEndedIterator for HexToBytesIter<'a> {
}

impl<'a> ExactSizeIterator for HexToBytesIter<'a> {
#[inline]
fn len(&self) -> usize { self.iter.len() / 2 }
}

impl<'a> FusedIterator for HexToBytesIter<'a> {}

#[cfg(any(feature = "std", feature = "core2"))]
impl<'a> io::Read for HexToBytesIter<'a> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut bytes_read = 0usize;
for dst in buf {
Expand Down Expand Up @@ -116,6 +122,7 @@ where
{
type Item = char;

#[inline]
fn next(&mut self) -> Option<char> {
match self.low {
Some(c) => {
Expand All @@ -130,6 +137,7 @@ where
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (min, max) = self.iter.size_hint();
match self.low {
Expand All @@ -143,6 +151,7 @@ impl<I> DoubleEndedIterator for BytesToHexIter<I>
where
I: DoubleEndedIterator + Iterator<Item = u8>,
{
#[inline]
fn next_back(&mut self) -> Option<char> {
match self.low {
Some(c) => {
Expand All @@ -162,6 +171,7 @@ impl<I> ExactSizeIterator for BytesToHexIter<I>
where
I: ExactSizeIterator + Iterator<Item = u8>,
{
#[inline]
fn len(&self) -> usize { self.iter.len() * 2 }
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub enum Case {
}

impl Default for Case {
#[inline]
fn default() -> Self { Case::Lower }
}

Expand Down
1 change: 1 addition & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait FromHex: Sized {
impl FromHex for Vec<u8> {
type Err = HexToBytesError;

#[inline]
fn from_byte_iter<I>(iter: I) -> Result<Self, Self::Err>
where
I: Iterator<Item = Result<u8, HexToBytesError>> + ExactSizeIterator + DoubleEndedIterator,
Expand Down

0 comments on commit ea40b56

Please sign in to comment.