Encoded

Trait Encoded 

Source
pub trait Encoded: Encode {
    // Required method
    fn encoded_len(&self) -> usize;
}
Expand description

A trait for calculating the total encoded length of an MQTT packet.

This trait is automatically implemented for all types that implement Encode.

§Examples

use mqute_codec::codec::{Encode, Encoded};
use bytes::BytesMut;
use mqute_codec::Error;

struct Packet {
    payload: Vec<u8>,
}

impl Encode for Packet {
    fn encode(&self, buf: &mut BytesMut) -> Result<(), Error> {
        buf.extend_from_slice(&self.payload);
        Ok(())
    }

    fn payload_len(&self) -> usize {
        self.payload.len()
    }
}

let packet = Packet { payload: vec![0x00, 0x01] };
assert_eq!(packet.encoded_len(), 4); // 1 byte for control byte, 1 byte for remaining length, 2 bytes for payload

Required Methods§

Source

fn encoded_len(&self) -> usize

Calculates the total encoded length of the packet.

The total length includes:

  • 1 byte for the control byte.
  • Variable bytes for the remaining length (encoded as a variable byte integer).
  • The length of the payload.

Implementors§

Source§

impl<T> Encoded for T
where T: Encode,