Skip to main content

FrameRead

Trait FrameRead 

Source
pub trait FrameRead<'a>: Sized {
    type Error: Debug;

    // Required method
    fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>;
}
Expand description

Decodes Self from a zero-copy buffer cursor.

The cursor buf is a &mut &'a [u8] - a mutable reference to a shared slice. Each call to decode advances the cursor by consuming bytes from the front. The lifetime 'a ties any borrowed data in Self back to the original buffer, ensuring zero allocation for slice fields.

§Implementing for custom types

Most types should use #[derive(FrameRead)] from ace-macros. Manual implementation is required when field lengths depend on context from a parent struct - use [FrameReadWithContext] in those cases.

§Blanket impls provided

  • u8, u16, u32 - big-endian
  • [u8; N] - fixed-size arrays
  • &'a [u8] - consumes all remaining bytes
  • Option<T: FrameRead> - None if buffer empty, Some(T::decode(...)) otherwise
  • FrameIter<'a, T: FrameRead> - lazy iterator consuming all remaining bytes

Required Associated Types§

Required Methods§

Source

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> FrameRead<'a> for &'a [u8]

Consumes all remaining bytes - only valid as a trailing field.

Source§

type Error = DiagError

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Source§

impl<'a> FrameRead<'a> for u8

Source§

type Error = DiagError

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Source§

impl<'a> FrameRead<'a> for u16

Source§

type Error = DiagError

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Source§

impl<'a> FrameRead<'a> for u32

Source§

type Error = DiagError

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Source§

impl<'a, T> FrameRead<'a> for Option<T>
where T: FrameRead<'a>,

None if buffer is empty, Some(T::decode(...)) otherwise.

Source§

type Error = <T as FrameRead<'a>>::Error

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Source§

impl<'a, const N: usize> FrameRead<'a> for [u8; N]

Source§

type Error = DiagError

Source§

fn decode(buf: &mut &'a [u8]) -> Result<Self, Self::Error>

Implementors§

Source§

impl<'a, T> FrameRead<'a> for FrameIter<'a, T>
where T: FrameRead<'a>,

FrameIter implements FrameRead for use as a trailing field in derived structs. Consumes all remaining bytes from the cursor.

Source§

type Error = <T as FrameRead<'a>>::Error