#[repr(transparent)]pub struct HexString<const C: Case>(_);
Expand description
Provides a structured representation of a hexadecimal string.
It is guaranteed to be a valid hexadecimal string, whether initialized from a string or from bytes. A valid [’HexString`] should contain only alphanumerical characters such as :
- ff04ad992c
- FF04AD99C
And must not mix upper and lower alphabetic characters.
Examples
The idiomatic way to construct a HexString
is to call HexString::new
method with a
string.
use hexstring::{HexString, Case};
let hex = HexString::<{ Case::Upper }>::new("ABCDEF").unwrap();
As the example shown, creating a hexadecimal string is a bit convoluted due to the usage of
const generic parameter.
Two convenient type aliases must be used instead of the raw HexString
type :
use hexstring::{UpperHexString, LowerHexString};
let lowercase_hex = LowerHexString::new("abcdef").unwrap();
let uppercase_hex = UpperHexString::new("ABCDEF").unwrap();
HexString
has support for conversion from and into array of bytes.
use hexstring::LowerHexString;
let expected_bytes = [41, 24, 42];
let hex = LowerHexString::from(expected_bytes);
let bytes = Vec::from(hex);
assert_eq!(expected_bytes, &bytes[..]);
Implementations§
source§impl<const C: Case> HexString<C>
impl<const C: Case> HexString<C>
source§impl HexString<{ Case::Lower }>
impl HexString<{ Case::Lower }>
sourcepub fn to_uppercase(self) -> UpperHexString
pub fn to_uppercase(self) -> UpperHexString
Constructs an UpperHexString
from a LowerHexString
.
This method performs a copy if the internal string is a string literal.
source§impl HexString<{ Case::Upper }>
impl HexString<{ Case::Upper }>
sourcepub fn to_lowercase(self) -> LowerHexString
pub fn to_lowercase(self) -> LowerHexString
Constructs a LowerHexString
from an UpperHexString
.
This method performs a copy if the internal string is a string literal.
Trait Implementations§
source§impl<'de, const C: Case> Deserialize<'de> for HexString<C>
impl<'de, const C: Case> Deserialize<'de> for HexString<C>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<const C: Case> Ord for HexString<C>
impl<const C: Case> Ord for HexString<C>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<const C: Case> PartialEq<HexString<C>> for HexString<C>
impl<const C: Case> PartialEq<HexString<C>> for HexString<C>
source§impl<const C: Case> PartialOrd<HexString<C>> for HexString<C>
impl<const C: Case> PartialOrd<HexString<C>> for HexString<C>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more