Skip to content

Commit ba05f63

Browse files
authored
Mark hex_bytes2hex_str_unchecked as unsafe (#15)
1 parent 1e4c742 commit ba05f63

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v4.1.0
2+
- Mark `hex_bytes2hex_str_unchecked` as unsafe.
3+
14
## v4.0.0
25
- Use `is_hex_ascii` to optimize performance.
36
- Add benchmark results.

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ license = "GPL-3.0"
1818
name = "array-bytes"
1919
readme = "README.md"
2020
repository = "https://github.com/hack-ink/array-bytes"
21-
version = "4.0.0"
21+
version = "4.1.0"
2222

2323
[badges]
2424
maintenance = { status = "actively-developed" }

benches/bench.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use rustc_hex::{FromHex, ToHex};
88
const DATA: &[u8] = include_bytes!("../src/lib.rs");
99

1010
fn bench_encode(c: &mut Criterion) {
11-
c.bench_function("array_bytes::bytes2hex", |b| {
12-
b.iter(|| array_bytes::bytes2hex("", DATA))
13-
});
11+
c.bench_function("array_bytes::bytes2hex", |b| b.iter(|| array_bytes::bytes2hex("", DATA)));
1412

1513
c.bench_function("hex::encode", |b| b.iter(|| hex::encode(DATA)));
1614

fuzz/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ edition = "2021"
44
metadata = { cargo-fuzz = true }
55
name = "array-bytes-fuzz"
66
publish = false
7-
version = "4.0.0"
7+
version = "4.1.0"
88

99
[dependencies]
10-
array-bytes = { path = ".." }
11-
libfuzzer-sys = "0.4"
10+
array-bytes = { version = "4.1", path = ".." }
11+
libfuzzer-sys = { version = "0.4" }
1212

1313
[workspace]
1414
members = ["."]

src/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,16 @@ pub fn hex_bytes2hex_str(bytes: &[u8]) -> ArrayBytesResult<&str> {
235235
///
236236
/// # Examples
237237
/// ```
238-
/// assert_eq!(
239-
/// array_bytes::hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"),
240-
/// "0x4c6f7665204a616e6520466f7265766572",
241-
/// );
238+
/// unsafe {
239+
/// assert_eq!(
240+
/// array_bytes::hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"),
241+
/// "0x4c6f7665204a616e6520466f7265766572",
242+
/// );
243+
/// }
242244
/// ```
243-
pub fn hex_bytes2hex_str_unchecked(bytes: &[u8]) -> &str {
244-
unsafe {
245-
#[allow(clippy::transmute_bytes_to_str)]
246-
mem::transmute(bytes)
247-
}
245+
pub unsafe fn hex_bytes2hex_str_unchecked(bytes: &[u8]) -> &str {
246+
#[allow(clippy::transmute_bytes_to_str)]
247+
mem::transmute(bytes)
248248
}
249249

250250
/// [`Bytes`] to [`Hex`].
@@ -599,5 +599,6 @@ where
599599
fn is_hex_ascii(byte: &u8) -> bool {
600600
// Convert to lowercase.
601601
let byte = byte | 0b10_0000;
602+
602603
matches!(byte, b'0'..=b'9' | b'a'..=b'f')
603604
}

src/test.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ fn hex_bytes2hex_str_should_work() {
105105

106106
#[test]
107107
fn hex_bytes2hex_str_unchecked_should_work() {
108-
assert_eq!(
109-
hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"),
110-
"0x4c6f7665204a616e6520466f7265766572",
111-
);
112-
assert_eq!(
113-
hex_bytes2hex_str_unchecked(b"4c6f7665204a616e6520466f7265766572"),
114-
"4c6f7665204a616e6520466f7265766572",
115-
);
108+
unsafe {
109+
assert_eq!(
110+
hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"),
111+
"0x4c6f7665204a616e6520466f7265766572",
112+
);
113+
assert_eq!(
114+
hex_bytes2hex_str_unchecked(b"4c6f7665204a616e6520466f7265766572"),
115+
"4c6f7665204a616e6520466f7265766572",
116+
);
117+
}
116118
}
117119

118120
#[test]

0 commit comments

Comments
 (0)