pub struct GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,{
pub props: Option<Properties>,
/* private fields */
}
Expand description
A PUBREL packet for MQTT v5.0 protocol.
The PUBREL packet is the third packet in the QoS 2 PUBLISH message exchange sequence. It is sent in response to a PUBREC packet and triggers the final PUBCOMP response. This packet instructs the receiver to release any stored state for the specified packet identifier and complete the QoS 2 delivery sequence.
§MQTT v5.0 Specification
According to the MQTT v5.0 specification, the PUBREL packet:
- Is sent by the sender of a PUBLISH packet with QoS 2 in response to a PUBREC
- Contains the same Packet Identifier as the original PUBLISH and PUBREC packets
- Has a fixed header with packet type 6 (0110) and reserved bits set to 0010
- May optionally include a reason code indicating the result of the release operation
- May optionally include properties for additional metadata
- Must be acknowledged with a PUBCOMP packet
§QoS 2 Message Flow
The PUBREL packet is part of the four-packet QoS 2 handshake:
- PUBLISH (QoS 2) -> 2. PUBREC -> 3. PUBREL -> 4. 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 Pubrel
uses u16
packet identifiers as per MQTT specification.
§Examples
Creating a basic PUBREL packet:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(123u16)
.build()
.unwrap();
assert_eq!(pubrel.packet_id(), 123u16);
Creating a PUBREL with reason code:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(456u16)
.reason_code(mqtt::result_code::PubrelReasonCode::Success)
.build()
.unwrap();
assert_eq!(pubrel.packet_id(), 456u16);
assert_eq!(pubrel.reason_code(), Some(mqtt::result_code::PubrelReasonCode::Success));
Creating a PUBREL with properties:
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::prelude::*;
let props = mqtt::packet::Properties::from(vec![
mqtt::packet::Property::ReasonString("Release successful".to_string())
]);
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(789u16)
.reason_code(mqtt::result_code::PubrelReasonCode::Success)
.props(props)
.build()
.unwrap();
Fields§
§props: Option<Properties>
Optional MQTT v5.0 properties associated with this PUBREL 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> GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn props(&self) -> &Option<Properties>
pub fn props(&self) -> &Option<Properties>
Optional MQTT v5.0 properties associated with this PUBREL 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> GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericPubrel<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 PUBREL packet.
The packet identifier must match the packet identifier of the original PUBLISH and PUBREC packets in the QoS 2 message exchange.
§Returns
The packet identifier as the specified PacketIdType
.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(1337u16)
.build()
.unwrap();
assert_eq!(pubrel.packet_id(), 1337u16);
Sourcepub fn reason_code(&self) -> Option<PubrelReasonCode>
pub fn reason_code(&self) -> Option<PubrelReasonCode>
Returns the reason code of this PUBREL packet, if present.
The reason code indicates the result of the packet identifier release operation.
If no reason code is present, it implies successful processing
(equivalent to PubrelReasonCode::Success
).
Available reason codes:
Success
: The packet identifier has been released successfullyPacketIdentifierNotFound
: The specified packet identifier was not found
§Returns
An Option<PubrelReasonCode>
containing the reason code if present,
or None
if no reason code was included in the packet.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(123u16)
.reason_code(mqtt::result_code::PubrelReasonCode::Success)
.build()
.unwrap();
assert_eq!(pubrel.reason_code(),
Some(mqtt::result_code::PubrelReasonCode::Success));
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total size of this PUBREL packet in bytes.
This includes the fixed header, variable header (packet identifier, optional reason code), and optional properties.
§Returns
The total packet size in bytes as a usize
.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(1u16)
.build()
.unwrap();
let size = pubrel.size();
// Minimum size: 1 (fixed header) + 1 (remaining length) + 2 (packet id)
assert!(size >= 4);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Converts this PUBREL packet into a vector of I/O slices for efficient network transmission.
This method creates zero-copy I/O slices that can be used with vectored I/O operations for efficient packet transmission over network connections.
§Returns
A Vec<IoSlice<'_>>
containing slices of the packet data ready for transmission.
The slices are ordered according to the MQTT v5.0 packet structure.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(1u16)
.build()
.unwrap();
let buffers = pubrel.to_buffers();
// Can be used with vectored write operations
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parses a PUBREL packet from raw bytes.
This method deserializes a PUBREL packet from its wire format according to the MQTT v5.0 specification. It validates the packet structure and ensures all components are correctly formatted.
§Parameters
data
- The raw byte slice containing the PUBREL packet data (excluding fixed header)
§Returns
A Result
containing:
Ok((GenericPubrel<PacketIdType>, usize))
- The parsed packet and number of bytes consumedErr(MqttError)
- Parsing error if the packet is malformed
§Errors
Returns MqttError::MalformedPacket
if:
- The packet identifier is zero (invalid)
- The reason code is invalid
- The property format is incorrect
- The packet is truncated or malformed
§Examples
use mqtt_protocol_core::mqtt;
let packet_data = &[0x00, 0x01]; // Packet ID = 1
match mqtt::packet::v5_0::Pubrel::parse(packet_data) {
Ok((pubrel, consumed)) => {
assert_eq!(pubrel.packet_id(), 1u16);
assert_eq!(consumed, 2);
}
Err(e) => panic!("Parse error: {:?}", e),
}
Trait Implementations§
Source§impl AsConcrete<GenericPubrel<u16>> for Packet
impl AsConcrete<GenericPubrel<u16>> for Packet
fn as_concrete(&self) -> Option<&Pubrel>
Source§impl<PacketIdType> Clone for GenericPubrel<PacketIdType>
impl<PacketIdType> Clone for GenericPubrel<PacketIdType>
Source§fn clone(&self) -> GenericPubrel<PacketIdType>
fn clone(&self) -> GenericPubrel<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 GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug trait implementation for PUBREL packets.
impl<PacketIdType> Debug for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug trait implementation for PUBREL packets.
This implementation provides debug formatting that delegates to the Display implementation, ensuring consistent output format for both debugging and display purposes.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(123u16)
.build()
.unwrap();
println!("{:?}", pubrel);
// Output: {"type":"pubrel","packet_id":123}
Source§impl<PacketIdType> Display for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display trait implementation for PUBREL packets.
impl<PacketIdType> Display for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display trait implementation for PUBREL packets.
This implementation provides a human-readable string representation of the PUBREL packet in JSON format. This is useful for debugging, logging, and diagnostic purposes.
§Format
The output format is a JSON string containing:
type
: The packet type (“pubrel”)packet_id
: The packet identifierreason_code
: The reason code (if present)props
: The properties (if present)
If serialization fails, an error message is returned in JSON format.
§Examples
use mqtt_protocol_core::mqtt;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(42u16)
.build()
.unwrap();
println!("{}", pubrel);
// Output: {"type":"pubrel","packet_id":42}
Source§impl<PacketIdType> From<GenericPubrel<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<GenericPubrel<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: GenericPubrel<PacketIdType>) -> GenericPacket<PacketIdType>
fn from(v: GenericPubrel<PacketIdType>) -> GenericPacket<PacketIdType>
Source§impl<PacketIdType> GenericPacketDisplay for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Generic packet display trait implementation for PUBREL packets.
impl<PacketIdType> GenericPacketDisplay for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Generic packet display trait implementation for PUBREL packets.
This implementation provides generic display formatting capabilities that can be used by the MQTT protocol library framework for consistent packet representation across different display contexts.
§Methods
fmt_debug()
: Provides debug formatting via the Debug traitfmt_display()
: Provides display formatting via the Display trait
Source§impl<PacketIdType> GenericPacketTrait for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
Generic packet trait implementation for PUBREL packets.
impl<PacketIdType> GenericPacketTrait for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
Generic packet trait implementation for PUBREL packets.
This implementation provides the generic packet interface required by the MQTT protocol library framework. It enables PUBREL packets to be used polymorphically with other packet types in the system.
§Methods
size()
: Returns the total packet size in bytesto_buffers()
: Returns I/O slices for efficient transmission
Source§impl IntoConcreteOwned<GenericPubrel<u16>> for Packet
impl IntoConcreteOwned<GenericPubrel<u16>> for Packet
fn into_concrete_owned(self) -> Option<Pubrel>
Source§impl<PacketIdType> PacketKind for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> PacketKind for GenericPubrel<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 GenericPubrel<PacketIdType>
impl<PacketIdType> PartialEq for GenericPubrel<PacketIdType>
Source§impl<PacketIdType> Serialize for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize implementation for PUBREL packets.
impl<PacketIdType> Serialize for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize implementation for PUBREL packets.
This implementation allows PUBREL packets to be serialized into various formats (JSON, YAML, etc.) using the serde framework. The serialization includes all relevant packet fields in a structured format.
§Serialized Fields
type
: Always “pubrel” to identify the packet typepacket_id
: The packet identifier valuereason_code
: The reason code (if present)props
: The properties (if present)
§Examples
use mqtt_protocol_core::mqtt;
use serde_json;
let pubrel = mqtt::packet::v5_0::Pubrel::builder()
.packet_id(123u16)
.reason_code(mqtt::result_code::PubrelReasonCode::Success)
.build()
.unwrap();
let json = serde_json::to_string(&pubrel).unwrap();
// Result: {"type":"pubrel","packet_id":123,"reason_code":"Success"}
Source§impl<PacketIdType> TryFrom<GenericPubrel<PacketIdType>> for GenericStorePacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryFrom<GenericPubrel<PacketIdType>> for GenericStorePacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§impl<PacketIdType> TryInto<GenericPubrel<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<GenericPubrel<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> Eq for GenericPubrel<PacketIdType>
impl<PacketIdType> SendableRole<Any> for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Client> for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Server> for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> StructuralPartialEq for GenericPubrel<PacketIdType>where
PacketIdType: IsPacketId,
Auto Trait Implementations§
impl<PacketIdType> Freeze for GenericPubrel<PacketIdType>
impl<PacketIdType> RefUnwindSafe for GenericPubrel<PacketIdType>
impl<PacketIdType> Send for GenericPubrel<PacketIdType>
impl<PacketIdType> Sync for GenericPubrel<PacketIdType>
impl<PacketIdType> Unpin for GenericPubrel<PacketIdType>
impl<PacketIdType> UnwindSafe for GenericPubrel<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.