#[byte_string]
Expand description
Proc macro that can generate multiple helper functions and traits for types that wrap an array, Vec, or BoundedVec of bytes. What code is generated by the macro is controlled by passing the following arguments:
debug
: implements Debug that uses hex to encode the byteshex_serialize
: implementsserde::Serialize
that encode the bytes to a hex string as intermediate format. This implementation is useful for saving bytes in Json and similar formats.hex_deserialize
: implementsserde::Deserialize
that decodes the type from a string containig hex-encoded bytes. This implementation is useful for decoding hex data from Json and similar formats.from_num
: implementsFrom<u64>
from_bytes
: implements either [From<&[u8]>
] or [TryFrom<&[u8]
] depending on whether the the type can be infallibly (Vec) or fallibly (array, BoundedVec) cast from a byte slice.decode_hex
: adds functionsdecode_hex
anddecode_hex_unsafe
that decode the type from a &str and a FromStr implementation equivalent to callingdecode_hex
.to_hex_string
: adds a functionto_hex_string
that returns the inner bytes as a hex string.as_ref
: generates [AsRef<[u8]>
] implementation
Note: As the code generated by this macro is meant to be no_std
-compatible, some of the
options require alloc
crate to be available in the scope where the code is generated.
ยงExample
extern crate alloc;
use byte_string_derive::byte_string;
use sp_core::{ ConstU32, bounded_vec::BoundedVec };
#[byte_string(debug, hex_serialize, hex_deserialize, from_num, from_bytes, decode_hex, to_hex_string, as_ref)]
pub struct MyArrayBytes([u8; 32]);
#[byte_string(debug, hex_serialize, hex_deserialize, from_num, from_bytes, decode_hex, to_hex_string, as_ref)]
pub struct MyVecBytes(Vec<u8>);
#[byte_string(from_bytes)]
pub struct MyBoundedVecBytes(BoundedVec<u8, ConstU32<32>>);