Struct SubEntry

Source
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 or sensors/#

§Wire Format

In the MQTT wire protocol, each subscription entry is encoded as:

  1. Topic filter as an MQTT string (2-byte length + UTF-8 bytes)
  2. 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

Source

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 entry
  • Err(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();
Source

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");
Source

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);
Source

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 successfully
  • Err(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");
Source

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);
Source

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:

  1. Topic filter with length prefix
  2. 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)?;
Source

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);
Source

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 consumed
  • Err(MqttError::MalformedPacket) - If the data is malformed or incomplete
§Wire Format
  1. Topic filter as MQTT string (2-byte length + UTF-8 bytes)
  2. 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 Clone for SubEntry

Source§

fn clone(&self) -> SubEntry

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SubEntry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for SubEntry

Source§

fn cmp(&self, other: &SubEntry) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SubEntry

Source§

fn eq(&self, other: &SubEntry) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SubEntry

Source§

fn partial_cmp(&self, other: &SubEntry) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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 string
  • options: The subscription options as a structured object
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for SubEntry

Source§

impl StructuralPartialEq for SubEntry

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsConcrete<T> for T

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoConcreteOwned<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more