Skip to content

Commit

Permalink
Mark hex_bytes2hex_str_unchecked as unsafe (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Sep 7, 2022
1 parent 1e4c742 commit ba05f63
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 1 addition & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down
6 changes: 3 additions & 3 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["."]
Expand Down
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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')
}
18 changes: 10 additions & 8 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ba05f63

Please sign in to comment.