Skip to main content

decode

Function decode 

Source
pub fn decode(frame: &[u8], output: &mut [u8]) -> Result<usize, SerialError>
Expand description

Decodes a single COBS frame, recovering the original payload.

Decoding stops at the DELIMITER that closes the frame, or at the end of the slice if it carries no trailing delimiter.

§Arguments

  • frame - the encoded bytes, with or without the trailing DELIMITER.
  • output - the buffer the payload is written into; it never needs more room than frame.

§Returns

The number of payload bytes written to output.

§Errors

Returns SerialError::TruncatedFrame if a code byte claims more data than the frame carries (which also catches a stray zero inside a run), and SerialError::BufferTooSmall if output cannot hold the payload.

§Examples

use pamoja_serial::cobs;

let mut payload = [0u8; 4];
let n = cobs::decode(&[0x03, 0x11, 0x22, 0x02, 0x33, 0x00], &mut payload)?;
assert_eq!(&payload[..n], &[0x11, 0x22, 0x00, 0x33]);