diff --git a/CHANGELOG b/CHANGELOG index 982cf0e..78e5e4d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +## v4.1.0 +- Mark `hex_bytes2hex_str_unchecked` as unsafe. + ## v4.0.0 - Use `is_hex_ascii` to optimize performance. - Add benchmark results. diff --git a/Cargo.lock b/Cargo.lock index 1875b58..233fa07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "array-bytes" -version = "4.0.0" +version = "4.1.0" dependencies = [ "criterion", "faster-hex", diff --git a/Cargo.toml b/Cargo.toml index 44938ca..6b11778 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ license = "GPL-3.0" name = "array-bytes" readme = "README.md" repository = "https://github.com/hack-ink/array-bytes" -version = "4.0.0" +version = "4.1.0" [badges] maintenance = { status = "actively-developed" } diff --git a/benches/bench.rs b/benches/bench.rs index f6ba86a..984ca46 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -8,9 +8,7 @@ use rustc_hex::{FromHex, ToHex}; const DATA: &[u8] = include_bytes!("../src/lib.rs"); fn bench_encode(c: &mut Criterion) { - c.bench_function("array_bytes::bytes2hex", |b| { - b.iter(|| array_bytes::bytes2hex("", DATA)) - }); + c.bench_function("array_bytes::bytes2hex", |b| b.iter(|| array_bytes::bytes2hex("", DATA))); c.bench_function("hex::encode", |b| b.iter(|| hex::encode(DATA))); diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 6031956..bcdcd20 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -4,11 +4,11 @@ edition = "2021" metadata = { cargo-fuzz = true } name = "array-bytes-fuzz" publish = false -version = "4.0.0" +version = "4.1.0" [dependencies] -array-bytes = { path = ".." } -libfuzzer-sys = "0.4" +array-bytes = { version = "4.1", path = ".." } +libfuzzer-sys = { version = "0.4" } [workspace] members = ["."] diff --git a/src/lib.rs b/src/lib.rs index ac3e9b7..08700f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -235,16 +235,16 @@ pub fn hex_bytes2hex_str(bytes: &[u8]) -> ArrayBytesResult<&str> { /// /// # Examples /// ``` -/// assert_eq!( -/// array_bytes::hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"), -/// "0x4c6f7665204a616e6520466f7265766572", -/// ); +/// unsafe { +/// assert_eq!( +/// array_bytes::hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"), +/// "0x4c6f7665204a616e6520466f7265766572", +/// ); +/// } /// ``` -pub fn hex_bytes2hex_str_unchecked(bytes: &[u8]) -> &str { - unsafe { - #[allow(clippy::transmute_bytes_to_str)] - mem::transmute(bytes) - } +pub unsafe fn hex_bytes2hex_str_unchecked(bytes: &[u8]) -> &str { + #[allow(clippy::transmute_bytes_to_str)] + mem::transmute(bytes) } /// [`Bytes`] to [`Hex`]. @@ -599,5 +599,6 @@ where fn is_hex_ascii(byte: &u8) -> bool { // Convert to lowercase. let byte = byte | 0b10_0000; + matches!(byte, b'0'..=b'9' | b'a'..=b'f') } diff --git a/src/test.rs b/src/test.rs index 7578222..0c8d11d 100644 --- a/src/test.rs +++ b/src/test.rs @@ -105,14 +105,16 @@ fn hex_bytes2hex_str_should_work() { #[test] fn hex_bytes2hex_str_unchecked_should_work() { - assert_eq!( - hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"), - "0x4c6f7665204a616e6520466f7265766572", - ); - assert_eq!( - hex_bytes2hex_str_unchecked(b"4c6f7665204a616e6520466f7265766572"), - "4c6f7665204a616e6520466f7265766572", - ); + unsafe { + assert_eq!( + hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"), + "0x4c6f7665204a616e6520466f7265766572", + ); + assert_eq!( + hex_bytes2hex_str_unchecked(b"4c6f7665204a616e6520466f7265766572"), + "4c6f7665204a616e6520466f7265766572", + ); + } } #[test]