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:
- Sender sends PUBLISH packet with QoS 2
- Receiver responds with PUBREC packet
- Sender responds with PUBREL packet
- 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 diagnosticsUserProperty
: Name-value pairs for application-specific metadata
Only one ReasonString
property is allowed per packet.
Implementations§
Source§impl<PacketIdType> GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn props(&self) -> &Option<Properties>
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 diagnosticsUserProperty
: Name-value pairs for application-specific metadata
Only one ReasonString
property is allowed per packet.
Source§impl<PacketIdType> GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn packet_type() -> PacketType
pub fn packet_type() -> PacketType
Sourcepub fn packet_id(&self) -> PacketIdType
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);
Sourcepub fn reason_code(&self) -> Option<PubcompReasonCode>
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));
Sourcepub fn size(&self) -> usize
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);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
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());
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
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 consumedErr(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 AsConcrete<GenericPubcomp<u16>> for Packet
impl AsConcrete<GenericPubcomp<u16>> for Packet
fn as_concrete(&self) -> Option<&Pubcomp>
Source§impl<PacketIdType> Clone for GenericPubcomp<PacketIdType>
impl<PacketIdType> Clone for GenericPubcomp<PacketIdType>
Source§fn clone(&self) -> GenericPubcomp<PacketIdType>
fn clone(&self) -> GenericPubcomp<PacketIdType>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<PacketIdType> Debug for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug implementation for PUBCOMP packets.
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§impl<PacketIdType> Display for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display implementation for PUBCOMP packets.
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§impl<PacketIdType> From<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: GenericPubcomp<PacketIdType>) -> GenericPacket<PacketIdType>
fn from(v: GenericPubcomp<PacketIdType>) -> GenericPacket<PacketIdType>
Source§impl<PacketIdType> GenericPacketDisplay for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
GenericPacketDisplay implementation for PUBCOMP packets.
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§impl<PacketIdType> GenericPacketTrait for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
GenericPacketTrait implementation for PUBCOMP packets.
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§impl IntoConcreteOwned<GenericPubcomp<u16>> for Packet
impl IntoConcreteOwned<GenericPubcomp<u16>> for Packet
fn into_concrete_owned(self) -> Option<Pubcomp>
Source§impl<PacketIdType> PacketKind for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> PacketKind for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
Source§const IS_PUBCOMP: bool = true
const IS_PUBCOMP: bool = true
true
if this is a PUBCOMP packetSource§const IS_CONNECT: bool = false
const IS_CONNECT: bool = false
true
if this is a CONNECT packetSource§const IS_CONNACK: bool = false
const IS_CONNACK: bool = false
true
if this is a CONNACK packetSource§const IS_PUBLISH: bool = false
const IS_PUBLISH: bool = false
true
if this is a PUBLISH packetSource§const IS_SUBSCRIBE: bool = false
const IS_SUBSCRIBE: bool = false
true
if this is a SUBSCRIBE packetSource§const IS_UNSUBSCRIBE: bool = false
const IS_UNSUBSCRIBE: bool = false
true
if this is an UNSUBSCRIBE packetSource§const IS_UNSUBACK: bool = false
const IS_UNSUBACK: bool = false
true
if this is an UNSUBACK packetSource§const IS_PINGREQ: bool = false
const IS_PINGREQ: bool = false
true
if this is a PINGREQ packetSource§const IS_PINGRESP: bool = false
const IS_PINGRESP: bool = false
true
if this is a PINGRESP packetSource§const IS_DISCONNECT: bool = false
const IS_DISCONNECT: bool = false
true
if this is a DISCONNECT packetSource§impl<PacketIdType> PartialEq for GenericPubcomp<PacketIdType>
impl<PacketIdType> PartialEq for GenericPubcomp<PacketIdType>
Source§fn eq(&self, other: &GenericPubcomp<PacketIdType>) -> bool
fn eq(&self, other: &GenericPubcomp<PacketIdType>) -> bool
self
and other
values to be equal, and is used by ==
.Source§impl<PacketIdType> Serialize for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize implementation for PUBCOMP packets.
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 identifierreason_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§impl<PacketIdType> TryInto<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<GenericPubcomp<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> Eq for GenericPubcomp<PacketIdType>
impl<PacketIdType> SendableRole<Any> for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Client> for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Server> for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> StructuralPartialEq for GenericPubcomp<PacketIdType>where
PacketIdType: IsPacketId,
Auto Trait Implementations§
impl<PacketIdType> Freeze for GenericPubcomp<PacketIdType>
impl<PacketIdType> RefUnwindSafe for GenericPubcomp<PacketIdType>
impl<PacketIdType> Send for GenericPubcomp<PacketIdType>
impl<PacketIdType> Sync for GenericPubcomp<PacketIdType>
impl<PacketIdType> Unpin for GenericPubcomp<PacketIdType>
impl<PacketIdType> UnwindSafe for GenericPubcomp<PacketIdType>
Blanket Implementations§
Source§impl<T> AsConcrete<T> for T
impl<T> AsConcrete<T> for T
fn as_concrete(&self) -> Option<&T>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.