2 unstable releases

0.2.0 Sep 2, 2020
0.1.0 Feb 1, 2020

#2724 in Rust patterns

Download history 34/week @ 2023-11-05 19/week @ 2023-11-12 33/week @ 2023-11-19 56/week @ 2023-11-26 18/week @ 2023-12-03 42/week @ 2023-12-10 91/week @ 2023-12-17 83/week @ 2023-12-24 20/week @ 2023-12-31 10/week @ 2024-01-07 8/week @ 2024-01-14 8/week @ 2024-01-21 12/week @ 2024-01-28 22/week @ 2024-02-04 51/week @ 2024-02-11 68/week @ 2024-02-18

153 downloads per month
Used in 2 crates (via emerald-vault)

Apache-2.0

16KB
319 lines

Provides a macro to allows creation of a simple byte-array backed structs. Such struct has a predefined size and allocated on stack.

Usage

Dependency

[dependencies]
byte-array-struct = "0.2"

Example

// 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);
}

Provides

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)

Features

  • with-serde to implement serialization/deserialization with Serde. Uses Hex encoded strings.

Dependencies

~19–295KB