pub trait Encode {
// Required methods
fn encode(&self, buf: &mut BytesMut) -> Result<(), Error>;
fn payload_len(&self) -> usize;
}Expand description
A trait for encoding MQTT packets into a buffer.
Types that implement this trait can be serialized into a BytesMut buffer for
transmission over the network.
Required Methods§
Sourcefn encode(&self, buf: &mut BytesMut) -> Result<(), Error>
fn encode(&self, buf: &mut BytesMut) -> Result<(), Error>
Encodes the packet into the provided buffer.
§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![0x30, 0x00] };
let mut buffer = BytesMut::new();
packet.encode(&mut buffer).unwrap();
assert_eq!(buffer.to_vec(), vec![0x30, 0x00]);Sourcefn payload_len(&self) -> usize
fn payload_len(&self) -> usize
Returns the length of the payload in bytes.
§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.payload_len(), 2);