Remove hashes::hex::HexWriter

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.
This commit is contained in:
Tobin C. Harding 2023-01-22 12:05:08 +11:00
parent d66ee48482
commit e079524b2a
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 1 additions and 77 deletions

View File

@ -16,7 +16,7 @@
//!
#[cfg(any(feature = "std", feature = "alloc"))]
use crate::alloc::{string::String, vec::Vec};
use crate::alloc::vec::Vec;
#[cfg(any(test, feature = "std"))]
use std::io;
@ -151,43 +151,6 @@ impl<'a> DoubleEndedIterator 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_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl FromHex for Vec<u8> {
@ -246,8 +209,6 @@ mod tests {
#[cfg(any(feature = "std", feature = "alloc"))]
use internals::hex::exts::DisplayHex;
use std::io::Write;
#[test]
#[cfg(any(feature = "std", feature = "alloc"))]
fn hex_roundtrip() {
@ -303,41 +264,4 @@ mod tests {
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());
})
}
}