pub struct FixedHeader { /* private fields */ }Expand description
Represents the fixed header of an MQTT packet.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
// Create a fixed header for a CONNECT packet
let header = FixedHeader::new(PacketType::Connect, 10);
assert_eq!(header.packet_type(), PacketType::Connect);
assert_eq!(header.remaining_len(), 10);Implementations§
Source§impl FixedHeader
impl FixedHeader
Sourcepub fn new(packet: PacketType, remaining_len: usize) -> Self
pub fn new(packet: PacketType, remaining_len: usize) -> Self
Creates a new FixedHeader with the specified packet type and remaining length.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
let header = FixedHeader::new(PacketType::Publish, 20);
assert_eq!(header.packet_type(), PacketType::Publish);Sourcepub fn try_from(control_byte: u8, remaining_len: usize) -> Result<Self, Error>
pub fn try_from(control_byte: u8, remaining_len: usize) -> Result<Self, Error>
Attempts to create a FixedHeader from a control byte and remaining length.
§Errors
Returns an error if the packet type is invalid.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
use mqute_codec::Error;
let header = FixedHeader::try_from(0x30, 10).unwrap();
assert_eq!(header.packet_type(), PacketType::Publish);Sourcepub fn with_flags(
packet_type: PacketType,
flags: Flags,
remaining_len: usize,
) -> Self
pub fn with_flags( packet_type: PacketType, flags: Flags, remaining_len: usize, ) -> Self
Creates a FixedHeader with custom flags.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType, Flags};
use mqute_codec::protocol::QoS;
let flags = Flags::new(QoS::AtLeastOnce);
let header = FixedHeader::with_flags(PacketType::Publish, flags, 15);
assert_eq!(header.flags().qos, QoS::AtLeastOnce);Sourcepub fn packet_type(&self) -> PacketType
pub fn packet_type(&self) -> PacketType
Returns the packet type encoded in the control byte.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
let header = FixedHeader::new(PacketType::Subscribe, 5);
assert_eq!(header.packet_type(), PacketType::Subscribe);Sourcepub fn flags(&self) -> Flags
pub fn flags(&self) -> Flags
Extracts and returns the flags from the control byte.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType, Flags};
use mqute_codec::protocol::QoS;
let header = FixedHeader::new(PacketType::Publish, 10);
let flags = header.flags();
assert_eq!(flags.qos, QoS::AtMostOnce);Sourcepub fn remaining_len(&self) -> usize
pub fn remaining_len(&self) -> usize
Returns the remaining length of the payload.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
let header = FixedHeader::new(PacketType::Publish, 25);
assert_eq!(header.remaining_len(), 25);Sourcepub fn fixed_len(&self) -> usize
pub fn fixed_len(&self) -> usize
Returns the length of the fixed header in bytes.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
let header = FixedHeader::new(PacketType::Publish, 10);
assert_eq!(header.fixed_len(), 2); // 1 byte for control byte, 1 byte for remaining lengthSourcepub fn packet_len(&self) -> usize
pub fn packet_len(&self) -> usize
Returns the total length of the packet (fixed header + payload).
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
let header = FixedHeader::new(PacketType::Publish, 10);
assert_eq!(header.packet_len(), 12); // 2 bytes for fixed header, 10 bytes for payloadSourcepub fn decode(
buf: &[u8],
inbound_max_size: Option<usize>,
) -> Result<Self, Error>
pub fn decode( buf: &[u8], inbound_max_size: Option<usize>, ) -> Result<Self, Error>
Decodes a fixed header from a buffer.
§Errors
Returns an error if decoding fails or the payload size exceeds the limit.
§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
use bytes::BytesMut;
let mut buf = BytesMut::from(&[0x30, 0x04,
0x00, 0x00,
0x00, 0x00][..]); // Publish packet with remaining length 4
let header = FixedHeader::decode(&buf, None).unwrap();
assert_eq!(header.packet_type(), PacketType::Publish);
assert_eq!(header.remaining_len(), 4);Trait Implementations§
Source§impl Clone for FixedHeader
impl Clone for FixedHeader
Source§fn clone(&self) -> FixedHeader
fn clone(&self) -> FixedHeader
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FixedHeader
impl Debug for FixedHeader
Source§impl PartialEq for FixedHeader
impl PartialEq for FixedHeader
impl Copy for FixedHeader
impl Eq for FixedHeader
impl StructuralPartialEq for FixedHeader
Auto Trait Implementations§
impl Freeze for FixedHeader
impl RefUnwindSafe for FixedHeader
impl Send for FixedHeader
impl Sync for FixedHeader
impl Unpin for FixedHeader
impl UnwindSafe for FixedHeader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more