array_bytes

Trait Dehexify

Source
pub trait Dehexify
where Self: Sized,
{ // Required method fn dehexify<H>(hex: H) -> Result<Self> where H: AsRef<[u8]>; }
Expand description

Dehexify the given hex to Self.

§Examples

use array_bytes::{Dehexify, Error};
use smallvec::SmallVec;

// Unsigned.
assert_eq!(u8::dehexify("34"), Ok(52));
assert_eq!(u16::dehexify("208"), Ok(520));
assert_eq!(u32::dehexify("0x4f5da2"), Ok(5_201_314));
assert_eq!(u64::dehexify("0x4F5DA2"), Ok(5_201_314));
assert_eq!(u128::dehexify("4f5da2"), Ok(5_201_314));
assert_eq!(usize::dehexify("4F5DA2"), Ok(5_201_314));
// Array.
assert_eq!(
	<[u8; 17]>::dehexify("0x4c6f7665204a616e6520466f7265766572"),
	Ok(*b"Love Jane Forever")
);
// SmallVec.
assert_eq!(
	SmallVec::dehexify("0x4c6f7665204a616e6520466f7265766572").unwrap().into_vec(),
	b"Love Jane Forever".to_vec()
);
assert_eq!(SmallVec::dehexify("我爱你"), Err(Error::InvalidLength));
assert_eq!(SmallVec::dehexify("0x我爱你"), Err(Error::InvalidLength));
// Vec.
assert_eq!(
	<Vec<u8>>::dehexify("0x4c6f7665204a616e6520466f7265766572"),
	Ok(b"Love Jane Forever".to_vec())
);
assert_eq!(
	<Vec<u8>>::dehexify("我爱你 "),
	Err(Error::InvalidCharacter { character: 'æ', index: 0 })
);
assert_eq!(
	<Vec<u8>>::dehexify(" 我爱你"),
	Err(Error::InvalidCharacter { character: ' ', index: 0 })
);

Required Methods§

Source

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Dehexify Self from hex.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Dehexify for u8

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for u16

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for u32

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for u64

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for u128

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for usize

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for Vec<u8>

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl Dehexify for SmallVec<[u8; 64]>

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Source§

impl<const N: usize> Dehexify for [u8; N]

Source§

fn dehexify<H>(hex: H) -> Result<Self>
where H: AsRef<[u8]>,

Implementors§