0 day streak
rust / expert
Snippet

Zero-Copy Parsing of Binary Headers via Lifetime Reference Mapping

Illustrates high-performance parsing of binary protocols without allocation. By linking the lifetime of the parsed struct fields directly to the input slice, memory allocation is completely avoided.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::convert::TryInto;
#[derive(Debug)]
pub struct BinaryPacket<'a> {
pub magic: &'a [u8; 4],
pub payload_len: u32,
pub payload: &'a [u8],
}
impl<'a> BinaryPacket<'a> {
pub fn parse(data: &'a [u8]) -> Result<Self, &'static str> {
if data.len() < 8 {
return Err("Data too short to contain header");
}
let magic: &'a [u8; 4] = data[0..4].try_into()
.map_err(|_| "Failed to parse magic bytes")?;
let len_bytes = data[4..8].try_into()
.map_err(|_| "Failed to parse length bytes")?;
let payload_len = u32::from_be_bytes(len_bytes);
let end_idx = 8 + payload_len as usize;
if data.len() < end_idx {
return Err("Data length does not match payload header length");
}
let payload = &data[8..end_idx];
Ok(Self { magic, payload_len, payload })
}
}
Breakdown
1
pub struct BinaryPacket<'a> {
Defines a struct containing lifetime-bound references to the source buffer.
2
let magic: &'a [u8; 4] = data[0..4].try_into()
Converts the sub-slice into a fixed-size array reference of exactly 4 bytes.
3
let payload_len = u32::from_be_bytes(len_bytes);
Parses big-endian bytes into a standard unsigned 32-bit integer at compile time.
4
let payload = &data[8..end_idx];
Borrows the payload section of the original slice without copying any data.