#array #hex #slice #array-vec #vec

no-std array-bytes

A collection of array/bytes/hex utilities

29 stable releases (7 major)

new 7.1.0 Dec 7, 2024
6.2.3 May 16, 2024
6.2.2 Dec 6, 2023
6.2.0 Nov 8, 2023
0.1.0 Nov 23, 2020

#73 in Encoding

Download history 63205/week @ 2024-08-20 73309/week @ 2024-08-27 77927/week @ 2024-09-03 66984/week @ 2024-09-10 62812/week @ 2024-09-17 69430/week @ 2024-09-24 73933/week @ 2024-10-01 67897/week @ 2024-10-08 73538/week @ 2024-10-15 73709/week @ 2024-10-22 69229/week @ 2024-10-29 80096/week @ 2024-11-05 67579/week @ 2024-11-12 65568/week @ 2024-11-19 67036/week @ 2024-11-26 73560/week @ 2024-12-03

287,412 downloads per month
Used in 837 crates (72 directly)

Apache-2.0/GPL-3.0

67KB
2K SLoC

array-bytes

A Collection of Array/Bytes/Hex Utilities.

License GPLv3 License MIT Checks Docs GitHub tag (latest by date) GitHub code lines GitHub last commit

Abilities

TryFromHex trait

  • Convert hex to num
    • type AsRef<[u8]> -> isize
    • type AsRef<[u8]> -> i8
    • type AsRef<[u8]> -> i16
    • type AsRef<[u8]> -> i32
    • type AsRef<[u8]> -> i64
    • type AsRef<[u8]> -> i128
    • type AsRef<[u8]> -> usize
    • type AsRef<[u8]> -> u8
    • type AsRef<[u8]> -> u16
    • type AsRef<[u8]> -> u32
    • type AsRef<[u8]> -> u64
    • type AsRef<[u8]> -> u128
  • Convert hex to array
    • type AsRef<[u8]> -> [u8; N], N = { [1, 64], 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 }
    • type AsRef<[u8]> -> Vec<u8>

Hex trait

  • Convert num to hex
    • type isize -> String
    • type i8 -> String
    • type i16 -> String
    • type i32 -> String
    • type i64 -> String
    • type i128 -> String
    • type usize -> String
    • type u8 -> String
    • type u16 -> String
    • type u32 -> String
    • type u64 -> String
    • type u128 -> String
  • Convert array to hex
    • type Vec<u8> -> String
    • type [u8; N] -> String, N = { [1, 64], 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 }
    • type &[u8] -> String

slice prefixed functions

  • Build fixed length Array from Slice
    • type &[T] -> [T; N]
    • type &[T] -> &[T; N]
  • Transform Slice to G
    • type &[T] -> G
    • e.g. &[0_u8, ...] -> [u8; 20] -> H160

prefix and suffix functions

  • Prefixes/suffixes the given element to the given slice to make it a fixed-size array of length N.

bytes prefixed functions

  • Convert bytes to hex
    • type AsRef<[u8]> -> String

hex prefixed functions

  • Convert HexBytes to hex
    • type &[u8] -> &str
    • e.g. b"0x..." -> "0x..."
  • Transform hex from Array
    • type &str -> [u8; N]
  • Convert hex to bytes
    • type AsRef<[u8]> -> Vec<u8>
  • Convert hex to Slice
    • type AsRef<[u8]> -> &[u8]
  • Transform hex to T
    • type AsRef<[u8]> -> T
    • e.g. "0x..." -> [u8; 20] -> H160

vec prefixed functions

  • Build fixed length Array from Vec
    • type Vec<T> -> [T; N]
  • Transform Vec to G
    • type Vec<T> -> G
    • e.g. vec![0_u8, ...] -> [u8; 20] -> H160

Serde support (require feature serde)

  • #[serde(deserialize_with = "array_bytes::hex_deserialize_n_into")]
    • type S -> T
    • e.g. "0x..." -> H160
  • #[serde(deserialize_with = "array_bytes::de_try_from_hex")]
    • type S -> impl TryFromHex
    • e.g. "0xA" -> 10_u32
  • #[serde(serialize_with = "array_bytes::ser_hex/array_bytes::ser_hex_without_prefix")]
    • type S -> impl Hex
    • e.g. "0x00" -> vec![0_u8]

Benchmark results

Tuesday, January 9th, 2024
array_bytes::bytes2hex  time:   [26.426 µs 26.473 µs 26.518 µs]
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) low mild
  1 (1.00%) high mild
  1 (1.00%) high severe

const_hex::encode       time:   [994.78 ns 1.0084 µs 1.0232 µs]

faster_hex::hex_string  time:   [11.728 µs 11.769 µs 11.815 µs]

faster_hex::hex_encode_fallback
                        time:   [11.704 µs 11.737 µs 11.773 µs]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

hex::encode             time:   [86.105 µs 86.250 µs 86.433 µs]
Found 14 outliers among 100 measurements (14.00%)
  1 (1.00%) low severe
  4 (4.00%) low mild
  6 (6.00%) high mild
  3 (3.00%) high severe

rustc_hex::to_hex       time:   [44.486 µs 45.538 µs 46.723 µs]
Found 16 outliers among 100 measurements (16.00%)
  1 (1.00%) low mild
  2 (2.00%) high mild
  13 (13.00%) high severe

array_bytes::hex2bytes  time:   [43.576 µs 44.529 µs 45.404 µs]
                        Performance has regressed.
Found 11 outliers among 100 measurements (11.00%)
  7 (7.00%) low mild
  4 (4.00%) high mild

array_bytes::hex2bytes_unchecked
                        time:   [64.190 µs 65.311 µs 66.359 µs]

array_bytes::hex2slice  time:   [45.484 µs 46.988 µs 48.736 µs]
                        Performance has regressed.
Found 12 outliers among 100 measurements (12.00%)
  2 (2.00%) high mild
  10 (10.00%) high severe

array_bytes::hex2slice_unchecked
                        time:   [62.339 µs 63.317 µs 64.279 µs]
                        Performance has regressed.

const_hex::decode       time:   [13.601 µs 13.629 µs 13.665 µs]
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) low mild
  1 (1.00%) high severe

faster_hex::hex_decode  time:   [28.015 µs 28.061 µs 28.110 µs]
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

faster_hex::hex_decode_unchecked
                        time:   [11.782 µs 11.797 µs 11.812 µs]
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) low severe
  1 (1.00%) low mild

faster_hex::hex_decode_fallback
                        time:   [11.748 µs 11.767 µs 11.785 µs]
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) low severe
  4 (4.00%) low mild
  1 (1.00%) high mild

hex::decode             time:   [93.055 µs 94.781 µs 96.583 µs]

hex::decode_to_slice    time:   [31.949 µs 33.509 µs 35.285 µs]
Found 13 outliers among 100 measurements (13.00%)
  5 (5.00%) high mild
  8 (8.00%) high severe

rustc_hex::from_hex     time:   [105.99 µs 108.05 µs 110.11 µs]

License

Licensed under either of Apache-2.0 or GPL-3.0 at your option.

Dependencies

~160KB