Skip to main content

FrameReader

Struct FrameReader 

Source
pub struct FrameReader<'a> { /* private fields */ }
Expand description

Reads length-prefixed frames from a byte stream.

Yields individual message payloads that can be decoded with BitReader.

§Example

use vexil_runtime::{BitReader, FrameReader};

let stream: &[u8] = &[
    4, 0x2A, 0x00, 0x00, 0x00,  // frame 1: len=4, u32(42) LE
    6, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, // frame 2: len=6, string "hello"
];
let mut fr = FrameReader::new(stream);

let frame1 = fr.read_frame().unwrap().unwrap();
let mut r = BitReader::new(frame1);
assert_eq!(r.read_u32().unwrap(), 42);

let frame2 = fr.read_frame().unwrap().unwrap();
let mut r = BitReader::new(frame2);
assert_eq!(r.read_string().unwrap(), "hello");

assert!(fr.read_frame().is_none()); // EOF

Implementations§

Source§

impl<'a> FrameReader<'a>

Source

pub fn new(data: &'a [u8]) -> Self

Create a new frame reader over the given byte stream.

Source

pub fn read_frame(&mut self) -> Option<Result<&'a [u8], DecodeError>>

Read the next frame. Returns the payload bytes, or None at EOF.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no more frames.

Source

pub fn remaining(&self) -> &'a [u8]

Returns remaining bytes (partial frame or trailing data).

Auto Trait Implementations§

§

impl<'a> Freeze for FrameReader<'a>

§

impl<'a> RefUnwindSafe for FrameReader<'a>

§

impl<'a> Send for FrameReader<'a>

§

impl<'a> Sync for FrameReader<'a>

§

impl<'a> Unpin for FrameReader<'a>

§

impl<'a> UnsafeUnpin for FrameReader<'a>

§

impl<'a> UnwindSafe for FrameReader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.