pub struct SubEntry { /* private fields */ }
Expand description
MQTT Subscription Entry
Represents a single subscription entry consisting of a topic filter and subscription options. This structure is used in SUBSCRIBE packets to specify what topics to subscribe to and how messages should be handled.
Each subscription entry contains:
- A topic filter string that may include wildcards (
+
and#
) - Subscription options that control message delivery behavior
§Topic Filter Format
Topic filters follow MQTT specification rules:
- Single-level wildcard:
+
matches any single level - Multi-level wildcard:
#
matches any number of levels (must be last) - Example:
home/+/temperature
orsensors/#
§Wire Format
In the MQTT wire protocol, each subscription entry is encoded as:
- Topic filter as an MQTT string (2-byte length + UTF-8 bytes)
- Subscription options as a single byte
§Examples
use mqtt_protocol_core::mqtt;
// Basic subscription with default options
let entry = mqtt::packet::SubEntry::new(
"sensors/temperature",
mqtt::packet::SubOpts::new()
).unwrap();
// Subscription with custom options
let opts = mqtt::packet::SubOpts::new()
.set_qos(mqtt::packet::Qos::AtLeastOnce)
.set_nl(true);
let entry = mqtt::packet::SubEntry::new("home/+/status", opts).unwrap();
Implementations§
Source§impl SubEntry
impl SubEntry
Sourcepub fn new(
topic_filter: impl AsRef<str>,
sub_opts: SubOpts,
) -> Result<Self, MqttError>
pub fn new( topic_filter: impl AsRef<str>, sub_opts: SubOpts, ) -> Result<Self, MqttError>
Create a new subscription entry
Creates a SubEntry
with the specified topic filter and subscription options.
The topic filter is validated to ensure it’s a valid UTF-8 string and within
the MQTT size limits (maximum 65,535 bytes).
§Parameters
topic_filter
- The topic filter string (may contain wildcards + and #)sub_opts
- The subscription options controlling message delivery
§Returns
Ok(SubEntry)
- Successfully created subscription entryErr(MqttError::MalformedPacket)
- If topic filter exceeds maximum length
§Examples
use mqtt_protocol_core::mqtt;
// Basic subscription
let entry = mqtt::packet::SubEntry::new(
"sensors/temperature",
mqtt::packet::SubOpts::new()
).unwrap();
// Subscription with wildcards and custom options
let opts = mqtt::packet::SubOpts::new().set_qos(mqtt::packet::Qos::AtLeastOnce);
let entry = mqtt::packet::SubEntry::new("home/+/status", opts).unwrap();
Sourcepub fn topic_filter(&self) -> &str
pub fn topic_filter(&self) -> &str
Get the topic filter as a string slice
Returns the topic filter string for this subscription entry.
The topic filter may contain MQTT wildcards (+
for single level,
#
for multiple levels).
§Returns
A string slice containing the topic filter
§Examples
use mqtt_protocol_core::mqtt;
let entry = mqtt::packet::SubEntry::new("sensors/+/temperature",
mqtt::packet::SubOpts::new()).unwrap();
assert_eq!(entry.topic_filter(), "sensors/+/temperature");
Sourcepub fn sub_opts(&self) -> &SubOpts
pub fn sub_opts(&self) -> &SubOpts
Get the subscription options
Returns a reference to the subscription options that control how messages matching this topic filter should be delivered.
§Returns
A reference to the SubOpts
containing the subscription options
§Examples
use mqtt_protocol_core::mqtt;
let opts = mqtt::packet::SubOpts::new().set_qos(mqtt::packet::Qos::AtLeastOnce);
let entry = mqtt::packet::SubEntry::new("test/topic", opts).unwrap();
assert_eq!(entry.sub_opts().qos(), mqtt::packet::Qos::AtLeastOnce);
Sourcepub fn set_topic_filter(
&mut self,
topic_filter: String,
) -> Result<(), MqttError>
pub fn set_topic_filter( &mut self, topic_filter: String, ) -> Result<(), MqttError>
Set the topic filter for this subscription entry
Updates the topic filter with a new value. The new topic filter is validated to ensure it’s valid UTF-8 and within size limits.
§Parameters
topic_filter
- The new topic filter string
§Returns
Ok(())
- Topic filter updated successfullyErr(MqttError::MalformedPacket)
- If topic filter exceeds maximum length
§Examples
use mqtt_protocol_core::mqtt;
let mut entry = mqtt::packet::SubEntry::new("old/topic",
mqtt::packet::SubOpts::new()).unwrap();
entry.set_topic_filter("new/topic".to_string()).unwrap();
assert_eq!(entry.topic_filter(), "new/topic");
Sourcepub fn set_sub_opts(&mut self, sub_opts: SubOpts)
pub fn set_sub_opts(&mut self, sub_opts: SubOpts)
Set the subscription options for this entry
Updates the subscription options that control how messages matching this topic filter should be delivered.
§Parameters
sub_opts
- The new subscription options
§Examples
use mqtt_protocol_core::mqtt;
let mut entry = mqtt::packet::SubEntry::new("test/topic",
mqtt::packet::SubOpts::new()).unwrap();
let new_opts = mqtt::packet::SubOpts::new().set_qos(mqtt::packet::Qos::ExactlyOnce);
entry.set_sub_opts(new_opts);
assert_eq!(entry.sub_opts().qos(), mqtt::packet::Qos::ExactlyOnce);
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
contain the complete wire format representation of this subscription entry.
§Returns
A vector of IoSlice
buffers containing:
- Topic filter with length prefix
- Subscription options byte
§Examples
use mqtt_protocol_core::mqtt;
let entry = mqtt::packet::SubEntry::new("test/topic",
mqtt::packet::SubOpts::new()).unwrap();
let buffers = entry.to_buffers();
// Can be used with vectored write operations
// 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 entry data
Returns a vector containing all subscription entry bytes in a single continuous buffer.
This method is compatible with no-std environments and provides an alternative
to to_buffers()
when vectored I/O is not needed.
The returned buffer contains:
- Topic filter with length prefix
- Subscription options byte
§Returns
A vector containing the complete entry data
§Examples
use mqtt_protocol_core::mqtt;
let entry = mqtt::packet::SubEntry::new("test/topic",
mqtt::packet::SubOpts::new()).unwrap();
let buffer = entry.to_continuous_buffer();
// buffer contains all subscription entry bytes
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the total encoded size of this subscription entry
Returns the number of bytes this subscription entry will occupy in the MQTT wire format, including the topic filter with its length prefix and the subscription options byte.
§Returns
The total size in bytes for the wire format representation
§Examples
use mqtt_protocol_core::mqtt;
let entry = mqtt::packet::SubEntry::new("test",
mqtt::packet::SubOpts::new()).unwrap();
// Size = 2 bytes (length prefix) + 4 bytes ("test") + 1 byte (options) = 7 bytes
assert_eq!(entry.size(), 7);
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parse a subscription entry from byte data
Decodes a subscription entry from the MQTT wire format, which consists of a topic filter (MQTT string with length prefix) followed by a subscription options byte.
§Parameters
data
- Byte buffer containing the encoded subscription entry
§Returns
Ok((SubEntry, bytes_consumed))
- Successfully parsed entry and number of bytes consumedErr(MqttError::MalformedPacket)
- If the data is malformed or incomplete
§Wire Format
- Topic filter as MQTT string (2-byte length + UTF-8 bytes)
- Subscription options as single byte
§Examples
use mqtt_protocol_core::mqtt;
// Buffer containing: length=4, "test", options=0x01
let buffer = &[0x00, 0x04, b't', b'e', b's', b't', 0x01];
let (entry, consumed) = mqtt::packet::SubEntry::parse(buffer).unwrap();
assert_eq!(entry.topic_filter(), "test");
assert_eq!(entry.sub_opts().qos(), mqtt::packet::Qos::AtLeastOnce);
assert_eq!(consumed, 7);
Trait Implementations§
Source§impl Default for SubEntry
Implementation of Default
for SubEntry
impl Default for SubEntry
Implementation of Default
for SubEntry
Creates a subscription entry with default values:
- Empty topic filter string
- Default subscription options (QoS 0, all flags false)
Note: An empty topic filter is not valid for actual MQTT usage but provides a default state for initialization purposes.
Source§impl Display for SubEntry
Implementation of Display
for SubEntry
impl Display for SubEntry
Implementation of Display
for SubEntry
Formats the subscription entry as a JSON string for human-readable output. This is particularly useful for logging and debugging purposes. If JSON serialization fails, an error message is displayed instead.
Source§impl Ord for SubEntry
impl Ord for SubEntry
Source§impl PartialOrd for SubEntry
impl PartialOrd for SubEntry
Source§impl Serialize for SubEntry
Implementation of Serialize
for SubEntry
impl Serialize for SubEntry
Implementation of Serialize
for SubEntry
Serializes the subscription entry to a structured format with separate fields for the topic filter and subscription options. This provides a clear JSON representation suitable for debugging and logging.
§Serialized Fields
topic_filter
: The topic filter stringoptions
: The subscription options as a structured object
impl Eq for SubEntry
impl StructuralPartialEq for SubEntry
Auto Trait Implementations§
impl Freeze for SubEntry
impl RefUnwindSafe for SubEntry
impl Send for SubEntry
impl Sync for SubEntry
impl Unpin for SubEntry
impl UnwindSafe for SubEntry
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> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
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.