diff --git a/src/ext.rs b/src/ext.rs index 3c69061..8af87ac 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -5,16 +5,19 @@ use std::{ }, }; +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct HexSlice<'a>(&'a [u8]); impl<'a> HexSlice<'a> { + /// Create a new `HexSlice` wrapper #[inline] pub fn new(from: &'a T) -> Self where T: AsRef<[u8]> + ?Sized + 'a { Self(from.as_ref()) } + /// Create a hex string output pub fn to_hex_string(&self) -> String { @@ -60,3 +63,18 @@ impl AsRef<[u8]> for HexSlice<'_> self.0 } } + +#[cfg(test)] +mod tests +{ + use super::*; + #[test] + fn hex_encode_bytes() + { + let input = b"hello world"; + let output = input.as_hex(); + assert_eq!("68656c6c6f20776f726c64", output.to_hex_string()); + assert_eq!(input.as_hex(), output.as_hex()); + assert_eq!(format!("{}", output), "68656c6c6f20776f726c64"); + } +}