|
|
@ -5,16 +5,19 @@ use std::{
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct HexSlice<'a>(&'a [u8]);
|
|
|
|
pub struct HexSlice<'a>(&'a [u8]);
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> HexSlice<'a>
|
|
|
|
impl<'a> HexSlice<'a>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Create a new `HexSlice` wrapper
|
|
|
|
#[inline] pub fn new<T>(from: &'a T) -> Self
|
|
|
|
#[inline] pub fn new<T>(from: &'a T) -> Self
|
|
|
|
where T: AsRef<[u8]> + ?Sized + 'a
|
|
|
|
where T: AsRef<[u8]> + ?Sized + 'a
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Self(from.as_ref())
|
|
|
|
Self(from.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a hex string output
|
|
|
|
pub fn to_hex_string(&self) -> String
|
|
|
|
pub fn to_hex_string(&self) -> String
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
@ -60,3 +63,18 @@ impl AsRef<[u8]> for HexSlice<'_>
|
|
|
|
self.0
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|