Skip to main content

tuwunel_core/matrix/event/
state_key.rs

1//! State-event key types and ordering helpers.
2//!
3//! Keys combine a state event type with its state key for use in maps. The
4//! module also provides forward and reverse comparison functions.
5
6use std::cmp::Ordering;
7
8use ruma::events::StateEventType;
9use smallstr::SmallString;
10
11/// Composite key identifying one state event slot.
12///
13/// The event type is compared before the state-key string. This layout is used
14/// as the key for in-memory state maps.
15pub type TypeStateKey = (StateEventType, StateKey);
16
17/// Inline-backed string used for Matrix state keys.
18///
19/// The inline budget lets short keys remain inline, while longer keys spill to
20/// heap storage.
21pub type StateKey = SmallString<[u8; INLINE_SIZE]>;
22
23const INLINE_SIZE: usize = 48;
24
25/// Compares state keys in ascending event-type and state-key order.
26///
27/// Event type is the primary key and the state-key string breaks ties. The
28/// ordering matches the natural tuple order of `TypeStateKey`.
29#[inline]
30#[must_use]
31pub fn cmp(a: &TypeStateKey, b: &TypeStateKey) -> Ordering { a.0.cmp(&b.0).then(a.1.cmp(&b.1)) }
32
33/// Compares state keys in descending event-type and state-key order.
34///
35/// Both components are reversed together, producing the inverse of `cmp`. It is
36/// suitable for descending sorts over the same key domain.
37#[inline]
38#[must_use]
39pub fn rcmp(a: &TypeStateKey, b: &TypeStateKey) -> Ordering { b.0.cmp(&a.0).then(b.1.cmp(&a.1)) }