Skip to main content

SlipDecoder

Struct SlipDecoder 

Source
pub struct SlipDecoder<const N: usize> { /* private fields */ }
Expand description

A streaming SLIP decoder that reassembles whole frames from a serial byte stream.

A serial read returns whatever bytes have arrived, which is rarely a whole packet, so a real receive loop feeds bytes in as they come and acts on each frame as it completes. SlipDecoder buffers up to N payload bytes; push returns the finished payload when an END closes the frame, and None while one is still being assembled.

§Examples

use pamoja_serial::slip::{SlipDecoder, END};

let mut decoder: SlipDecoder<32> = SlipDecoder::new();
let mut frames = 0;
// Two packets arrive back to back in one read.
for &byte in &[b'o', b'k', END, b'g', b'o', END] {
    if let Some(frame) = decoder.push(byte)? {
        frames += 1;
        assert!(frame == b"ok" || frame == b"go");
    }
}
assert_eq!(frames, 2);

Implementations§

Source§

impl<const N: usize> SlipDecoder<N>

Source

pub const fn new() -> Self

Creates an empty decoder with room for an N-byte payload.

§Returns

A decoder ready to receive the first byte.

Source

pub fn reset(&mut self)

Discards any partly assembled frame, returning the decoder to its initial state.

Source

pub fn push(&mut self, byte: u8) -> Result<Option<&[u8]>, SerialError>

Feeds one byte from the stream into the decoder.

§Arguments
  • byte - the next byte received on the serial line.
§Returns

Some(payload) when this byte completed a frame, or None while a frame is still being assembled. An empty frame (a stray END with nothing buffered) is treated as a flush and yields None.

§Errors

Returns SerialError::InvalidEscape if an ESC is followed by an unexpected byte, SerialError::TruncatedFrame if an END arrives mid-escape, and SerialError::BufferTooSmall if the payload exceeds N bytes. After any error the partial frame is discarded and the decoder resumes at the next byte.

Trait Implementations§

Source§

impl<const N: usize> Debug for SlipDecoder<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for SlipDecoder<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for SlipDecoder<N>

§

impl<const N: usize> RefUnwindSafe for SlipDecoder<N>

§

impl<const N: usize> Send for SlipDecoder<N>

§

impl<const N: usize> Sync for SlipDecoder<N>

§

impl<const N: usize> Unpin for SlipDecoder<N>

§

impl<const N: usize> UnsafeUnpin for SlipDecoder<N>

§

impl<const N: usize> UnwindSafe for SlipDecoder<N>

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.