Skip to main content

FixedHeader

Struct FixedHeader 

Source
pub struct FixedHeader { /* private fields */ }
Expand description

Packet header types and fixed header implementation 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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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 length
Source

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 payload
Source

pub fn encode(&self, buf: &mut BytesMut) -> Result<(), Error>

Encodes the fixed header into a buffer.

§Errors

Returns an error if encoding fails.

§Examples
use mqute_codec::protocol::{FixedHeader, PacketType};
use bytes::BytesMut;

let mut buf = BytesMut::new();
let header = FixedHeader::new(PacketType::Publish, 10);
header.encode(&mut buf).unwrap();
Source

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

Source§

fn clone(&self) -> FixedHeader

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for FixedHeader

Source§

impl Debug for FixedHeader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for FixedHeader

Source§

impl PartialEq for FixedHeader

Source§

fn eq(&self, other: &FixedHeader) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for FixedHeader

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.