Skip to main content

State

Trait State 

pub trait State {
    type Error;

Show 22 methods // Required methods fn drain_outbox( &mut self, visit: &mut dyn FnMut(PacketId, OutboxEntry<'_>) -> DrainAction, ); fn drain_pubrel(&mut self, visit: &mut dyn FnMut(PacketId) -> DrainAction); fn first_pending_group(&self) -> Option<PendingMeta>; fn for_each_filter_entry(&self, f: &mut dyn FnMut(FilterEntry<'_>)); fn for_each_incoming_qos2(&self, f: &mut dyn FnMut(PacketId)); fn for_each_outbox_entry( &self, f: &mut dyn FnMut(PacketId, OutboxEntry<'_>), ); fn for_each_pubrel_pending(&self, f: &mut dyn FnMut(PacketId)); fn forget_server_state(&mut self); fn get_filter_entry(&self, filter: &TopicFilter) -> Option<FilterEntry<'_>>; fn has_incoming_qos2(&self, packet_id: PacketId) -> bool; fn has_outbox_entry(&self, packet_id: PacketId) -> bool; fn has_pubrel_pending(&self, packet_id: PacketId) -> bool; fn has_resubscribe(&self) -> bool; fn is_packet_id_in_use(&self, packet_id: PacketId) -> bool; fn mark_subscription_in_flight(&mut self, packet_id: PacketId); fn modify(&mut self, op: Modify<'_>) -> Applied; fn pending_filters( &self, packet_id: PacketId, ) -> impl Iterator<Item = (FilterEntry<'_>, usize)>; fn reset_in_flight(&mut self); fn resolve_pending( &mut self, packet_id: PacketId, decide: &mut dyn FnMut(FilterEntry<'_>, usize) -> Resolution, ); fn start_resubscribe( &mut self, packet_id: PacketId, ) -> Option<FilterEntry<'_>>; fn store(&mut self, op: Store<'_>) -> Result<(), Self::Error>; // Provided method fn apply(&mut self, op: Op<'_>) -> Result<Applied, Self::Error> { ... }
}
Expand description

Persistent session state that is kept across network connections.

This trait follows the repository pattern. Each method is a mechanical storage primitive. All MQTT protocol logic is in Client. The client passes its decisions to the session as DrainAction/Resolution values.

§Contract

Implementors must persist state durably when Session Expiry Interval > 0 (§3.1.2.11.2). When the interval is 0 (or absent), state only needs to live for the duration of the network connection.

Adding a method to this trait is a breaking change by design.

Required Associated Types§

type Error

Error returned when the session has no room for new data.

Required Methods§

fn drain_outbox( &mut self, visit: &mut dyn FnMut(PacketId, OutboxEntry<'_>) -> DrainAction, )

Visits each outbox entry not already in flight on the current connection, in packet-id order, applying the DrainAction the visitor returns. Sent marks it in-flight and was_sent. Discard removes it. Stop halts the walk. This is the only outbound send path, so the visitor is the only place that writes a publish to the wire.

fn drain_pubrel(&mut self, visit: &mut dyn FnMut(PacketId) -> DrainAction)

Visits each PUBREL-pending entry not already in flight on the current connection, applying the returned DrainAction. Mirrors drain_outbox for the QoS 2 PUBREL resend.

fn first_pending_group(&self) -> Option<PendingMeta>

Returns the pending SUBSCRIBE/UNSUBSCRIBE group with the lowest packet id that is not already in flight on the current connection, or None when none remain. This is a read-only peek; the caller iterates the group’s filters with pending_filters, enqueues the packet, then commits the group with mark_subscription_in_flight.

Looping over this is the only send path for subscription state, mirroring drain_outbox for publishes.

fn for_each_filter_entry(&self, f: &mut dyn FnMut(FilterEntry<'_>))

Iterates over every filter entry with its full state, for snapshotting (compaction/persistence).

fn for_each_incoming_qos2(&self, f: &mut dyn FnMut(PacketId))

Iterates over every stored incoming QoS 2 packet identifier.

fn for_each_outbox_entry(&self, f: &mut dyn FnMut(PacketId, OutboxEntry<'_>))

Iterates over every outbox entry.

Used for snapshotting (compaction) and capability re-checks. The send path uses drain_outbox instead.

fn for_each_pubrel_pending(&self, f: &mut dyn FnMut(PacketId))

Iterates over every PUBREL-pending packet identifier.

Used during reconnect to fail awaiting-PUBCOMP futures on session loss and for snapshotting. The resend path uses drain_pubrel.

fn forget_server_state(&mut self)

Discards what the server forgot when it lost the session (§4.1).

Clears the incoming QoS 2 packet identifiers and the was_sent flag of every outbox entry, so resends are first deliveries (DUP=0). Every filter changes as FilterState::after_session_loss describes. PUBREL flows are discarded separately with drain_pubrel. Never allocates.

fn get_filter_entry(&self, filter: &TopicFilter) -> Option<FilterEntry<'_>>

Returns the stored entry for a topic filter, or None if it has not been touched. Used by the client to determine the filter’s current state before recording an UNSUBSCRIBE transition.

fn has_incoming_qos2(&self, packet_id: PacketId) -> bool

Checks whether an incoming QoS 2 packet identifier is stored (i.e., PUBREC was already sent for this packet ID).

fn has_outbox_entry(&self, packet_id: PacketId) -> bool

Returns true if the outbox contains a publish under packet_id.

fn has_pubrel_pending(&self, packet_id: PacketId) -> bool

Returns true if a PUBREL is pending for packet_id (the QoS 2 publish is in the awaiting-PUBCOMP phase).

Lets the client answer a duplicate PUBREC idempotently (re-send PUBREL) instead of treating it as an unknown identifier.

fn has_resubscribe(&self) -> bool

Returns true if a Resubscribing filter waits to be sent again.

fn is_packet_id_in_use(&self, packet_id: PacketId) -> bool

Returns true if packet_id is currently occupied by any tracked operation. This includes outbox entries, PUBREL-pending entries and in-flight SUBSCRIBE/UNSUBSCRIBE requests.

Used by the client to skip identifiers that are already in use during packet ID allocation.

fn mark_subscription_in_flight(&mut self, packet_id: PacketId)

Marks every filter under packet_id in flight, so the next first_pending_group skips the group until reset_in_flight re-arms it. Also records the position of each filter in the packet for resolve_pending. Called once the group’s packet has been enqueued.

fn modify(&mut self, op: Modify<'_>) -> Applied

Changes or removes stored data. Never needs new storage.

Applied reports whether the change took effect. Applied::Absent signals that the op should not be persisted.

fn pending_filters( &self, packet_id: PacketId, ) -> impl Iterator<Item = (FilterEntry<'_>, usize)>

Iterates the filters of the pending request under packet_id, in packet order, each with its position in the packet. Once the request is marked with mark_subscription_in_flight, the position is the one at that time, even if filters have left the request since. It is the position of the filter’s reason code in the SUBACK or UNSUBACK.

fn reset_in_flight(&mut self)

Clears the in-flight marker on every outbox entry, PUBREL-pending entry, and in-flight SUBSCRIBE/UNSUBSCRIBE request.

Called when a connection is established (every connect, resumed or reset) so the next drain_outbox / drain_pubrel / subscription drain (peeked via first_pending_group) retransmits everything once. Re-arming subscription requests here is what makes an unacknowledged SUBSCRIBE re-send on a resumed session too: brokers do not retransmit SUBACK/UNSUBACK as resumable session state. This is per-connection scratch state and is never persisted.

fn resolve_pending( &mut self, packet_id: PacketId, decide: &mut dyn FnMut(FilterEntry<'_>, usize) -> Resolution, )

Visits every filter entry pending (Subscribing or Unsubscribing) under packet_id, in packet order, and applies the returned Resolution. decide also receives the position of the entry as pending_filters reports it.

fn start_resubscribe(&mut self, packet_id: PacketId) -> Option<FilterEntry<'_>>

Starts the SUBSCRIBE of one Resubscribing filter under the free packet_id. The new state is the one FilterState::resubscribe returns. Returns the changed entry, or None if no filter waits to be resubscribed. Never allocates.

fn store(&mut self, op: Store<'_>) -> Result<(), Self::Error>

Stores new data.

§Errors

Returns Error if the session has no room for the data. The session is then unchanged.

Provided Methods§

fn apply(&mut self, op: Op<'_>) -> Result<Applied, Self::Error>

Applies a recorded operation, for example when replaying a journal.

§Errors

Returns the error of store.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl State for InMemory

Available on crate feature alloc only.
§

impl State for JournaledSession

Available on crate feature alloc only.