Crates.io | byte-array-struct |
lib.rs | byte-array-struct |
version | 0.2.0 |
source | src |
created_at | 2020-02-01 22:41:48.51746 |
updated_at | 2020-09-02 02:14:44.20233 |
description | Macro to create a byte-array backed struct |
homepage | |
repository | https://github.com/emeraldpay/byte-array-struct |
max_upload_size | |
id | 204036 |
size | 27,285 |
Provides a macro to allows creation of a simple byte-array backed structs. Such struct has a predefined size and allocated on stack.
[dependencies]
byte-array-struct = "0.2"
// create struct named Address backed by [u8; 24]
// basically a shortcut to `pub struct Address([u8; 24]);`
byte_array_struct!(
pub struct Address(24);
);
impl Address {
// any additional functionality for Address type
}
// passed as a value on stack
fn send(to: Address) {
// ...
}
fn main() {
//accepts hex, which can also be prefixed with 0x
let foo = Address::from_str("0123456789abcdef0123456789abcdef0123456789abcdef").unwrap();
send(foo);
}
Macro provides implementation for following traits:
.deref()
.from_str(s)
, which accepts a hex string with the length of target array; may be optionally prefixed with 0x
.to_string()
.from([u8; ...])
and .from(&[u8; ...])
, where ...
is the defined size.try_from(Vec<u8>)
and .try_from(&[u8])
.into(Vec<u8>)
and .into([u8; ...])
.serialize
and .deserialize
for Serde, with with-serde
feature enabled (not enabled by default)with-serde
to implement serialization/deserialization with Serde. Uses Hex encoded strings.