pub struct GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,{
pub props: Option<Properties>,
/* private fields */
}
Expand description
A PUBACK packet for MQTT v5.0 protocol.
The PUBACK packet is the response to a PUBLISH packet with QoS level 1. This packet acknowledges the receipt of a PUBLISH packet and may contain a reason code and properties to provide additional information about the acknowledgment status.
§MQTT v5.0 Specification
According to the MQTT v5.0 specification, the PUBACK packet:
- Is sent by the receiver of a PUBLISH packet with QoS 1
- Contains the same Packet Identifier as the PUBLISH packet being acknowledged
- May optionally include a reason code indicating the result of the PUBLISH processing
- May optionally include properties for additional metadata
§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 Puback
uses u16
packet identifiers as per MQTT specification.
§Examples
Creating a basic PUBACK packet:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(123u16)
.build()
.unwrap();
assert_eq!(puback.packet_id(), 123u16);
Creating a PUBACK with reason code:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(456u16)
.reason_code(mqtt::result_code::PubackReasonCode::Success)
.build()
.unwrap();
assert_eq!(puback.packet_id(), 456u16);
assert_eq!(puback.reason_code(), Some(mqtt::result_code::PubackReasonCode::Success));
Fields§
§props: Option<Properties>
Optional MQTT v5.0 properties associated with this PUBACK 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> GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn props(&self) -> &Option<Properties>
pub fn props(&self) -> &Option<Properties>
Optional MQTT v5.0 properties associated with this PUBACK 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> GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPuback<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 PUBACK packet.
The packet identifier must match the packet identifier of the PUBLISH packet that this PUBACK is acknowledging.
§Returns
The packet identifier as the specified PacketIdType
.
§Examples
use mqtt_protocol_core::mqtt;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(1337u16)
.build()
.unwrap();
assert_eq!(puback.packet_id(), 1337u16);
Sourcepub fn reason_code(&self) -> Option<PubackReasonCode>
pub fn reason_code(&self) -> Option<PubackReasonCode>
Returns the reason code of this PUBACK packet, if present.
The reason code indicates the result of the PUBLISH packet processing.
If no reason code is present, it implies successful processing
(equivalent to PubackReasonCode::Success
).
§Returns
An Option<PubackReasonCode>
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::*;
// PUBACK without reason code (implies success)
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(1u16)
.build()
.unwrap();
assert_eq!(puback.reason_code(), None);
// PUBACK with explicit reason code
let puback_with_reason = mqtt::packet::v5_0::Puback::builder()
.packet_id(2u16)
.reason_code(mqtt::result_code::PubackReasonCode::NoMatchingSubscribers)
.build()
.unwrap();
assert_eq!(puback_with_reason.reason_code(),
Some(mqtt::result_code::PubackReasonCode::NoMatchingSubscribers));
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total size of the PUBACK 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 puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(1u16)
.build()
.unwrap();
let size = puback.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 PUBACK 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 puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(1u16)
.build()
.unwrap();
let buffers = puback.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 PUBACK packet from raw bytes.
This method deserializes a byte array into a PUBACK 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 PUBACK packet data (excluding fixed header and remaining length)
§Returns
A Result
containing:
Ok((Self, usize))
- The parsed PUBACK 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 PUBACK packets
- More than one ReasonString property is present
§Examples
use mqtt_protocol_core::mqtt;
// Parse PUBACK packet data
let data = &[0x00, 0x01]; // packet_id = 1
let (puback, consumed) = mqtt::packet::v5_0::Puback::parse(data).unwrap();
assert_eq!(puback.packet_id(), 1u16);
assert_eq!(consumed, 2);
Trait Implementations§
Source§impl AsConcrete<GenericPuback<u16>> for Packet
impl AsConcrete<GenericPuback<u16>> for Packet
fn as_concrete(&self) -> Option<&Puback>
Source§impl<PacketIdType> Clone for GenericPuback<PacketIdType>
impl<PacketIdType> Clone for GenericPuback<PacketIdType>
Source§fn clone(&self) -> GenericPuback<PacketIdType>
fn clone(&self) -> GenericPuback<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 GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug
implementation for GenericPuback
.
impl<PacketIdType> Debug for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug
implementation for GenericPuback
.
Uses the same JSON formatting as the Display
implementation
to provide consistent debug output across the library.
§Examples
use mqtt_protocol_core::mqtt;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(456u16)
.build()
.unwrap();
println!("{:?}", puback);
// Output: {"type":"puback","packet_id":456}
Source§impl<PacketIdType> Display for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display
implementation for GenericPuback
.
impl<PacketIdType> Display for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display
implementation for GenericPuback
.
Formats the PUBACK packet as JSON for human-readable output. This is useful for logging, debugging, and diagnostic purposes.
§Output Format
The output is JSON formatted and includes all packet fields. If serialization fails, an error message is displayed instead.
§Examples
use mqtt_protocol_core::mqtt;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(123u16)
.build()
.unwrap();
println!("{}", puback);
// Output: {"type":"puback","packet_id":123}
Source§impl<PacketIdType> From<GenericPuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<GenericPuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: GenericPuback<PacketIdType>) -> GenericPacket<PacketIdType>
fn from(v: GenericPuback<PacketIdType>) -> GenericPacket<PacketIdType>
Source§impl<PacketIdType> GenericPacketDisplay for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
GenericPacketDisplay
implementation for GenericPuback
.
impl<PacketIdType> GenericPacketDisplay for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
GenericPacketDisplay
implementation for GenericPuback
.
Provides formatted display capabilities for the PUBACK packet that can be used by the generic packet display system. This enables consistent formatting across all packet types.
§Trait Methods
fmt_debug()
: Debug formatting (JSON output)fmt_display()
: Display formatting (JSON output)
Source§impl<PacketIdType> GenericPacketTrait for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
GenericPacketTrait
implementation for GenericPuback
.
impl<PacketIdType> GenericPacketTrait for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
GenericPacketTrait
implementation for GenericPuback
.
Provides the core packet interface methods required by the MQTT protocol library. This trait enables the PUBACK packet to be used generically alongside other packet types in the protocol processing pipeline.
§Trait Methods
size()
: Returns the total packet size in bytesto_buffers()
: Converts the packet to I/O slices for transmission
Source§impl IntoConcreteOwned<GenericPuback<u16>> for Packet
impl IntoConcreteOwned<GenericPuback<u16>> for Packet
fn into_concrete_owned(self) -> Option<Puback>
Source§impl<PacketIdType> PacketKind for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> PacketKind for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
Source§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_PUBCOMP: bool = false
const IS_PUBCOMP: bool = false
true
if this is a PUBCOMP 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 GenericPuback<PacketIdType>
impl<PacketIdType> PartialEq for GenericPuback<PacketIdType>
Source§impl<PacketIdType> Serialize for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize
implementation for GenericPuback
.
impl<PacketIdType> Serialize for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize
implementation for GenericPuback
.
Serializes the PUBACK packet into a structured format suitable for JSON output or other serialization formats. The serialization includes the packet type, packet identifier, optional reason code, and optional properties.
§Serialized Fields
type
: Always “puback”packet_id
: The packet identifierreason_code
: Present only if a reason code was setprops
: Present only if properties were set
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let puback = mqtt::packet::v5_0::Puback::builder()
.packet_id(42u16)
.reason_code(mqtt::result_code::PubackReasonCode::Success)
.build()
.unwrap();
let json = serde_json::to_string(&puback).unwrap();
// JSON will contain: {"type":"puback","packet_id":42,"reason_code":"Success"}
Source§impl<PacketIdType> TryInto<GenericPuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<GenericPuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> Eq for GenericPuback<PacketIdType>
impl<PacketIdType> SendableRole<Any> for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Client> for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Server> for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> StructuralPartialEq for GenericPuback<PacketIdType>where
PacketIdType: IsPacketId,
Auto Trait Implementations§
impl<PacketIdType> Freeze for GenericPuback<PacketIdType>
impl<PacketIdType> RefUnwindSafe for GenericPuback<PacketIdType>
impl<PacketIdType> Send for GenericPuback<PacketIdType>
impl<PacketIdType> Sync for GenericPuback<PacketIdType>
impl<PacketIdType> Unpin for GenericPuback<PacketIdType>
impl<PacketIdType> UnwindSafe for GenericPuback<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.