From 3388a5fd20b9cf5af5dd1087e8ad328f5ff1a621 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 3 Jan 2025 17:52:07 +0800 Subject: [PATCH] Release `v9.1.0` --- CHANGELOG | 3 + Cargo.lock | 6 +- Cargo.toml | 2 +- README.md | 32 +++++------ src/hex/dehexify.rs | 3 + src/hex/hexify.rs | 131 ++++++++++---------------------------------- src/serde.rs | 8 +-- 7 files changed, 58 insertions(+), 127 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 034724b..8f71a69 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +### v9.1.0 +- Use reference instead of value in `Hexify` trait and related serialize functions. + ### v9.0.0 - Expose more friendly APIs, `Hexify` and `DeHexify` traits. - Un-public some tiny functions to encourage using `Hexify` and `DeHexify` traits. diff --git a/Cargo.lock b/Cargo.lock index 49847ae..22b3d2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "array-bytes" -version = "9.0.0" +version = "9.1.0" dependencies = [ "const-hex", "criterion", @@ -589,9 +589,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "syn" -version = "2.0.92" +version = "2.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126" +checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 58c63eb..260ed53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ license = "Apache-2.0/GPL-3.0" name = "array-bytes" readme = "README.md" repository = "https://github.com/hack-ink/array-bytes" -version = "9.0.0" +version = "9.1.0" [profile.ci-dev] incremental = false diff --git a/README.md b/README.md index e8c7575..1f3ef0e 100644 --- a/README.md +++ b/README.md @@ -84,26 +84,26 @@ assert_eq!( ## Benchmark The following benchmarks were run on a `Apple M4 Max 64GB - macOS 15.2 (24C101)`. -
Sun, Dec 29th, 2024
+
Fri, Jan 3rd, 2025
```rs // Hexify. -array_bytes::Hexify::hexify time: [11.195 µs 11.227 µs 11.264 µs] -const_hex::encode time: [1.0546 µs 1.0823 µs 1.1099 µs] -faster_hex::hex_string time: [12.054 µs 12.103 µs 12.154 µs] -faster_hex::hex_encode_fallback time: [12.170 µs 12.209 µs 12.245 µs] -hex::encode time: [87.014 µs 87.164 µs 87.312 µs] -rustc_hex::to_hex time: [45.022 µs 45.616 µs 46.304 µs] +array_bytes::Hexify::hexify time: [10.978 µs 10.997 µs 11.021 µs] +const_hex::encode time: [941.68 ns 946.55 ns 951.44 ns] +faster_hex::hex_string time: [11.478 µs 11.498 µs 11.519 µs] +faster_hex::hex_encode_fallback time: [11.546 µs 11.563 µs 11.580 µs] +hex::encode time: [85.347 µs 85.524 µs 85.751 µs] +rustc_hex::to_hex time: [46.267 µs 47.009 µs 47.759 µs] // Dehexify. -array_bytes::Dehexify::dehexify time: [19.601 µs 19.815 µs 20.061 µs] -array_bytes::dehexify_slice_mut time: [20.455 µs 20.471 µs 20.489 µs] -const_hex::decode time: [14.098 µs 14.118 µs 14.137 µs] -faster_hex::hex_decode time: [29.356 µs 29.395 µs 29.435 µs] -faster_hex::hex_decode_unchecked time: [12.089 µs 12.134 µs 12.208 µs] -faster_hex::hex_decode_fallback time: [12.067 µs 12.082 µs 12.098 µs] -hex::decode time: [97.005 µs 98.854 µs 100.65 µs] -hex::decode_to_slice time: [39.262 µs 40.562 µs 42.064 µs] -rustc_hex::from_hex time: [108.91 µs 110.77 µs 112.53 µs] +array_bytes::Dehexify::dehexify time: [19.143 µs 19.156 µs 19.173 µs] +array_bytes::dehexify_slice_mut time: [20.245 µs 20.274 µs 20.307 µs] +const_hex::decode time: [13.861 µs 14.276 µs 14.975 µs] +faster_hex::hex_decode time: [28.499 µs 28.545 µs 28.593 µs] +faster_hex::hex_decode_unchecked time: [11.775 µs 11.799 µs 11.828 µs] +faster_hex::hex_decode_fallback time: [11.818 µs 11.840 µs 11.862 µs] +hex::decode time: [90.870 µs 91.481 µs 92.126 µs] +hex::decode_to_slice time: [32.272 µs 32.553 µs 32.927 µs] +rustc_hex::from_hex time: [106.68 µs 107.45 µs 108.31 µs] ``` To run the benchmarks yourself: diff --git a/src/hex/dehexify.rs b/src/hex/dehexify.rs index 0c4f51a..30d00b0 100644 --- a/src/hex/dehexify.rs +++ b/src/hex/dehexify.rs @@ -29,6 +29,9 @@ static HEX2DIGIT: [Option; 256] = { /// /// # Examples /// ``` +/// use array_bytes::{Dehexify, Error}; +/// use smallvec::SmallVec; +/// /// // Unsigned. /// assert_eq!(u8::dehexify("34"), Ok(52)); /// assert_eq!(u16::dehexify("208"), Ok(520)); diff --git a/src/hex/hexify.rs b/src/hex/hexify.rs index 60cae0b..8a13838 100644 --- a/src/hex/hexify.rs +++ b/src/hex/hexify.rs @@ -44,16 +44,16 @@ const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF"; /// ``` pub trait Hexify { /// Hexify `Self`. - fn hexify(self) -> String; + fn hexify(&self) -> String; /// Hexify `Self` with uppercase. - fn hexify_upper(self) -> String; + fn hexify_upper(&self) -> String; /// Hexify `Self` with `0x` prefix. - fn hexify_prefixed(self) -> String; + fn hexify_prefixed(&self) -> String; /// Hexify `Self` with `0x` prefix and uppercase. - fn hexify_prefixed_upper(self) -> String; + fn hexify_prefixed_upper(&self) -> String; } macro_rules! hexify_unsigned { ($self:expr, $map:expr) => {{ @@ -103,40 +103,22 @@ macro_rules! impl_hexify_for_unsigned { ($($t:ty,)+) => { $( impl Hexify for $t { - fn hexify(self) -> String { + fn hexify(&self) -> String { hexify_unsigned!(self, HEX_CHARS) } - fn hexify_upper(self) -> String { + fn hexify_upper(&self) -> String { hexify_unsigned!(self, HEX_CHARS_UPPER) } - fn hexify_prefixed(self) -> String { + fn hexify_prefixed(&self) -> String { hexify_unsigned_prefixed!(self, HEX_CHARS) } - fn hexify_prefixed_upper(self) -> String { + fn hexify_prefixed_upper(&self) -> String { hexify_unsigned_prefixed!(self, HEX_CHARS_UPPER) } } - - impl Hexify for &$t { - fn hexify(self) -> String { - (*self).hexify() - } - - fn hexify_upper(self) -> String { - (*self).hexify_upper() - } - - fn hexify_prefixed(self) -> String { - (*self).hexify_prefixed() - } - - fn hexify_prefixed_upper(self) -> String { - (*self).hexify_prefixed_upper() - } - } )+ }; } @@ -202,90 +184,33 @@ macro_rules! hexify_prefixed { unsafe { String::from_utf8_unchecked(hex_bytes.into_vec()) } }}; } -impl Hexify for [u8; N] { - fn hexify(self) -> String { - hexify!(self, HEX_CHARS) - } +macro_rules! hexify_bytes_fns { + () => { + fn hexify(&self) -> String { + hexify!(self, HEX_CHARS) + } - fn hexify_upper(self) -> String { - hexify!(self, HEX_CHARS_UPPER) - } + fn hexify_upper(&self) -> String { + hexify!(self, HEX_CHARS_UPPER) + } - fn hexify_prefixed(self) -> String { - hexify_prefixed!(self, HEX_CHARS) - } + fn hexify_prefixed(&self) -> String { + hexify_prefixed!(self, HEX_CHARS) + } - fn hexify_prefixed_upper(self) -> String { - hexify_prefixed!(self, HEX_CHARS_UPPER) - } + fn hexify_prefixed_upper(&self) -> String { + hexify_prefixed!(self, HEX_CHARS_UPPER) + } + }; } -impl Hexify for &[u8; N] { - fn hexify(self) -> String { - hexify!(self, HEX_CHARS) - } - - fn hexify_upper(self) -> String { - hexify!(self, HEX_CHARS_UPPER) - } - - fn hexify_prefixed(self) -> String { - hexify_prefixed!(self, HEX_CHARS) - } - - fn hexify_prefixed_upper(self) -> String { - hexify_prefixed!(self, HEX_CHARS_UPPER) - } +impl Hexify for [u8; N] { + hexify_bytes_fns! {} } -impl Hexify for &[u8] { - fn hexify(self) -> String { - hexify!(self, HEX_CHARS) - } - - fn hexify_upper(self) -> String { - hexify!(self, HEX_CHARS_UPPER) - } - - fn hexify_prefixed(self) -> String { - hexify_prefixed!(self, HEX_CHARS) - } - - fn hexify_prefixed_upper(self) -> String { - hexify_prefixed!(self, HEX_CHARS_UPPER) - } +impl Hexify for [u8] { + hexify_bytes_fns! {} } impl Hexify for Vec { - fn hexify(self) -> String { - hexify!(self, HEX_CHARS) - } - - fn hexify_upper(self) -> String { - hexify!(self, HEX_CHARS_UPPER) - } - - fn hexify_prefixed(self) -> String { - hexify_prefixed!(self, HEX_CHARS) - } - - fn hexify_prefixed_upper(self) -> String { - hexify_prefixed!(self, HEX_CHARS_UPPER) - } -} -impl Hexify for &Vec { - fn hexify(self) -> String { - hexify!(self, HEX_CHARS) - } - - fn hexify_upper(self) -> String { - hexify!(self, HEX_CHARS_UPPER) - } - - fn hexify_prefixed(self) -> String { - hexify_prefixed!(self, HEX_CHARS) - } - - fn hexify_prefixed_upper(self) -> String { - hexify_prefixed!(self, HEX_CHARS_UPPER) - } + hexify_bytes_fns! {} } #[test] fn hexify_should_work() { diff --git a/src/serde.rs b/src/serde.rs index a989327..960955e 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -29,7 +29,7 @@ use crate::{prelude::*, Dehexify, Hexify}; /// r#"{"_0":"5","_1":"2","_2":"0","_3":"01030104"}"# /// ); /// ``` -pub fn ser_hexify(value: T, serializer: S) -> Result +pub fn ser_hexify(value: &T, serializer: S) -> Result where S: Serializer, T: Hexify, @@ -60,7 +60,7 @@ where /// r#"{"_0":"5","_1":"2","_2":"0","_3":"01030104"}"# /// ); /// ``` -pub fn ser_hexify_upper(value: T, serializer: S) -> Result +pub fn ser_hexify_upper(value: &T, serializer: S) -> Result where S: Serializer, T: Hexify, @@ -91,7 +91,7 @@ where /// r#"{"_0":"0x5","_1":"0x2","_2":"0x0","_3":"0x01030104"}"# /// ); /// ``` -pub fn ser_hexify_prefixed(value: T, serializer: S) -> Result +pub fn ser_hexify_prefixed(value: &T, serializer: S) -> Result where T: Hexify, S: Serializer, @@ -122,7 +122,7 @@ where /// r#"{"_0":"0x5","_1":"0x2","_2":"0x0","_3":"0x01030104"}"# /// ); /// ``` -pub fn ser_hexify_prefixed_upper(value: T, serializer: S) -> Result +pub fn ser_hexify_prefixed_upper(value: &T, serializer: S) -> Result where S: Serializer, T: Hexify,