Skip to main content

max_encoded_len

Function max_encoded_len 

Source
pub const fn max_encoded_len(payload_len: usize) -> usize
Expand description

Returns an output length that is always large enough to hold the COBS encoding of a payload of payload_len bytes, including the trailing DELIMITER.

COBS adds one code byte for every run of up to 254 bytes, plus the one delimiter, so the overhead is bounded and small. This rounds the run count up and so may return one byte more than the tightest possible encoding, which only ever over-allocates the buffer.

§Arguments

  • payload_len - the length of the payload to be encoded.

§Returns

The maximum number of bytes encode can write for that payload.

§Examples

use pamoja_serial::cobs;

// A short payload costs one code byte and one delimiter on top of the data.
assert_eq!(cobs::max_encoded_len(10), 12);