Struct SubscriptionBuilder

Source
pub struct SubscriptionBuilder<MessageType, F>
where F: Clone,
{ /* private fields */ }
Expand description

Immutable builder for configuring MQTT subscriptions

Implementations§

Source§

impl<MessageType, F> SubscriptionBuilder<MessageType, F>
where F: Clone,

Source

pub fn new( client: MqttClient<F>, default_pattern: TopicPatternPath, ) -> SubscriptionBuilder<MessageType, F>

Create new builder with default pattern

Source

pub fn with_cache(self, capacity: usize) -> SubscriptionBuilder<MessageType, F>

Enable LRU cache for topic parsing optimization.

When the same MQTT topic paths are received repeatedly, caching the parsing results can significantly improve performance by avoiding redundant topic pattern matching.

§Parameters
  • capacity - Maximum number of topic parsing results to cache (LRU eviction) Set to 0 to disable caching entirely
§Performance Impact
  • Memory: ~50-200 bytes per cached topic (depending on topic complexity)
  • CPU: Reduces topic parsing overhead for repeated patterns
  • Use case: Most beneficial when same topics repeat frequently
§Examples
// Cache last 100 topic parsing results
let subscriber = client.my_topic()
    .subscription()
    .with_cache(100)
    .subscribe().await?;

// Disable caching for memory-constrained environments
let subscriber = client.my_topic()
    .subscription()
    .with_cache(0)
    .subscribe().await?;
Source

pub fn with_qos(self, qos: QoS) -> SubscriptionBuilder<MessageType, F>

Set QoS level

Source

pub fn with_pattern( self, custom_pattern: impl TryInto>, ) -> Result<SubscriptionBuilder<MessageType, F>, MqttClientError>

Override the default topic pattern with a custom one.

This allows using a different MQTT topic pattern than the one defined in the #[mqtt_topic] macro, while ensuring the parameter structure remains compatible. The new pattern must have the same parameter names and types as the original.

§Parameters
  • custom_pattern - New topic pattern string (e.g., “sensors/{location}/data/{sensor_id}”)
§Compatibility Requirements
  • Same parameter names: {location}, {sensor_id}, etc.
  • Same parameter order and count
  • Parameter types must match the struct fields
§Use Cases
  • Environment-specific patterns: Different topic structures for dev/prod
  • Multi-tenant systems: Adding tenant prefixes to topics
  • Legacy compatibility: Supporting old topic formats
  • A/B testing: Different topic patterns for the same data structure
§Examples
// Original pattern from macro: "sensors/{location}/{sensor_id}"
// Override with environment-specific pattern:
let subscriber = client.sensor_topic()
    .subscription()
    .with_pattern("prod/sensors/{location}/device/{sensor_id}")?  // ✅ Compatible
    .subscribe().await?;

// Multi-tenant pattern:
let subscriber = client.sensor_topic()
    .subscription()
    .with_pattern("tenant_123/sensors/{location}/{sensor_id}")?  // ✅ Compatible
    .subscribe().await?;

// ❌ This would fail - different parameter names:
// .with_pattern("sensors/{room}/{device_id}")  // Error: parameter mismatch
§Errors

Returns MqttClientError if:

  • Pattern syntax is invalid
  • Parameter names don’t match the original pattern
  • Parameter count differs from the original pattern
Source

pub async fn subscribe<PayloadType>( self, ) -> Result<MqttTopicSubscriber<MessageType, PayloadType, F>, MqttClientError>
where MessageType: FromMqttMessage<PayloadType, <F as MessageSerializer<PayloadType>>::DeserializeError>, PayloadType: Send + Sync + 'static, F: Default + Clone + Send + Sync + MessageSerializer<PayloadType>,

Subscribe using configured parameters

Trait Implementations§

Source§

impl<MessageType, F> Clone for SubscriptionBuilder<MessageType, F>
where F: Clone,

Source§

fn clone(&self) -> SubscriptionBuilder<MessageType, F>

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<MessageType, F> Debug for SubscriptionBuilder<MessageType, F>
where MessageType: Debug, F: Debug + Clone,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<MessageType, F> !Freeze for SubscriptionBuilder<MessageType, F>

§

impl<MessageType, F> RefUnwindSafe for SubscriptionBuilder<MessageType, F>
where F: RefUnwindSafe, MessageType: RefUnwindSafe,

§

impl<MessageType, F> Send for SubscriptionBuilder<MessageType, F>
where F: Send, MessageType: Send,

§

impl<MessageType, F> Sync for SubscriptionBuilder<MessageType, F>
where F: Sync, MessageType: Sync,

§

impl<MessageType, F> Unpin for SubscriptionBuilder<MessageType, F>
where F: Unpin, MessageType: Unpin,

§

impl<MessageType, F> UnwindSafe for SubscriptionBuilder<MessageType, F>
where F: UnwindSafe, MessageType: UnwindSafe,

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> 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<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> 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, 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,