Skip to main content

encode

Function encode 

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

Encodes a payload into a SLIP frame, terminated by an END byte.

Every END in the payload is written as ESC ESC_END and every ESC as ESC ESC_ESC; all other bytes pass through unchanged. A single END is appended to mark the end of the frame. A sender that wants RFC 1055’s noise-flushing behaviour may prepend an extra END of its own; decode and SlipDecoder ignore it.

§Arguments

  • payload - the bytes to frame.
  • output - the buffer the frame is written into; size it with max_encoded_len.

§Returns

The number of bytes written to output.

§Errors

Returns SerialError::BufferTooSmall if output cannot hold the whole frame.

§Examples

use pamoja_serial::slip::{self, END, ESC, ESC_END};

// A payload containing the delimiter byte is escaped, never emitted raw.
let mut frame = [0u8; 8];
let n = slip::encode(&[0x01, END, 0x02], &mut frame)?;
assert_eq!(&frame[..n], &[0x01, ESC, ESC_END, 0x02, END]);