len_bytes

Function len_bytes 

Source
pub fn len_bytes(len: usize) -> usize
Expand description

Calculates the number of bytes required to encode a length value using MQTT’s variable byte integer encoding format.

MQTT uses a variable-length encoding scheme for remaining length values where each byte encodes 7 bits of data with the most significant bit indicating continuation. This function determines how many bytes are needed to encode a given length value.

§Panics

Panics if the length exceeds the maximum allowed by MQTT specification (268,435,455 bytes or ~256 MB).

§MQTT Specification Reference

This implements the variable byte integer encoding from MQTT specification section 1.5.5.

§Example

use mqute_codec::protocol::util;

assert_eq!(util::len_bytes(127), 1);    // Fits in 1 byte
assert_eq!(util::len_bytes(128), 2);    // Requires 2 bytes
assert_eq!(util::len_bytes(16383), 2);  // Maximum for 2 bytes
assert_eq!(util::len_bytes(16384), 3);  // Requires 3 bytes