Skip to content

Commit 7cad036

Browse files
committed
Add hex_bytes2hex_str_unchecked
1 parent 90d159c commit 7cad036

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
- type `&[u8] -> String`
3838

3939
#### `hex` Prefixed Functions
40+
- Convert `HexBytes` to `Hex`
41+
- type `&[u8] -> &str`
42+
- e.g. `b"0x..." -> "0x..."`
4043
- Build fixed length `Array` from `Hex`
4144
- type `&str -> [u8; N]`
4245
- Convert `Hex` to `Bytes`

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ pub fn hex_bytes2hex_str(bytes: &[u8]) -> ArrayBytesResult<&str> {
231231
})
232232
}
233233

234+
/// Just like [`hex_bytes2hex_str`] but without the checking.
235+
///
236+
/// # Examples
237+
/// ```
238+
/// assert_eq!(
239+
/// array_bytes::hex_bytes2hex_str_unchecked(b"0x4c6f7665204a616e6520466f7265766572"),
240+
/// "0x4c6f7665204a616e6520466f7265766572",
241+
/// );
242+
/// ```
243+
pub fn hex_bytes2hex_str_unchecked(bytes: &[u8]) -> &str {
244+
unsafe {
245+
#[allow(clippy::transmute_bytes_to_str)]
246+
mem::transmute(bytes)
247+
}
248+
}
249+
234250
/// [`Bytes`] to [`Hex`].
235251
///
236252
/// # Examples

src/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ fn hex_bytes2hex_str_should_work() {
103103
);
104104
}
105105

106+
#[test]
107+
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+
);
116+
}
117+
106118
#[test]
107119
fn hex2array_should_work() {
108120
assert_eq!(hex2array("0x4c6f7665204a616e6520466f7265766572"), Ok(*b"Love Jane Forever"));

0 commit comments

Comments
 (0)