#byte #slice #read #zeroed #integer #bench #u64

from_bytes_or_zeroed

从任意字节数组切片中读取整数

1个不稳定版本

0.1.0 2023年5月14日

#2287算法

Download history 2008/week @ 2024-03-25 1959/week @ 2024-04-01 2040/week @ 2024-04-08 2116/week @ 2024-04-15 1665/week @ 2024-04-22 1731/week @ 2024-04-29 1488/week @ 2024-05-06 1230/week @ 2024-05-13 1563/week @ 2024-05-20 1324/week @ 2024-05-27 1488/week @ 2024-06-03 1052/week @ 2024-06-10 1074/week @ 2024-06-17 1223/week @ 2024-06-24 526/week @ 2024-07-01 1716/week @ 2024-07-08

4,588 每月下载量
用于 6 个crate(通过 bitcode_lightyear_patch

MIT/Apache

15KB
250

from_bytes_or_zeroed

一个从任意字节数组切片中读取整数的crate


lib.rs:

from_bytes_or_zeroed 是一个crate,用于从任意长度的字节数组切片中创建整数。

使用方法

use from_bytes_or_zeroed::FromBytesOrZeroed;

// If there are too few bytes the remaining bytes are zeroed.
let a = [1, 2];
let b = [1, 2, 0, 0, 0, 0, 0, 0];
assert_eq!(u64::from_le_bytes_or_zeroed(&a), u64::from_le_bytes(b));

// If there are too many bytes the extra bytes are ignored.
let a = [1, 2, 3, 4, 5, 6];
let b = [1, 2, 3, 4];
assert_eq!(u32::from_le_bytes_or_zeroed(&a), u32::from_le_bytes(b));

动机

这个crate的创建是因为在安全的Rust中实现这个操作[prim@u64]比使用不安全的操作慢得多。

test benches::bench_u64_safe    ... bench:       1,509 ns/iter (+/- 29)
test benches::bench_u64_unsafe  ... bench:         644 ns/iter (+/- 8)

无运行时依赖