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>
impl<const N: usize> CobsDecoder<N>
Sourcepub const fn new() -> Self
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.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Discards any partly assembled frame, returning the decoder to its initial state.
Sourcepub fn push(&mut self, byte: u8) -> Result<Option<&[u8]>, SerialError>
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.