pub struct GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,{
pub props: Properties,
/* private fields */
}
Expand description
MQTT 5.0 UNSUBACK packet representation with generic packet ID support
The UNSUBACK packet is sent by the MQTT server (broker) in response to an UNSUBSCRIBE packet from a client. It indicates the result of each unsubscription request and confirms that the client has been unsubscribed from the specified topic filters.
According to MQTT 5.0 specification, the UNSUBACK packet contains:
- Fixed header with packet type and remaining length
- Variable header with packet identifier, properties, and reason codes
- No payload
§Packet Structure
UNSUBACK Packet Structure:
+----------------+
| Fixed Header | - Packet type (0xB0) and remaining length
+----------------+
| Packet ID | - 2 bytes (or PacketIdType::Buffer size)
+----------------+
| Properties | - Property length + properties
+----------------+
| Reason Codes | - One or more reason codes (1 byte each)
+----------------+
§Reason Codes
Each reason code in the UNSUBACK packet corresponds to a topic filter in the original UNSUBSCRIBE packet and indicates the result of the unsubscription request:
Success codes:
0x00
Success - The unsubscription was successful0x11
No subscription existed - No matching subscription found
Error codes:
0x80
Unspecified error - An unspecified error occurred0x83
Implementation specific error - Server implementation specific error0x87
Not authorized - The client is not authorized to unsubscribe0x8F
Topic filter invalid - The topic filter is malformed0x91
Packet identifier in use - The packet identifier is already in use
§Properties
MQTT 5.0 UNSUBACK packets can include the following properties:
- Reason String: Human readable string for diagnostic purposes
- User Properties: Application-specific name-value pairs
Only one Reason String property is allowed per packet.
§Generic Packet ID Support
This implementation supports generic packet ID types through the PacketIdType
parameter.
While MQTT specification uses 16-bit packet IDs, this allows for extended packet IDs
(e.g., 32-bit) for broker cluster implementations to prevent packet ID exhaustion.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
// Create an UNSUBACK with successful unsubscriptions
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![
UnsubackReasonCode::Success,
UnsubackReasonCode::NoSubscriptionExisted,
])
.build()
.unwrap();
assert_eq!(unsuback.packet_id(), 42);
assert_eq!(unsuback.reason_codes().len(), 2);
// Create UNSUBACK with mixed success and error codes
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(100u16)
.reason_codes(vec![
UnsubackReasonCode::Success,
UnsubackReasonCode::NotAuthorized,
UnsubackReasonCode::TopicFilterInvalid,
])
.build()
.unwrap();
// Add properties for diagnostics
let mut props = mqtt::packet::Properties::new();
// props.add(mqtt::packet::Property::ReasonString("Partial success".to_string()));
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(200u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.props(props)
.build()
.unwrap();
// Serialize to bytes for network transmission
let buffers = unsuback.to_buffers();
Fields§
§props: Properties
MQTT 5.0 properties for the UNSUBACK packet
Contains optional properties like reason string and user properties. The properties are validated to ensure only allowed properties are included.
Implementations§
Source§impl<PacketIdType> GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn props(&self) -> &Properties
pub fn props(&self) -> &Properties
MQTT 5.0 properties for the UNSUBACK packet
Contains optional properties like reason string and user properties. The properties are validated to ensure only allowed properties are included.
Source§impl<PacketIdType> GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
Sourcepub fn builder() -> GenericUnsubackBuilder<PacketIdType>
pub fn builder() -> GenericUnsubackBuilder<PacketIdType>
Create a new GenericUnsubackBuilder for constructing UNSUBACK packets
Returns a builder instance that allows setting the various fields of an UNSUBACK packet in a fluent interface style. The builder ensures all required fields are set before creating the final packet.
§Returns
A new GenericUnsubackBuilder<PacketIdType>
instance
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::GenericUnsuback::<u16>::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
Sourcepub fn packet_type() -> PacketType
pub fn packet_type() -> PacketType
Get the packet type for UNSUBACK packets
Returns the constant packet type identifier for UNSUBACK packets.
This is always PacketType::Unsuback
for UNSUBACK packets.
§Returns
The packet type PacketType::Unsuback
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::packet_type::PacketType;
assert_eq!(mqtt::packet::v5_0::Unsuback::packet_type(), PacketType::Unsuback);
Sourcepub fn packet_id(&self) -> PacketIdType
pub fn packet_id(&self) -> PacketIdType
Get the packet identifier from the UNSUBACK packet
Returns the packet identifier that matches the UNSUBSCRIBE packet this UNSUBACK is responding to. The packet identifier is used to correlate the UNSUBACK with the original UNSUBSCRIBE request.
§Returns
The packet identifier as PacketIdType
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(1234u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
assert_eq!(unsuback.packet_id(), 1234);
Sourcepub fn reason_codes(&self) -> Vec<UnsubackReasonCode>
pub fn reason_codes(&self) -> Vec<UnsubackReasonCode>
Get the reason codes from the UNSUBACK packet
Returns a vector of reason codes indicating the result of each unsubscription request in the original UNSUBSCRIBE packet. Each reason code corresponds to a topic filter in the UNSUBSCRIBE packet, in the same order.
Invalid reason code bytes are converted to UnsubackReasonCode::UnspecifiedError
to maintain packet integrity.
§Returns
A Vec<UnsubackReasonCode>
containing the unsubscription results
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![
UnsubackReasonCode::Success,
UnsubackReasonCode::NotAuthorized,
UnsubackReasonCode::NoSubscriptionExisted,
])
.build()
.unwrap();
let codes = unsuback.reason_codes();
assert_eq!(codes.len(), 3);
assert_eq!(codes[0], UnsubackReasonCode::Success);
assert_eq!(codes[1], UnsubackReasonCode::NotAuthorized);
assert_eq!(codes[2], UnsubackReasonCode::NoSubscriptionExisted);
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parse an UNSUBACK packet from raw bytes
Parses the variable header and payload of an UNSUBACK packet from the provided byte buffer. The fixed header should already be parsed before calling this method.
§Parameters
data
- The raw bytes containing the UNSUBACK packet variable header and payload
§Returns
Returns a tuple containing:
- The parsed
GenericUnsuback
instance - The number of bytes consumed during parsing
§Errors
Returns MqttError
if:
- The packet is malformed (insufficient bytes, invalid packet ID, invalid reason codes)
- The packet violates protocol rules (no reason codes provided)
- Properties contain invalid or disallowed property types
- Multiple Reason String properties are present
§Examples
use mqtt_protocol_core::mqtt;
// Parse UNSUBACK packet from network data
let data = &[0x00, 0x10, 0x00, 0x00]; // packet_id=16, no properties, success code
let (unsuback, consumed) = mqtt::packet::v5_0::Unsuback::parse(data).unwrap();
assert_eq!(unsuback.packet_id(), 16);
assert_eq!(consumed, 4);
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Calculate the total size of the UNSUBACK packet in bytes
Returns the total number of bytes required to represent this UNSUBACK packet when serialized for network transmission. This includes the fixed header, variable header, and all reason codes.
§Returns
The total packet size in bytes
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
let size = unsuback.size();
assert!(size > 0);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Create IoSlice buffers for efficient network I/O
Returns a vector of IoSlice
objects that can be used for vectored I/O
operations, allowing zero-copy writes to network sockets. The buffers
represent the complete UNSUBACK packet in wire format.
§Returns
A vector of IoSlice
objects for vectored I/O operations
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
let buffers = unsuback.to_buffers();
// Use with vectored write: socket.write_vectored(&buffers)?;
Sourcepub fn to_continuous_buffer(&self) -> Vec<u8> ⓘ
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 provides an alternative to to_buffers()
for no-std environments
where vectored I/O is not available.
The returned buffer contains the complete UNSUBACK packet serialized according to the MQTT v5.0 protocol specification, including fixed header, remaining length, packet identifier, properties, and reason codes.
§Returns
A vector containing the complete packet data
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
let buffer = unsuback.to_continuous_buffer();
// buffer contains all packet bytes
Trait Implementations§
Source§impl<PacketIdType> Clone for GenericUnsuback<PacketIdType>
impl<PacketIdType> Clone for GenericUnsuback<PacketIdType>
Source§fn clone(&self) -> GenericUnsuback<PacketIdType>
fn clone(&self) -> GenericUnsuback<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 GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug trait implementation for GenericUnsuback
impl<PacketIdType> Debug for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Debug trait implementation for GenericUnsuback
Provides a debug representation of the UNSUBACK packet using the same JSON format as the Display trait. This ensures consistent output for logging and debugging.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
println!("{:?}", unsuback); // Prints JSON representation
Source§impl<PacketIdType> Display for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display trait implementation for GenericUnsuback
impl<PacketIdType> Display for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Display trait implementation for GenericUnsuback
Provides a human-readable JSON representation of the UNSUBACK packet. The display format includes the packet type, packet ID, properties (if any), and reason codes.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
println!("{}", unsuback); // Prints JSON representation
Source§impl<PacketIdType> From<GenericUnsuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<GenericUnsuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: GenericUnsuback<PacketIdType>) -> GenericPacket<PacketIdType>
fn from(v: GenericUnsuback<PacketIdType>) -> GenericPacket<PacketIdType>
Source§impl<PacketIdType> GenericPacketDisplay for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
GenericPacketDisplay implementation for GenericUnsuback
impl<PacketIdType> GenericPacketDisplay for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
GenericPacketDisplay implementation for GenericUnsuback
Provides standardized display formatting for UNSUBACK packets through the GenericPacketDisplay trait. This allows consistent formatting across different packet types in the library.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketDisplay;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
// Use trait methods for consistent formatting
println!("{}", format_args!("{}", unsuback));
Source§impl<PacketIdType> GenericPacketTrait for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
GenericPacketTrait implementation for GenericUnsuback
impl<PacketIdType> GenericPacketTrait for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
GenericPacketTrait implementation for GenericUnsuback
Provides the standard packet interface methods for UNSUBACK packets. This trait allows UNSUBACK packets to be used polymorphically with other MQTT packet types.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
// Use trait methods
let size = unsuback.size();
let buffers = unsuback.to_buffers();
Source§impl<PacketIdType> PacketKind for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> PacketKind for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
Source§const IS_UNSUBACK: bool = true
const IS_UNSUBACK: bool = true
true
if this is an UNSUBACK 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_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_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 GenericUnsuback<PacketIdType>
impl<PacketIdType> PartialEq for GenericUnsuback<PacketIdType>
Source§fn eq(&self, other: &GenericUnsuback<PacketIdType>) -> bool
fn eq(&self, other: &GenericUnsuback<PacketIdType>) -> bool
self
and other
values to be equal, and is used by ==
.Source§impl<PacketIdType> Serialize for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize trait implementation for GenericUnsuback
impl<PacketIdType> Serialize for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Serialize trait implementation for GenericUnsuback
Provides JSON serialization support for UNSUBACK packets. The serialized format includes the packet type, packet ID, properties (if any), and reason codes.
The serialized structure contains:
type
: Always “unsuback”packet_id
: The packet identifierprops
: Properties object (only if non-empty)reason_codes
: Array of reason codes (only if non-empty)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::UnsubackReasonCode;
let unsuback = mqtt::packet::v5_0::Unsuback::builder()
.packet_id(42u16)
.reason_codes(vec![UnsubackReasonCode::Success])
.build()
.unwrap();
let json = serde_json::to_string(&unsuback).unwrap();
// json contains: {"type":"unsuback","packet_id":42,"reason_codes":["Success"]}
Source§impl<PacketIdType> TryInto<GenericUnsuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<GenericUnsuback<PacketIdType>> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> Eq for GenericUnsuback<PacketIdType>
impl<PacketIdType> SendableRole<Any> for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> SendableRole<Server> for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
impl<PacketIdType> StructuralPartialEq for GenericUnsuback<PacketIdType>where
PacketIdType: IsPacketId,
Auto Trait Implementations§
impl<PacketIdType> Freeze for GenericUnsuback<PacketIdType>
impl<PacketIdType> RefUnwindSafe for GenericUnsuback<PacketIdType>
impl<PacketIdType> Send for GenericUnsuback<PacketIdType>
impl<PacketIdType> Sync for GenericUnsuback<PacketIdType>
impl<PacketIdType> Unpin for GenericUnsuback<PacketIdType>
impl<PacketIdType> UnwindSafe for GenericUnsuback<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.