Crate array_bytes

Source
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§

Error

Traits§

Dehexify
Dehexify the given hex to Self.
Hexify
Hexify Self.

Functions§

de_bytes_destringify
Deserialize string to bytes.
de_dehexify
Deserialize hex to T.
dehexify_array_then_deserialize_into
Deserialize hex to T where T: From<[u8; N]>.
dehexify_array_then_into
Dehexify hex to a fixed length bytes vector then convert it to T where T: From<[u8; N]>.
dehexify_slice_mut
Dehexify hex into a mutable slice source.
dehexify_vec_then_deserialize_into
Deserialize hex to T where T: From<Vec<u8>>.
dehexify_vec_then_into
Dehexify hex to a bytes vector then convert it to T where T: 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 with 0x prefix.
ser_hexify_prefixed_upper
Serialize T to hex with 0x 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] to V where V: 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> to V where V: From<[T; N]>.

Type Aliases§

Result