From 2ddfec4bfc86f51ad92c38f3cbcb8c2f8cad31fd Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 13 Aug 2020 21:42:33 +0100 Subject: [PATCH] test hex string --- src/ext.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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"); + } +}