GenericPubcomp

Struct GenericPubcomp 

Source
pub struct GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,
{ pub props: Option<Properties>, /* private fields */ }
Expand description

A PUBCOMP packet for MQTT v5.0 protocol.

The PUBCOMP packet is the fourth and final packet in the QoS 2 PUBLISH message flow. It is sent by the sender of the original PUBLISH packet in response to a PUBREL packet from the receiver, completing the QoS 2 message delivery guarantee.

§MQTT v5.0 Specification

According to the MQTT v5.0 specification, the PUBCOMP packet:

  • Is the final packet in the QoS 2 PUBLISH message handshake
  • Is sent by the original sender in response to a PUBREL packet
  • Contains the same Packet Identifier as the original PUBLISH packet
  • May optionally include a reason code indicating the result of the message completion
  • May optionally include properties for additional metadata
  • Completes the QoS 2 message delivery flow: PUBLISH -> PUBREC -> PUBREL -> PUBCOMP

§Generic Support

This struct supports generic packet identifiers through the PacketIdType parameter, allowing for extended packet ID ranges (e.g., u32) for broker clustering scenarios. The standard type alias Pubcomp uses u16 packet identifiers as per MQTT specification.

§QoS 2 Message Flow

The PUBCOMP packet is part of the QoS 2 message delivery flow:

  1. Sender sends PUBLISH packet with QoS 2
  2. Receiver responds with PUBREC packet
  3. Sender responds with PUBREL packet
  4. Receiver responds with PUBCOMP packet (this packet)

§Examples

Creating a basic PUBCOMP packet:

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(123u16)
    .build()
    .unwrap();

assert_eq!(pubcomp.packet_id(), 123u16);

Creating a PUBCOMP with reason code:

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(456u16)
    .reason_code(mqtt::result_code::PubcompReasonCode::Success)
    .build()
    .unwrap();

assert_eq!(pubcomp.packet_id(), 456u16);
assert_eq!(pubcomp.reason_code(), Some(mqtt::result_code::PubcompReasonCode::Success));

Fields§

§props: Option<Properties>

Optional MQTT v5.0 properties associated with this PUBCOMP packet.

Properties can include:

  • ReasonString: Human readable string designed for diagnostics
  • UserProperty: Name-value pairs for application-specific metadata

Only one ReasonString property is allowed per packet.

Implementations§

Source§

impl<PacketIdType> GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source

pub fn props(&self) -> &Option<Properties>

Optional MQTT v5.0 properties associated with this PUBCOMP packet.

Properties can include:

  • ReasonString: Human readable string designed for diagnostics
  • UserProperty: Name-value pairs for application-specific metadata

Only one ReasonString property is allowed per packet.

Source§

impl<PacketIdType> GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source

pub fn builder() -> GenericPubcompBuilder<PacketIdType>

Creates a new builder for constructing a PUBCOMP packet.

§Returns

A new GenericPubcompBuilder instance for building PUBCOMP packets.

§Examples
use mqtt_protocol_core::mqtt;

let builder = mqtt::packet::v5_0::Pubcomp::builder();
let pubcomp = builder
    .packet_id(42u16)
    .build()
    .unwrap();
Source

pub fn packet_type() -> PacketType

Returns the packet type for PUBCOMP packets.

§Returns

Always returns PacketType::Pubcomp.

§Examples
use mqtt_protocol_core::mqtt;

let packet_type = mqtt::packet::v5_0::Pubcomp::packet_type();
assert_eq!(packet_type, mqtt::packet::packet_type::PacketType::Pubcomp);
Source

pub fn packet_id(&self) -> PacketIdType

Returns the packet identifier of this PUBCOMP packet.

The packet identifier must match the packet identifier of the original PUBLISH packet in the QoS 2 message flow.

§Returns

The packet identifier as the specified PacketIdType.

§Examples
use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1337u16)
    .build()
    .unwrap();

assert_eq!(pubcomp.packet_id(), 1337u16);
Source

pub fn reason_code(&self) -> Option<PubcompReasonCode>

Returns the reason code of this PUBCOMP packet, if present.

The reason code indicates the result of the QoS 2 message completion. If no reason code is present, it implies successful completion (equivalent to PubcompReasonCode::Success).

§Returns

An Option<PubcompReasonCode> containing the reason code if present, or None if no reason code was included in the packet.

§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

// PUBCOMP without reason code (implies success)
let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

assert_eq!(pubcomp.reason_code(), None);

// PUBCOMP with explicit reason code
let pubcomp_with_reason = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(2u16)
    .reason_code(mqtt::result_code::PubcompReasonCode::PacketIdentifierNotFound)
    .build()
    .unwrap();

assert_eq!(pubcomp_with_reason.reason_code(),
           Some(mqtt::result_code::PubcompReasonCode::PacketIdentifierNotFound));
Source

pub fn size(&self) -> usize

Returns the total size of the PUBCOMP packet in bytes.

This includes the fixed header, remaining length field, packet identifier, optional reason code, optional property length, and optional properties.

§Returns

The total packet size in bytes.

§Examples
use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

let size = pubcomp.size();
// Minimum size: 1 (fixed header) + 1 (remaining length) + 2 (packet id) = 4 bytes
assert!(size >= 4);
Source

pub fn to_buffers(&self) -> Vec<IoSlice<'_>>

Converts the PUBCOMP packet into a vector of I/O slices for efficient transmission.

This method prepares the packet data for network transmission by organizing it into a series of byte slices that can be sent without additional copying.

§Returns

A Vec<IoSlice<'_>> containing the packet data organized as I/O slices. The slices are ordered as: fixed header, remaining length, packet identifier, optional reason code, optional property length, and optional properties.

§Examples
use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

let buffers = pubcomp.to_buffers();
assert!(!buffers.is_empty());
Source

pub fn to_continuous_buffer(&self) -> Vec<u8>

Create a continuous buffer containing the complete packet data

Returns a vector containing all packet bytes in a single continuous buffer. This method is compatible with no-std environments and provides an alternative to to_buffers() when vectored I/O is not needed.

The returned buffer contains the complete PUBCOMP packet serialized according to the MQTT v5.0 protocol specification, including fixed header, remaining length, packet identifier, reason code, and properties.

§Returns

A vector containing the complete packet data

§Examples
use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

let buffer = pubcomp.to_continuous_buffer();
// buffer contains all packet bytes
Source

pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>

Parses a PUBCOMP packet from raw bytes.

This method deserializes a byte array into a PUBCOMP packet structure, validating the packet format and extracting all components including packet identifier, optional reason code, and optional properties.

§Parameters
  • data - A byte slice containing the PUBCOMP packet data (excluding fixed header and remaining length)
§Returns

A Result containing:

  • Ok((Self, usize)) - The parsed PUBCOMP packet and the number of bytes consumed
  • Err(MqttError) - An error if the packet is malformed or invalid
§Errors

Returns MqttError::MalformedPacket if:

  • The packet identifier is zero (invalid)
  • The data is too short to contain a valid packet identifier
  • The reason code is invalid
  • Properties are malformed

Returns MqttError::ProtocolError if:

  • Invalid properties are present for PUBCOMP packets
  • More than one ReasonString property is present
§Examples
use mqtt_protocol_core::mqtt;

// Parse a minimal PUBCOMP packet (packet ID only)
let data = [0x00, 0x01]; // packet ID = 1
let (pubcomp, consumed) = mqtt::packet::v5_0::Pubcomp::parse(&data).unwrap();
assert_eq!(pubcomp.packet_id(), 1u16);
assert_eq!(consumed, 2);

Trait Implementations§

Source§

impl<PacketIdType> Clone for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Clone, PacketIdType::Buffer: Clone,

Source§

fn clone(&self) -> GenericPubcomp<PacketIdType>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<PacketIdType> Debug for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Debug implementation for PUBCOMP packets.

Uses the same formatting as Display, providing JSON output for debugging. This ensures consistent representation in debug output, logs, and error messages.

§Examples

use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(42u16)
    .build()
    .unwrap();

println!("{:?}", pubcomp);
// Output: {"type":"pubcomp","packet_id":42}
Source§

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

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

impl<PacketIdType> Display for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Display implementation for PUBCOMP packets.

Formats the PUBCOMP packet as a JSON string for human-readable output. This is useful for logging, debugging, and diagnostic purposes.

§Examples

use mqtt_protocol_core::mqtt;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(42u16)
    .build()
    .unwrap();

println!("{}", pubcomp);
// Output: {"type":"pubcomp","packet_id":42}
Source§

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

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

impl<PacketIdType> From<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

fn from(v: GenericPubcomp<PacketIdType>) -> GenericPacket<PacketIdType>

Converts to this type from the input type.
Source§

impl<PacketIdType> GenericPacketDisplay for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

GenericPacketDisplay implementation for PUBCOMP packets.

This trait provides unified display formatting for all MQTT packet types, enabling consistent output across different packet implementations.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

// Use generic display interface
println!("{}", pubcomp); // Uses fmt_display
println!("{:?}", pubcomp); // Uses fmt_debug
Source§

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

Source§

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

Source§

impl<PacketIdType> GenericPacketTrait for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

GenericPacketTrait implementation for PUBCOMP packets.

This trait provides a unified interface for all MQTT packet types, enabling generic packet handling and processing.

§Examples

use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(1u16)
    .build()
    .unwrap();

// Use generic packet interface
let packet_size = pubcomp.size();
let buffers = pubcomp.to_buffers();
Source§

fn size(&self) -> usize

Source§

fn to_buffers(&self) -> Vec<IoSlice<'_>>

Source§

fn to_continuous_buffer(&self) -> Vec<u8>

Create a continuous buffer containing the complete packet data Read more
Source§

impl<PacketIdType> PacketKind for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source§

const IS_PUBCOMP: bool = true

true if this is a PUBCOMP packet
Source§

const IS_V5_0: bool = true

true if this is an MQTT v5.0 packet
Source§

const IS_CONNECT: bool = false

true if this is a CONNECT packet
Source§

const IS_CONNACK: bool = false

true if this is a CONNACK packet
Source§

const IS_PUBLISH: bool = false

true if this is a PUBLISH packet
Source§

const IS_PUBACK: bool = false

true if this is a PUBACK packet
Source§

const IS_PUBREC: bool = false

true if this is a PUBREC packet
Source§

const IS_PUBREL: bool = false

true if this is a PUBREL packet
Source§

const IS_SUBSCRIBE: bool = false

true if this is a SUBSCRIBE packet
Source§

const IS_SUBACK: bool = false

true if this is a SUBACK packet
Source§

const IS_UNSUBSCRIBE: bool = false

true if this is an UNSUBSCRIBE packet
Source§

const IS_UNSUBACK: bool = false

true if this is an UNSUBACK packet
Source§

const IS_PINGREQ: bool = false

true if this is a PINGREQ packet
Source§

const IS_PINGRESP: bool = false

true if this is a PINGRESP packet
Source§

const IS_DISCONNECT: bool = false

true if this is a DISCONNECT packet
Source§

const IS_AUTH: bool = false

true if this is an AUTH packet (v5.0 only)
Source§

const IS_V3_1_1: bool = false

true if this is an MQTT v3.1.1 packet
Source§

impl<PacketIdType> PartialEq for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + PartialEq, PacketIdType::Buffer: PartialEq,

Source§

fn eq(&self, other: &GenericPubcomp<PacketIdType>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<PacketIdType> Serialize for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Serialize implementation for PUBCOMP packets.

This implementation allows PUBCOMP packets to be serialized to JSON format for debugging, logging, and testing purposes. The serialization includes the packet type, packet identifier, optional reason code, and optional properties.

§Serialized Format

The JSON format includes:

  • type: Always “pubcomp”
  • packet_id: The packet identifier
  • reason_code: The reason code (if present)
  • props: The MQTT properties (if present)

§Examples

use mqtt_protocol_core::mqtt;
use serde_json;

let pubcomp = mqtt::packet::v5_0::Pubcomp::builder()
    .packet_id(123u16)
    .build()
    .unwrap();

let json = serde_json::to_string(&pubcomp).unwrap();
// JSON: {"type":"pubcomp","packet_id":123}
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<PacketIdType> TryInto<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

type Error = &'static str

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

fn try_into( self, ) -> Result<GenericPubcomp<PacketIdType>, <Self as TryInto<GenericPubcomp<PacketIdType>>>::Error>

Performs the conversion.
Source§

impl<PacketIdType> Eq for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId + Eq, PacketIdType::Buffer: Eq,

Source§

impl<PacketIdType> SendableRole<Any> for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source§

impl<PacketIdType> SendableRole<Client> for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source§

impl<PacketIdType> SendableRole<Server> for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Source§

impl<PacketIdType> StructuralPartialEq for GenericPubcomp<PacketIdType>
where PacketIdType: IsPacketId,

Auto Trait Implementations§

§

impl<PacketIdType> Freeze for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: Freeze,

§

impl<PacketIdType> RefUnwindSafe for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: RefUnwindSafe,

§

impl<PacketIdType> Send for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: Send,

§

impl<PacketIdType> Sync for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: Sync,

§

impl<PacketIdType> Unpin for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: Unpin,

§

impl<PacketIdType> UnwindSafe for GenericPubcomp<PacketIdType>
where <PacketIdType as IsPacketId>::Buffer: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<Role, PacketIdType, T> Sendable<Role, PacketIdType> for T
where Role: RoleType, PacketIdType: IsPacketId, T: SendableRole<Role> + SendableVersion + Display + Debug + PacketKind + SendableHelper<Role, PacketIdType>,

Source§

fn dispatch_send( self, connection: &mut GenericConnection<Role, PacketIdType>, ) -> Vec<GenericEvent<PacketIdType>>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.