pub struct GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,{
pub props: Option<Properties>,
/* private fields */
}
Expand description
A PUBREC packet for MQTT v5.0 protocol.
The PUBREC packet is the response to a PUBLISH packet with QoS level 2 and is the second step in the four-part handshake for QoS 2 message delivery. The complete QoS 2 flow is:
- PUBLISH (sender -> receiver)
- PUBREC (receiver -> sender) - this packet
- PUBREL (sender -> receiver)
- PUBCOMP (receiver -> sender)
§MQTT v5.0 Specification
According to the MQTT v5.0 specification, the PUBREC packet:
- Is sent by the receiver of a PUBLISH packet with QoS 2
- Contains the same Packet Identifier as the PUBLISH packet being acknowledged
- Indicates that the message has been received but not yet processed
- May optionally include a reason code indicating the result of the message reception
- May optionally include properties for additional metadata
- Must be followed by a PUBREL packet from the original sender
§QoS 2 Message Flow
The PUBREC packet ensures exactly-once delivery semantics:
- Upon receiving a QoS 2 PUBLISH, the receiver stores the message and sends PUBREC
- The PUBREC acknowledges message receipt and requests the sender to proceed
- The sender must respond with PUBREL to confirm message processing should continue
- Only after receiving PUBREL does the receiver process the message and send PUBCOMP
§Reason Codes
PUBREC packets can include reason codes to indicate the result of message processing:
Success
(0x00): Message received successfullyNoMatchingSubscribers
(0x10): No subscribers matched the topicUnspecifiedError
(0x80): An unspecified error occurredImplementationSpecificError
(0x83): Implementation-specific errorNotAuthorized
(0x87): Client not authorized to publish to this topicTopicNameInvalid
(0x90): Topic name format is invalidPacketIdentifierInUse
(0x91): Packet identifier already in useQuotaExceeded
(0x97): Quota exceededPayloadFormatInvalid
(0x99): Payload format does not match the format indicator
§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 Pubrec
uses u16
packet identifiers as per MQTT specification.
§Examples
Creating a basic PUBREC packet:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
assert_eq!(pubrec.packet_id(), 123u16);
assert_eq!(pubrec.reason_code(), None);
Creating a PUBREC with reason code:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(456u16)
.reason_code(mqtt::result_code::PubrecReasonCode::Success)
.build()
.unwrap();
assert_eq!(pubrec.packet_id(), 456u16);
assert_eq!(pubrec.reason_code(), Some(mqtt::result_code::PubrecReasonCode::Success));
Creating a PUBREC with properties:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let properties = vec![
mqtt::packet::Property::ReasonString("Message received".to_string()),
mqtt::packet::Property::UserProperty(("key".to_string(), "value".to_string())),
];
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(789u16)
.reason_code(mqtt::result_code::PubrecReasonCode::Success)
.props(properties)
.build()
.unwrap();
assert_eq!(pubrec.packet_id(), 789u16);
assert!(pubrec.props().is_some());
Fields§
§props: Option<Properties>
Optional MQTT v5.0 properties associated with this PUBREC 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.
Properties can only be included if a reason code is also present.
Implementations§
Source§impl<PacketIdType> GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn props(&self) -> &Option<Properties>
pub fn props(&self) -> &Option<Properties>
Optional MQTT v5.0 properties associated with this PUBREC 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.
Properties can only be included if a reason code is also present.
Source§impl<PacketIdType> GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubrec<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 for this PUBREC packet.
The packet identifier must match the packet identifier of the PUBLISH packet being acknowledged. It is used to correlate the PUBREC with the original PUBLISH in the QoS 2 message flow.
§Returns
The packet identifier as the generic PacketIdType
.
§Examples
use mqtt_protocol_core::mqtt;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
assert_eq!(pubrec.packet_id(), 123u16);
Sourcepub fn reason_code(&self) -> Option<PubrecReasonCode>
pub fn reason_code(&self) -> Option<PubrecReasonCode>
Returns the reason code for this PUBREC packet, if present.
The reason code indicates the result of the PUBLISH message processing.
If no reason code is present, it is assumed to be Success
(0x00).
§Returns
Some(PubrecReasonCode)
if a reason code was included in the packetNone
if no reason code was present (implies success)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
// PUBREC without reason code (implies success)
let pubrec1 = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
assert_eq!(pubrec1.reason_code(), None);
// PUBREC with explicit reason code
let pubrec2 = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(456u16)
.reason_code(mqtt::result_code::PubrecReasonCode::NoMatchingSubscribers)
.build()
.unwrap();
assert_eq!(pubrec2.reason_code(), Some(mqtt::result_code::PubrecReasonCode::NoMatchingSubscribers));
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total size of this PUBREC packet in bytes.
This includes the fixed header, remaining length field, and all variable header and payload components.
§Returns
The total packet size in bytes.
§Examples
use mqtt_protocol_core::mqtt;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
let size = pubrec.size();
// Size includes: fixed header (1) + remaining length (1) + packet_id (2) = 4 bytes minimum
assert!(size >= 4);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Converts this PUBREC packet into a vector of I/O slices for efficient writing.
This method provides zero-copy serialization by returning references to the
internal packet data as IoSlice
objects, which can be used with vectored
I/O operations.
§Returns
A vector of IoSlice
objects representing the complete packet data.
§Examples
use mqtt_protocol_core::mqtt;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
let buffers = pubrec.to_buffers();
// Use buffers for vectored I/O operations
Sourcepub fn to_continuous_buffer(&self) -> Vec<u8> ⓘ
pub fn to_continuous_buffer(&self) -> Vec<u8> ⓘ
Converts the PUBREC packet into a continuous buffer for no-std environments.
This method serializes the entire packet into a single contiguous byte vector,
which is suitable for no-std environments where IoSlice
is not available.
The resulting buffer contains the complete MQTT v5.0 PUBREC packet ready
for transmission over a network connection.
§Returns
A Vec<u8>
containing the complete packet data in MQTT wire format:
- Fixed header (1 byte): Packet type and flags
- Remaining length (1-4 bytes): Variable length encoding
- Packet identifier (2 bytes): Matching the PUBLISH packet
- Reason code (1 byte): Optional, if present
- Property length (1-4 bytes): Optional, if properties present
- Properties: Optional MQTT v5.0 properties
§Performance
This method allocates a new vector and copies packet data into it. For
zero-copy operations in std environments, use to_buffers()
instead.
§Examples
use mqtt_protocol_core::mqtt;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
let buffer = pubrec.to_continuous_buffer();
// Use buffer for writing to network streams
// stream.write_all(&buffer).await?;
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parses a PUBREC packet from raw byte data.
This method parses the variable header and properties of a PUBREC packet from the provided byte slice. The fixed header should have already been parsed and removed from the data.
§Parameters
data
- The raw packet data excluding the fixed header
§Returns
Ok((GenericPubrec, usize))
- The parsed PUBREC packet and number of bytes consumedErr(MqttError)
- If the packet data is malformed or invalid
§Errors
Returns MqttError::MalformedPacket
if:
- The packet identifier is zero (invalid per MQTT specification)
- The reason code is invalid
- The packet structure is malformed
Returns MqttError::ProtocolError
if:
- Invalid properties are present
- Multiple ReasonString properties are included
§Examples
use mqtt_protocol_core::mqtt;
// Assume `packet_data` contains valid PUBREC packet bytes
let packet_data = &[0x00, 0x01]; // packet_id = 1
let (pubrec, consumed) = mqtt::packet::v5_0::Pubrec::parse(packet_data).unwrap();
assert_eq!(pubrec.packet_id(), 1u16);
assert_eq!(consumed, 2);
Trait Implementations§
Source§impl<PacketIdType> Clone for GenericPubrec<PacketIdType>
impl<PacketIdType> Clone for GenericPubrec<PacketIdType>
Source§fn clone(&self) -> GenericPubrec<PacketIdType>
fn clone(&self) -> GenericPubrec<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 GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Debug
trait for PUBREC packet debugging.
impl<PacketIdType> Debug for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Debug
trait for PUBREC packet debugging.
This implementation delegates to the Display
implementation to provide
consistent formatting for debug output. The JSON format makes it easy to
inspect packet contents during development and debugging.
§Examples
use mqtt_protocol_core::mqtt;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
println!("{:?}", pubrec);
// Output: {"type":"pubrec","packet_id":123}
Source§impl<PacketIdType> Display for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Display
trait for human-readable PUBREC packet representation.
impl<PacketIdType> Display for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Display
trait for human-readable PUBREC packet representation.
This implementation formats the PUBREC packet as a JSON string for easy reading
and debugging. It leverages the Serialize
implementation to generate consistent
output across different display contexts.
§Output Format
The output is a JSON object containing all relevant packet information:
- Packet type and identifier
- Reason code (if present)
- Properties (if present)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(42u16)
.reason_code(mqtt::result_code::PubrecReasonCode::Success)
.build()
.unwrap();
println!("{}", pubrec);
// Output: {"type":"pubrec","packet_id":42,"reason_code":"Success"}
Source§impl<PacketIdType> From<GenericPubrec<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<GenericPubrec<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: GenericPubrec<PacketIdType>) -> GenericPacket<PacketIdType>
fn from(v: GenericPubrec<PacketIdType>) -> GenericPacket<PacketIdType>
Source§impl<PacketIdType> GenericPacketDisplay for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of GenericPacketDisplay
for PUBREC packets.
impl<PacketIdType> GenericPacketDisplay for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of GenericPacketDisplay
for PUBREC packets.
This trait provides consistent display and debug formatting for PUBREC packets
when used in generic packet contexts. It delegates to the standard Display
and Debug
trait implementations.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
// Use in generic packet display contexts
println!("{}", pubrec); // Uses fmt_display
println!("{:?}", pubrec); // Uses fmt_debug
Source§impl<PacketIdType> GenericPacketTrait for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
Implementation of GenericPacketTrait
for PUBREC packets.
impl<PacketIdType> GenericPacketTrait for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
Implementation of GenericPacketTrait
for PUBREC packets.
This trait provides common packet functionality such as size calculation and buffer serialization. It allows PUBREC packets to be treated uniformly with other MQTT packet types in generic contexts.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.build()
.unwrap();
// Use trait methods
let size = pubrec.size();
let buffers = pubrec.to_buffers();
Source§impl<PacketIdType> PacketKind for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> PacketKind for GenericPubrec<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 GenericPubrec<PacketIdType>
impl<PacketIdType> PartialEq for GenericPubrec<PacketIdType>
Source§impl<PacketIdType> Serialize for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Serialize
trait for JSON serialization of PUBREC packets.
impl<PacketIdType> Serialize for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Implementation of Serialize
trait for JSON serialization of PUBREC packets.
This implementation serializes a PUBREC packet into a JSON object containing the packet type, packet identifier, optional reason code, and optional properties. The serialization format is suitable for debugging, logging, and API responses.
§Serialized Fields
type
: Always “pubrec” (string)packet_id
: The packet identifier (number)reason_code
: The reason code (optional, only if present)props
: The properties (optional, only if present)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrec = mqtt::packet::v5_0::Pubrec::builder()
.packet_id(123u16)
.reason_code(mqtt::result_code::PubrecReasonCode::Success)
.build()
.unwrap();
let json = serde_json::to_string(&pubrec).unwrap();
// JSON: {"type":"pubrec","packet_id":123,"reason_code":"Success"}
Source§impl<PacketIdType> TryInto<GenericPubrec<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<GenericPubrec<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> Eq for GenericPubrec<PacketIdType>
impl<PacketIdType> SendableRole<Any> for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Client> for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Server> for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> StructuralPartialEq for GenericPubrec<PacketIdType>where
PacketIdType: IsPacketId,
Auto Trait Implementations§
impl<PacketIdType> Freeze for GenericPubrec<PacketIdType>
impl<PacketIdType> RefUnwindSafe for GenericPubrec<PacketIdType>
impl<PacketIdType> Send for GenericPubrec<PacketIdType>
impl<PacketIdType> Sync for GenericPubrec<PacketIdType>
impl<PacketIdType> Unpin for GenericPubrec<PacketIdType>
impl<PacketIdType> UnwindSafe for GenericPubrec<PacketIdType>
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
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.