Expand description
A collection of Array/Bytes/Hex utilities with full No-STD compatibility.
Completely optimized for blockchain development. Especially the Polkadot-SDK.
§Usage
Here are a few quick examples of the most commonly used operations: hexifying and dehexifying.
However, this crate also offers many other utilities for Array/Bytes/Hex, each with comprehensive documentation and examples. Check them out on docs.rs!
use array_bytes::{Dehexify, Error, Hexify};
use smallvec::SmallVec;
// Hexify.
// Unsigned.
assert_eq!(52_u8.hexify(), "34");
assert_eq!(520_u16.hexify_upper(), "208");
assert_eq!(5_201_314_u32.hexify_prefixed(), "0x4f5da2");
assert_eq!(5_201_314_u64.hexify_prefixed_upper(), "0x4F5DA2");
assert_eq!(5_201_314_u128.hexify(), "4f5da2");
assert_eq!(5_201_314_usize.hexify_upper(), "4F5DA2");
// `[u8; N]`.
assert_eq!(*b"Love Jane Forever".hexify(), String::from("4c6f7665204a616e6520466f7265766572"));
// `&[u8; N]`.
assert_eq!(
b"Love Jane Forever".hexify_upper(),
String::from("4C6F7665204A616E6520466F7265766572")
);
// `&[u8]`.
assert_eq!(
b"Love Jane Forever".as_slice().hexify_prefixed(),
String::from("0x4c6f7665204a616e6520466f7265766572")
);
// `Vec<u8>`.
assert_eq!(
b"Love Jane Forever".to_vec().hexify_prefixed_upper(),
String::from("0x4C6F7665204A616E6520466F7265766572")
);
// `&Vec<u8>`.
assert_eq!(
(&b"Love Jane Forever".to_vec()).hexify(),
String::from("4c6f7665204a616e6520466f7265766572")
);
// Dehexify.
// 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 })
);
Re-exports§
pub use serde_bytes;
Enums§
Traits§
Functions§
- de_
bytes_ destringify - Deserialize string to bytes.
- de_
dehexify - Deserialize hex to
T
. - dehexify_
array_ then_ deserialize_ into - Deserialize hex to
T
whereT: From<[u8; N]>
. - dehexify_
array_ then_ into - Dehexify hex to a fixed length bytes vector then convert it to
T
whereT: From<[u8; N]>
. - dehexify_
slice_ mut - Dehexify hex into a mutable slice source.
- dehexify_
vec_ then_ deserialize_ into - Deserialize hex to
T
whereT: From<Vec<u8>>
. - dehexify_
vec_ then_ into - Dehexify hex to a bytes vector then convert it to
T
whereT: From<Vec<u8>
. - hexify_
hex_ bytes - Hexify the bytes which are already in hex.
- prefix_
with - Prefixes the given element to the given array/slice/vector to make it a fixed-size array of
length
N
. - ser_
bytes_ stringify - Serialize bytes to string.
- ser_
hexify - Serialize
T
to hex. - ser_
hexify_ prefixed - Serialize
T
to hex with0x
prefix. - ser_
hexify_ prefixed_ upper - Serialize
T
to hex with0x
prefix and uppercase. - ser_
hexify_ upper - Serialize
T
to hex with uppercase. - slice2array
- Convert
&[T]
to[T; N]
. - slice2array_
ref - Convert
&[T]
to&[T; N]
. - slice_
n_ into - Convert
&[T]
toV
whereV: From<[T; N]>
. - suffix_
with - Suffixes the given element to the given array/slice/vector to make it a fixed-size array of
length
N
. - vec2array
- Convert
Vec<T>
to[T; N]
. - vec_
n_ into - Convert
Vec<T>
toV
whereV: From<[T; N]>
.