fn encode_variable_byte(data: &VariableByte) -> Result<Vec<u8>, Error> {
    let mut bytes = vec![];
    let mut number: u32 = match data {
      VariableByte::One(value) => u32::from(*value),
      VariableByte::Two(value) => u32::from(*value),
      VariableByte::Three(value) => *value,
      VariableByte::Four(value) => *value,
    };

    if number > 268_435_455 {
      return Err(Error::GenerateError);
    }

    loop {
      // we are safe to unwrap here because (number % 128) will neveer be bigger than 127
      let mut encoded_byte: u8 = (number % 128).try_into().unwrap();
      number /= 128;

      if number > 0 {
        encoded_byte |= 128;
        bytes.push(encoded_byte);
      } else {
        bytes.push(encoded_byte);
        break;
      }
    }

    Ok(bytes)
  }
/// Used by to_vec() to format variable byte ints into the format defined in the
/// MQTT v5 spec.
///
/// The algorithm for encoding a non-negative integer (X) into the Variable Byte
/// Integer encoding scheme is as follows:
///
/// ```txt
/// do
///    encodedByte = X MOD 128
///    X = X DIV 128
///    // if there are more data to encode, set the top bit of this byte
///    if (X > 0)
///       encodedByte = encodedByte OR 128
///    endif
///    'output' encodedByte
/// while (X > 0)
/// ```
///
/// Where MOD is the modulo operator (% in C), DIV is integer division (/ in C),
/// and OR is bit-wise or (| in C).
Used by to_vec() to format variable byte ints into the format defined in the
MQTT v5 spec.

The algorithm for encoding a non-negative integer (X) into the Variable Byte
Integer encoding scheme is as follows.




Used by to_vec () to format variable byte ints into the format defined in the MQTT v5 spec . The algorithm for encoding a non - negative integer ( X ) into the Variable Byte Integer encoding scheme is as follows .
fn encode_variable_byte(data: &VariableByte) -> Result<Vec<u8>, Error> {
    let mut bytes = vec![];
    let mut number: u32 = match data {
      VariableByte::One(value) => u32::from(*value),
      VariableByte::Two(value) => u32::from(*value),
      VariableByte::Three(value) => *value,
      VariableByte::Four(value) => *value,
    };
    if number > 268_435_455 {
      return Err(Error::GenerateError);
    }
    loop {
      // we are safe to unwrap here because (number % 128) will neveer be bigger than 127
      let mut encoded_byte: u8 = (number % 128).try_into().unwrap();
      number /= 128;
      if number > 0 {
        encoded_byte |= 128;
        bytes.push(encoded_byte);
      } else {
        bytes.push(encoded_byte);
        break;
      }
    }
    Ok(bytes)
  }
fn encode_variable_byte ( data : & VariableByte ) -> Result < Vec < u8 > , Error > { let mut bytes = vec ! [ ] ; let mut number : u32 = match data { VariableByte :: One ( value ) => u32 :: from ( * value ) , VariableByte :: Two ( value ) => u32 :: from ( * value ) , VariableByte :: Three ( value ) => * value , VariableByte :: Four ( value ) => * value , } ; if number > 268_435_455 { return Err ( Error :: GenerateError ) ; } loop { // we are safe to unwrap here because (number % 128) will neveer be bigger than 127 let mut encoded_byte : u8 = ( number % 128 ) . try_into ( ) . unwrap ( ) ; number /= 128 ; if number > 0 { encoded_byte |= 128 ; bytes . push ( encoded_byte ) ; } else { bytes . push ( encoded_byte ) ; break ; } } Ok ( bytes ) }
Used by to_vec() to format variable byte ints into the format defined in the
MQTT v5 spec.
Used by to_vec () to format variable byte ints into the format defined in the MQTT v5 spec .
data
&VariableByte
data
&VariableByte