Expand description
from_bytes_or_zeroed
is a crate for creating integers from a slice of bytes of any length.
§Usage
ⓘ
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));
§Motivation
This crate was created because implementing this operation for u64
in safe Rust was
significantly slower than using unsafe.
test benches::bench_u64_safe ... bench: 1,509 ns/iter (+/- 29)
test benches::bench_u64_unsafe ... bench: 644 ns/iter (+/- 8)