test hex string

work
Avril 4 years ago
parent 6b7af42bd9
commit 2ddfec4bfc
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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<T>(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");
}
}

Loading…
Cancel
Save