#array #hex #slice #array-vec #vec #no-std

无std array-bytes

一组数组/字节/十六进制实用工具

27 个稳定版本 (6 个主要版本)

6.2.3 2024年5月16日
6.2.2 2023年12月6日
6.2.0 2023年11月8日
6.1.0 2023年4月18日
0.1.0 2020年11月23日

#37 in 编码

Download history 65037/week @ 2024-05-02 54735/week @ 2024-05-09 55142/week @ 2024-05-16 67310/week @ 2024-05-23 64734/week @ 2024-05-30 51429/week @ 2024-06-06 55817/week @ 2024-06-13 57445/week @ 2024-06-20 54652/week @ 2024-06-27 51510/week @ 2024-07-04 62595/week @ 2024-07-11 69802/week @ 2024-07-18 67988/week @ 2024-07-25 64866/week @ 2024-08-01 77323/week @ 2024-08-08 56431/week @ 2024-08-15

每月279,556次下载
795 个crate中使用 (66 个直接使用)

Apache-2.0/GPL-3.0

49KB
1K SLoC

array-bytes

一组数组/字节/十六进制实用工具。

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

功能

TryFromHex trait

  • 将十六进制转换为数字
    • 类型 AsRef<[u8]> -> isize
    • 类型 AsRef<[u8]> -> i8
    • 类型 AsRef<[u8]> -> i16
    • 类型 AsRef<[u8]> -> i32
    • 类型 AsRef<[u8]> -> i64
    • 类型 AsRef<[u8]> -> i128
    • 类型 AsRef<[u8]> -> usize
    • 类型 AsRef<[u8]> -> u8
    • 类型 AsRef<[u8]> -> u16
    • 类型 AsRef<[u8]> -> u32
    • 类型 AsRef<[u8]> -> u64
    • 类型 AsRef<[u8]> -> u128
  • 将十六进制转换为数组
    • 类型 AsRef<[u8]> -> [u8; N]N = { [1, 64], 128, 256, 512 }
    • 类型 AsRef<[u8]> -> Vec<u8>

Hex 特性

  • 将数字转换为十六进制
    • 类型 isize -> String
    • 类型 i8 -> String
    • 类型 i16 -> String
    • 类型 i32 -> String
    • 类型 i64 -> String
    • 类型 i128 -> String
    • 类型 usize -> String
    • 类型 u8 -> String
    • 类型 u16 -> String
    • 类型 u32 -> String
    • 类型 u64 -> String
    • 类型 u128 -> String
  • 将数组转换为十六进制
    • 类型 Vec<u8> -> String
    • 类型 [u8; N] -> String,其中 N = { [1, 64], 128, 256, 512 }
    • 类型 &[u8] -> String

slice 为前缀的函数

  • Slice 构建固定长度的 Array
    • 类型 &[T] -> [T; N]
    • 类型 &[T] -> &[T; N]
  • Slice 转换为 G
    • 类型 &[T] -> G
    • 例如 &[0_u8, ...] -> [u8; 20] -> H160

prefixsuffix 函数

  • 将给定元素添加到给定切片前/后,以使其成为长度为 N 的固定大小数组。

bytes 前缀函数

  • 将字节转换为十六进制
    • 类型 AsRef<[u8]> -> String

hex 前缀函数

  • HexBytes 转换为十六进制
    • 类型 &&u8[u8] -> &str
    • 例如 b"0x..." -> "0x..."
  • 将十六进制从 Array 转换
    • 类型 &str -> [u8; N]
  • 将十六进制转换为字节
    • 类型 AsRef<[u8]> -> Vec<u8>
  • 将十六进制转换为 Slice
    • 类型 AsRef<[u8]> -> &[u8]
  • 将十六进制转换为 T
    • 类型 AsRef<[u8]> -> T
    • 例如 "0x..." -> [u8; 20] -> H160

vec 前缀函数

  • Vec 构建固定长度的 Array
    • 类型 Vec<T> -> [T; N]
  • Vec 转换为 G
    • 类型 Vec<T> -> G
    • 例如 vec![0_u8, ...] -> [u8; 20] -> H160

Serde 支持(需要 serde 功能)

  • #[serde(deserialize_with= "array_bytes::hex_deserialize_n_into")]
    • 类型 S -> T
    • 例如 "0x..." -> H160
  • #[serde(deserialize_with= "array_bytes::de_hex2num")]
    • 类型 S -> Num
    • 例如 "0xA" -> 10_u32
  • #[serde(deserialize_with= "array_bytes::de_hex2bytes")]
    • 类型 S -> Vec<u8>
    • 例如 "0x00" -> vec![0_u8]

基准测试结果

星期二,2024年1月9日
array_bytes::bytes2hex  time:   [30.487 µs 30.513 µs 30.543 µs]
                        change: [-12.758% -7.1673% -2.3095%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

const_hex::encode       time:   [2.1197 µs 2.2245 µs 2.3208 µs]
                        change: [+25.796% +31.010% +36.449%] (p = 0.00 < 0.05)
                        Performance has regressed.

faster_hex::hex_string  time:   [13.666 µs 13.711 µs 13.754 µs]
                        change: [-0.3508% +0.0892% +0.5043%] (p = 0.68 > 0.05)
                        No change in performance detected.

faster_hex::hex_encode_fallback
                        time:   [13.476 µs 13.519 µs 13.564 µs]
                        change: [-0.1799% +0.2323% +0.6560%] (p = 0.27 > 0.05)
                        No change in performance detected.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

hex::encode             time:   [133.99 µs 135.65 µs 137.13 µs]
                        change: [-1.6763% +0.2181% +2.1203%] (p = 0.82 > 0.05)
                        No change in performance detected.
Found 15 outliers among 100 measurements (15.00%)
  13 (13.00%) low severe
  2 (2.00%) low mild

rustc_hex::to_hex       time:   [118.83 µs 124.46 µs 129.51 µs]
                        change: [-3.5525% +2.8439% +10.307%] (p = 0.42 > 0.05)
                        No change in performance detected.

array_bytes::hex2bytes  time:   [46.892 µs 47.510 µs 48.195 µs]
                        change: [-8.2282% -6.5411% -4.6367%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe

array_bytes::hex2bytes_unchecked
                        time:   [73.450 µs 73.842 µs 74.251 µs]
                        change: [+0.5740% +1.3693% +2.1806%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 17 outliers among 100 measurements (17.00%)
  11 (11.00%) low mild
  5 (5.00%) high mild
  1 (1.00%) high severe

array_bytes::hex2slice  time:   [57.825 µs 57.915 µs 58.007 µs]
                        change: [-0.9051% -0.6249% -0.3523%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 6 outliers among 100 measurements (6.00%)
  6 (6.00%) high mild

array_bytes::hex2slice_unchecked
                        time:   [73.574 µs 73.917 µs 74.281 µs]
                        change: [-4.9137% -4.1840% -3.4519%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  8 (8.00%) high mild
  5 (5.00%) high severe

const_hex::decode       time:   [15.849 µs 15.887 µs 15.924 µs]
                        change: [-3.1788% -2.1280% -1.2019%] (p = 0.00 < 0.05)
                        Performance has improved.

faster_hex::hex_decode  time:   [31.735 µs 31.764 µs 31.800 µs]
                        change: [-0.7403% -0.5216% -0.2674%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 7 outliers among 100 measurements (7.00%)
  5 (5.00%) high mild
  2 (2.00%) high severe

faster_hex::hex_decode_unchecked
                        time:   [13.059 µs 13.098 µs 13.145 µs]
                        change: [-0.7125% -0.3968% -0.1100%] (p = 0.01 < 0.05)
                        Change within noise threshold.
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) high mild
  4 (4.00%) high severe

faster_hex::hex_decode_fallback
                        time:   [13.074 µs 13.090 µs 13.108 µs]
                        change: [-1.9404% -1.5652% -1.1912%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe

hex::decode             time:   [131.19 µs 132.57 µs 134.26 µs]
                        change: [+1.9126% +3.2801% +4.8702%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 15 outliers among 100 measurements (15.00%)
  15 (15.00%) high severe

hex::decode_to_slice    time:   [57.577 µs 58.018 µs 58.447 µs]
                        change: [+0.0185% +1.1253% +2.2104%] (p = 0.04 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

rustc_hex::from_hex     time:   [130.19 µs 130.86 µs 131.48 µs]
                        change: [-1.8542% -1.4374% -0.9862%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

许可

根据您的选择,受Apache-2.0GPL-3.0许可。

依赖项

~165KB