Skip to content

Commit

Permalink
Merge #158: Remove default impl of hex_reserve_suggestion
Browse files Browse the repository at this point in the history
908ce81 Remove default impl of hex_reserve_suggestion (Tobin C. Harding)

Pull request description:

  The current default impl returns 0, this reduces performance if implementors forget to implement this method and they know up front how many bytes are needed. The trait is sealed so we are the only implementors and it turns out we forgot in one place (well many places because its a macro).

  Remove the default implementation of `hex_reserve_suggestion` and implement it manually for arrays.

ACKs for top commit:
  Kixunil:
    ACK 908ce81
  apoelstra:
    ACK 908ce81; successfully ran local tests

Tree-SHA512: 36e4ea218ead9f01cc20a0050b8d02a574ba74f8cb8fbb8e83aa970eab11e3fb43c98d8117e04b94fcbaded333e16e69acc68ff2f10fe85298639794014afd69
  • Loading branch information
apoelstra committed Feb 23, 2025
2 parents 259d52e + 908ce81 commit 3dee66b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ pub trait DisplayHex: Copy + sealed::IsRef + sealed::Sealed {
})
}

/// Hints how much bytes to reserve when creating a `String`.
///
/// Implementors that know the number of produced bytes upfront should override this.
/// Defaults to 0.
/// Hints how many bytes to reserve when creating a `String`.
///
/// If you don't know you can just return 0 and take the perf hit.
// We prefix the name with `hex_` to avoid potential collision with other methods.
fn hex_reserve_suggestion(self) -> usize { 0 }
fn hex_reserve_suggestion(self) -> usize;
}

fn internal_display(bytes: &[u8], f: &mut fmt::Formatter, case: Case) -> fmt::Result {
Expand Down Expand Up @@ -344,6 +342,8 @@ macro_rules! impl_array_as_hex {
fn as_hex(self) -> Self::Display {
DisplayArray::new(self)
}

fn hex_reserve_suggestion(self) -> usize { $len * 2 }
}
)*
}
Expand Down

0 comments on commit 3dee66b

Please sign in to comment.