pub fn encode_slice(packet: &Packet<'_>, buf: &mut [u8]) -> Result<usize, Error>
Expand description

Encode a Packet enum into a u8 slice.

// Instantiate a `Packet` to encode.
let packet = Publish {
   dup: false,
   qos: QoS::AtMostOnce,
   retain: false,
   topic_name: "test",
   payload: b"hello",
   pid: None,
}.into();

// Allocate buffer (should be appropriately-sized or able to grow as needed).
let mut buf = [0u8; 1024];

// Write bytes corresponding to `&Packet` into the `BytesMut`.
let len = encode_slice(&packet, &mut buf).expect("failed encoding");
assert_eq!(&buf[..len], &[0b00110000, 11,
                    0, 4, b't', b'e', b's', b't',
                   b'h', b'e', b'l', b'l', b'o']);