JournaledSession

Struct JournaledSession 

pub struct JournaledSession {
    inner: InMemorySession,
    buf: Vec<u8>,
}
Available on crate feature alloc only.
Expand description

A SessionState wrapper that serializes every mutation into raw bytes.

The inner InMemorySession is updated immediately so that queries always reflect the latest state. Operations accumulate as encoded bytes in an internal buffer until the caller takes them with take_journal for persistence.

This is a sans-io building block: it knows nothing about files or async. The I/O layer (e.g. the tokio crate) is responsible for writing the journal bytes to stable storage and calling replay on startup.

Fields§

§inner: InMemorySession§buf: Vec<u8>

Implementations§

§

impl JournaledSession

pub const fn new() -> Self

Creates a new journaled session with an empty in-memory state.

pub const fn from_inner(inner: InMemorySession) -> Self

Creates a journaled session wrapping an existing in-memory session.

Useful when replaying a journal: first rebuild the InMemorySession, then wrap it so that future mutations are recorded.

pub fn take_journal(&mut self) -> Vec<u8>

Takes the accumulated journal bytes, leaving the internal buffer empty.

The caller should persist these bytes (e.g. append to a file) before acknowledging the corresponding protocol events. The bytes can later be replayed with replay.

pub const fn journal_is_empty(&self) -> bool

Returns true if no journal bytes have accumulated since the last take_journal.

pub const fn inner(&self) -> &InMemorySession

Borrow the inner in-memory session.

pub const fn inner_mut(&mut self) -> &mut InMemorySession

Mutably borrow the inner in-memory session.

Mutations made through this reference are not recorded in the journal. Use this only for replay or testing.

pub fn replay( data: &[u8], session: &mut impl SessionState, ) -> Result<(), DecodeError>

Replay journal bytes against a SessionState, reproducing the mutations that were originally recorded.

This is typically called at startup to rebuild session state from a persisted journal file.

§Errors

Returns DecodeError if the journal bytes are malformed.

fn push_id_op(&mut self, tag: u8, packet_id: PacketId)

Appends raw bytes for a packet-id-only operation.

fn push_store_op( &mut self, tag: u8, packet_id: PacketId, phase: Option<Qos2Phase>, msg: (&Topic, &[u8], bool, &PublishProperties), )

Appends raw bytes for a store operation (QoS 1 or 2).

Trait Implementations§

§

impl Default for JournaledSession

§

fn default() -> Self

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

impl SessionState for JournaledSession

§

fn store_outgoing_qos1( &mut self, packet_id: PacketId, topic: &Topic, payload: &[u8], retain: bool, properties: &PublishProperties, )

Store a QoS 1 outbound publish (PUBLISH sent, awaiting PUBACK). Read more
§

fn remove_outgoing_qos1(&mut self, packet_id: PacketId)

Remove a QoS 1 outbound publish after PUBACK is received.
§

fn store_outgoing_qos2( &mut self, packet_id: PacketId, phase: Qos2Phase, topic: &Topic, payload: &[u8], retain: bool, properties: &PublishProperties, )

Store a QoS 2 outbound publish. Read more
§

fn advance_outgoing_qos2(&mut self, packet_id: PacketId)

Advance a QoS 2 outbound publish to the PUBREL-sent phase. Read more
§

fn remove_outgoing_qos2(&mut self, packet_id: PacketId)

Remove a QoS 2 outbound publish after PUBCOMP is received (or PUBREC with rc ≥ 0x80).
§

fn store_incoming_qos2(&mut self, packet_id: PacketId)

Store a QoS 2 inbound packet identifier (PUBREC sent, awaiting PUBREL from the server).
§

fn remove_incoming_qos2(&mut self, packet_id: PacketId)

Remove a QoS 2 inbound packet identifier after PUBREL is received and PUBCOMP has been sent.
§

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

Check whether a QoS 1 outbound publish is stored for this packet ID.
§

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

Check whether a QoS 2 outbound publish is stored for this packet ID.
§

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

Check whether a QoS 2 inbound packet identifier is stored (i.e., PUBREC was already sent for this packet ID).
§

fn try_for_each_outgoing_qos1<B>( &self, f: Qos1Callback<'_, B>, ) -> ControlFlow<B>

Iterate over all stored outgoing QoS 1 messages for retransmission on reconnect. Read more
§

fn try_for_each_outgoing_qos2<B>( &self, f: &mut dyn FnMut(OutgoingQos2<'_>) -> ControlFlow<B>, ) -> ControlFlow<B>

Iterate over all stored outgoing QoS 2 messages for retransmission on reconnect. Read more
§

fn try_for_each_incoming_qos2<B>( &self, f: &mut dyn FnMut(PacketId) -> ControlFlow<B>, ) -> ControlFlow<B>

Iterate over all stored incoming QoS 2 packet identifiers for re-acknowledging on reconnect. Read more
§

fn clear(&mut self)

Clear all session state. Called when connecting with Clean Start = 1.

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.