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>
impl<const N: usize> SlipDecoder<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 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.