Rust

wire

Module

The primitive encodings MQTT packets are built from: variable byte integers, length-prefixed strings, two-byte integers and single bytes.

Contents

use utopia_mqtt::wire::Length;
Rust
pub enum Length

A decoded variable byte integer.

Rust
use utopia_mqtt::wire::{Length, decode_length};

assert_eq!(decode_length(&[0xC8, 0x01], 0), (Length::Int(200), 2));
assert_eq!(decode_length(&[0x80; 9].iter().chain(&[0x01]).copied().collect::<Vec<_>>(), 0).0, Length::Float(9223372036854775808.0));
Also implements From.
use utopia_mqtt::wire::decode_length;
Rust
pub fn decode_length(data: &[u8], offset: usize) -> (Length, usize)

Decodes the variable byte integer at offset: the value and the number of bytes it took.

use utopia_mqtt::wire::encode_length;
Rust
pub fn encode_length(length: u64, out: &mut Vec<u8>)

Appends length as a variable byte integer (one to ten bytes; MQTT allows up to four).

use utopia_mqtt::wire::encode_string;
Rust
pub fn encode_string(value: &[u8], out: &mut Vec<u8>)

Appends value with its two-byte length prefix. A value longer than 65,535 bytes keeps the low 16 bits of its length in the prefix.

use utopia_mqtt::wire::read_byte;
Rust
pub fn read_byte(data: &[u8], offset: usize) -> (u8, usize)

Reads the byte at offset (a reason code, a protocol level, a flags byte): its value and the offset past it.

use utopia_mqtt::wire::read_int16;
Rust
pub fn read_int16(data: &[u8], offset: usize) -> (u16, usize)

Reads the two-byte big-endian integer at offset (a packet id, a keep-alive): its value and the offset past it.

use utopia_mqtt::wire::read_string;
Rust
pub fn read_string(data: &[u8], offset: usize) -> (&[u8], usize)

Reads the length-prefixed string at offset: its bytes and the offset past it.