Struct hexstring::HexString

source ·
#[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>

source

pub fn new<S: Into<Cow<'static, str>>>(s: S) -> Result<Self, Error>

Constructs a new HexString from a string.

Errors

This method fails if the given string is not a valid hexadecimal.

source

pub unsafe fn new_unchecked<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new HexString without checking the string.

Safety

The string should be a valid hexadecimal string.

source§

impl HexString<{ Case::Lower }>

source

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 }>

source

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<const C: Case> Clone for HexString<C>

source§

fn clone(&self) -> HexString<C>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const C: Case> Debug for HexString<C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const C: Case> Default for HexString<C>

source§

fn default() -> HexString<C>

Returns the “default value” for a type. Read more
source§

impl<'de, const C: Case> Deserialize<'de> for HexString<C>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<const C: Case> Display for HexString<C>

source§

fn fmt(&self, _derive_more_display_formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const C: Case> From<&[u8]> for HexString<C>

source§

fn from(bytes: &[u8]) -> Self

Converts to this type from the input type.
source§

impl<const C: Case, const N: usize> From<[u8; N]> for HexString<C>

source§

fn from(bytes: [u8; N]) -> Self

Converts to this type from the input type.
source§

impl<const C: Case> From<HexString<C>> for Vec<u8>

source§

fn from(s: HexString<C>) -> Self

Converts to this type from the input type.
source§

impl<const C: Case> From<Vec<u8, Global>> for HexString<C>

source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl<const C: Case> FromStr for HexString<C>

§

type Err = FromHexError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<const C: Case> Hash for HexString<C>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<const C: Case> Ord for HexString<C>

source§

fn cmp(&self, other: &HexString<C>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<const C: Case> PartialEq<HexString<C>> for HexString<C>

source§

fn eq(&self, other: &HexString<C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const C: Case> PartialOrd<HexString<C>> for HexString<C>

source§

fn partial_cmp(&self, other: &HexString<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<const C: Case> Serialize for HexString<C>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<const C: Case, const N: usize> TryFrom<HexString<C>> for [u8; N]

§

type Error = FromHexError

The type returned in the event of a conversion error.
source§

fn try_from(s: HexString<C>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<const C: Case> Eq for HexString<C>

source§

impl<const C: Case> StructuralEq for HexString<C>

source§

impl<const C: Case> StructuralPartialEq for HexString<C>

Auto Trait Implementations§

§

impl<const C: Case> RefUnwindSafe for HexString<C>

§

impl<const C: Case> Send for HexString<C>

§

impl<const C: Case> Sync for HexString<C>

§

impl<const C: Case> Unpin for HexString<C>

§

impl<const C: Case> UnwindSafe for HexString<C>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,