Merge rust-bitcoin/rust-bitcoin#1572: Remove `hashes::hex::HexWriter`

e079524b2a Remove hashes::hex::HexWriter (Tobin C. Harding)

Pull request description:

  The `HexWriter` is not used any more since we added the new hex code in internals for fast hex encoding.

  While we are removing the benches for `HexWriter` also remove the last remaining bench for writing using `Display` because this is not the correct place for that code - its trivial to re add later in the correct module.

ACKs for top commit:
  Kixunil:
    ACK e079524b2a
  apoelstra:
    ACK e079524b2a

Tree-SHA512: 3896ad55e9e6f0e30d5494f008d942d7cb2ab264c97205d184abdb1f560704d721514ca9878af6012d1c10a2b8753e86542af7137726c1620b8c363ff49b0a61
This commit is contained in:
Andrew Poelstra 2023-01-24 20:56:48 +00:00
commit 2a052acadf
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 1 additions and 77 deletions

View File

@ -16,7 +16,7 @@
//! //!
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
use crate::alloc::{string::String, vec::Vec}; use crate::alloc::vec::Vec;
#[cfg(any(test, feature = "std"))] #[cfg(any(test, feature = "std"))]
use std::io; use std::io;
@ -151,43 +151,6 @@ impl<'a> DoubleEndedIterator for HexIterator<'a> {
impl<'a> ExactSizeIterator for HexIterator<'a> {} impl<'a> ExactSizeIterator for HexIterator<'a> {}
/// A struct implementing [`io::Write`] that converts what's written to it into
/// a hex String.
#[cfg(any(test, feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(test, feature = "std", feature = "alloc"))))]
pub struct HexWriter(String);
#[cfg(any(test, feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(test, feature = "std", feature = "alloc"))))]
impl HexWriter {
/// Creates a new [`HexWriter`] with the `capacity` of the inner `String`
/// that will contain final hex value.
pub fn new(capacity: usize) -> Self {
HexWriter(String::with_capacity(capacity))
}
/// Returns the resulting hex string.
pub fn result(self) -> String {
self.0
}
}
#[cfg(any(test, feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(test, feature = "std", feature = "alloc"))))]
impl io::Write for HexWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
use core::fmt::Write;
for ch in buf {
write!(self.0, "{:02x}", ch).expect("writing to string");
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
#[cfg(any(test, feature = "std", feature = "alloc"))] #[cfg(any(test, feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl FromHex for Vec<u8> { impl FromHex for Vec<u8> {
@ -246,8 +209,6 @@ mod tests {
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
use internals::hex::exts::DisplayHex; use internals::hex::exts::DisplayHex;
use std::io::Write;
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
fn hex_roundtrip() { fn hex_roundtrip() {
@ -303,41 +264,4 @@ mod tests {
Err(Error::InvalidChar(194)) Err(Error::InvalidChar(194))
); );
} }
#[test]
#[cfg(any(feature = "std", feature = "alloc"))]
fn hex_writer() {
let vec: Vec<_> = (0u8..32).collect();
let mut writer = HexWriter::new(64);
writer.write_all(&vec[..]).unwrap();
assert_eq!(vec.to_lower_hex_string(), writer.result());
}
}
#[cfg(bench)]
mod benches {
use test::{Bencher, black_box};
use super::HexWriter;
use std::io::Write;
use crate::{sha256, Hash};
#[bench]
fn bench_to_hex(bh: &mut Bencher) {
let hash = sha256::Hash::hash(&[0; 1]);
bh.iter(|| {
black_box(hash.to_string());
})
}
#[bench]
fn bench_to_hex_writer(bh: &mut Bencher) {
let hash = sha256::Hash::hash(&[0; 1]);
bh.iter(|| {
let mut writer = HexWriter::new(64);
writer.write_all(hash.as_inner()).unwrap();
black_box(writer.result());
})
}
} }