mod.rs
Overview
This file defines the ThreadMessageQueueState structure and its associated implementations which represent the state of a message queue for threads, keyed by account addresses. It manages a collection of message inboxes per account and tracks the ordering of these accounts for message processing. The file also exposes builders and utilities related to constructing and manipulating the message queue state, particularly through diffs and ordered sets.
The primary focus is managing and querying the messages associated with accounts in a thread-safe, ordered manner, facilitating incremental updates and retrievals of messages for specific accounts.
Modules
account_messages_iterator: Public submodule likely providing iteration utilities over account messages within the queue.diff: Private submodule for handling state differences (diffs) and building new state versions incrementally.order_set: Private submodule managing the ordering of accounts within the message queue.
Structs and Implementations
ThreadMessageQueueState
This struct encapsulates the state of a thread message queue.
Fields
messages: BTreeMap<AccountAddress, AccountInbox>Stores the mapping from account addresses to their respective message inboxes.
Uses a
BTreeMapfor sorted key storage.Marked with a TODO comment indicating the current solution is a temporary or "dirty" one, possibly needing refactoring.
order_set: order_set::OrderSetMaintains the order of accounts for processing message queues.
Uses a custom-defined
OrderSettype from theorder_setmodule.
cursor: usizeTracks the current position within the ordered set or message queue.
Purpose likely related to maintaining iteration or processing state.
Traits Implemented
CloneEnables cloning of the entire state structure.
Serialize and Deserialize
Allows the state to be serialized/deserialized, supporting persistence or network transmission.
Custom debug formatter implemented to output
messagesandorder_setfields for debugging purposes.
Methods
empty() -> SelfReturns a new, empty
ThreadMessageQueueStateinstance.Initializes
messagesas an empty map,order_setas a new empty set, andcursorto zero.
Usage Example:
let empty_state = ThreadMessageQueueState::empty();account_inbox(&self, account_address: &AccountAddress) -> Option<&AccountInbox>Retrieves a reference to the inbox associated with a given
AccountAddressif it exists.
Parameters:
account_address: Reference to the account whose inbox is requested.
Returns:
Option<&AccountInbox>—Someif the inbox exists, otherwiseNone.
Usage Example:
if let Some(inbox) = state.account_inbox(&some_account_address) { // process inbox }build_next() -> ThreadMessageQueueStateBuilderProvides a builder object to create or update
ThreadMessageQueueStateinstances using diffs.Internally calls
ThreadMessageQueueStateDiff::builder()from thediffmodule.
Usage Example:
let builder = ThreadMessageQueueState::build_next(); // builder can be used to construct or modify state incrementallylength(&self) -> usizeReturns the total number of messages accumulated across all account inboxes.
Sums the length of each
AccountInboxinmessages.
Returns:
Total message count as
usize.
Usage Example:
let total_messages = state.length();destinations(&self) -> &order_set::OrderSetProvides access to the internal
OrderSetwhich represents the set of account destinations in order.
Returns:
Reference to the
OrderSet.
Usage Example:
let destinations = state.destinations();
Implementation Details and Algorithms
Message queues are stored as a
BTreeMapkeyed byAccountAddress, ensuring messages are sorted by account. This is useful for deterministic processing order and efficient lookup.The
OrderSettype manages the ordering of accounts, likely as a custom data structure optimized for ordering and quick membership checks.The
cursorfield likely serves as an iterator or processing pointer within the ordered message queue, though its usage is not fully detailed here.Incremental state modification is supported via the builder pattern exposed through the
build_next()method, which utilizes thediffsubmodule to apply changes efficiently.Serialization and deserialization allow the entire state to be saved or transmitted, facilitating persistence or distributed processing scenarios.
Interactions with Other Parts of the System
The file depends on types from the
crate::typesmodule, includingAccountAddressandAccountInbox, which represent the identity of accounts and their message containers.The
diffsubmodule provides functionality to compute and apply differences between queue states, enabling incremental updates and versioning.The
order_setsubmodule handles ordered collections of accounts, supporting the ordering semantics required by the message queue.The
account_messages_iteratormodule (public) likely offers iteration utilities which consume or traverse theThreadMessageQueueState, supporting message processing workflows.The
ThreadMessageQueueStateBuilderreturned bybuild_next()is a key interface for constructing or evolving the queue state, which interacts with diff computations and possibly external event sources.
Diagram
classDiagram
class ThreadMessageQueueState {
+messages: BTreeMap
-order_set: OrderSet
-cursor: usize
+empty()
+account_inbox()
+build_next()
+length()
+destinations()
}
ThreadMessageQueueState ..> AccountAddress : uses
ThreadMessageQueueState ..> AccountInbox : uses
ThreadMessageQueueState ..> order_set::OrderSet : contains
ThreadMessageQueueState ..> diff::ThreadMessageQueueStateDiff : uses builder
This diagram shows the main ThreadMessageQueueState struct, its key fields, and primary methods, as well as its relationships with AccountAddress, AccountInbox, the OrderSet for ordering, and the ThreadMessageQueueStateDiff for building updated states.