Skip to main content

CobsDecoder

Struct CobsDecoder 

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

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

Like SlipDecoder, this is what a real serial receive loop uses: it buffers up to N payload bytes and push returns the finished payload when the zero DELIMITER closes a frame, or None while one is still being assembled.

§Examples

use pamoja_serial::cobs::{CobsDecoder, DELIMITER};

let mut decoder: CobsDecoder<32> = CobsDecoder::new();
// The encoding of the payload 11 22 00 33, followed by the delimiter.
let stream = [0x03, 0x11, 0x22, 0x02, 0x33, DELIMITER];
let mut got = None;
for &byte in &stream {
    if let Some(frame) = decoder.push(byte)? {
        got = Some(frame.to_vec());
    }
}
assert_eq!(got.as_deref(), Some(&[0x11, 0x22, 0x00, 0x33][..]));

Implementations§

Source§

impl<const N: usize> CobsDecoder<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’s DELIMITER completed a frame, or None while a frame is still being assembled.

§Errors

Returns SerialError::TruncatedFrame if the delimiter arrives before a run’s data is complete, 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 CobsDecoder<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 CobsDecoder<N>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for CobsDecoder<N>
where [u8; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for CobsDecoder<N>
where [u8; N]: RefUnwindSafe,

§

impl<const N: usize> Send for CobsDecoder<N>
where [u8; N]: Send,

§

impl<const N: usize> Sync for CobsDecoder<N>
where [u8; N]: Sync,

§

impl<const N: usize> Unpin for CobsDecoder<N>
where [u8; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for CobsDecoder<N>
where [u8; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for CobsDecoder<N>
where [u8; N]: UnwindSafe,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.