Skip to main content

try_bytes

Function try_bytes 

Source
pub fn try_bytes<const N: usize>(data: &[u8], off: usize) -> Option<[u8; N]>
Expand description

Copy the N-byte window at off into an array; None if that window is not fully in range (too short, offset past EOF, or off + N overflowing usize). Never panics.

The array flavour of the readers above, for the fixed-width windows that are not integers — GUIDs, signatures, digests, fixed-size name fields:

use safe_read::try_bytes;
let record = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02];
assert_eq!(try_bytes::<4>(&record, 0), Some([0xde, 0xad, 0xbe, 0xef]));
assert_eq!(try_bytes::<4>(&record, 3), None); // runs past the end