parse_frame

Function parse_frame 

Source
pub fn parse_frame(buf: &[u8]) -> Result<(usize, usize)>
Expand description

Reads a header out of an arbitrary buffer, checks the crc, and if the crc matches the corresponding bytes, returns the start and end offsets in the buffer for the inner bytes. The end offset of the buffer is also the end offset for this entire frame, so you may advance any cursors to this point for reading the next frame in a sequence of bytes etc…

§Examples

use crc_frame::{write_frame_into, parse_frame};

let data = b"12345";

let mut buf = vec![];

write_frame_into(&mut buf, data).unwrap();
write_frame_into(&mut buf, data).unwrap();

let (begin_1, end_1) = parse_frame(&buf).unwrap();

assert_eq!(&buf[begin_1..end_1], data);

let (begin_2, end_2) = parse_frame(&buf[end_1..]).unwrap();

assert_eq!(&buf[begin_2..end_2], data);