pub struct CompactMessageVec { /* private fields */ }Expand description
Sorted message storage with O(log n) lookup by ID.
Messages are stored sorted by timestamp. A separate index of positions, ordered by each message’s id, gives O(log n) lookup by binary search through the positions.
Implementations§
Source§impl CompactMessageVec
impl CompactMessageVec
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn is_empty(&self) -> bool
Sourcepub fn messages(&self) -> &[CompactMessage]
pub fn messages(&self) -> &[CompactMessage]
Get all messages (sorted by timestamp)
Sourcepub fn messages_mut(&mut self) -> &mut Vec<CompactMessage>
pub fn messages_mut(&mut self) -> &mut Vec<CompactMessage>
Get a mutable reference to all messages. Call rebuild_index() after changing
any id or the order.
Sourcepub fn iter(&self) -> Iter<'_, CompactMessage> ⓘ
pub fn iter(&self) -> Iter<'_, CompactMessage> ⓘ
Iterate over messages (supports .rev())
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, CompactMessage> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, CompactMessage> ⓘ
Iterate over messages mutably
Sourcepub fn last(&self) -> Option<&CompactMessage>
pub fn last(&self) -> Option<&CompactMessage>
Get the last message
Sourcepub fn last_timestamp(&self) -> Option<u64>
pub fn last_timestamp(&self) -> Option<u64>
Get last message timestamp (in milliseconds)
Sourcepub fn first(&self) -> Option<&CompactMessage>
pub fn first(&self) -> Option<&CompactMessage>
Get the first message
Sourcepub fn find_by_id(&self, id: &[u8; 32]) -> Option<&CompactMessage>
pub fn find_by_id(&self, id: &[u8; 32]) -> Option<&CompactMessage>
Find a message by ID using binary search - O(log n)
Sourcepub fn find_by_id_mut(&mut self, id: &[u8; 32]) -> Option<&mut CompactMessage>
pub fn find_by_id_mut(&mut self, id: &[u8; 32]) -> Option<&mut CompactMessage>
Find a message by ID (mutable) - O(log n)
Sourcepub fn find_by_hex_id(&self, id_str: &str) -> Option<&CompactMessage>
pub fn find_by_hex_id(&self, id_str: &str) -> Option<&CompactMessage>
Find a message by ID string (hex or pending) - O(log n)
Sourcepub fn find_by_hex_id_mut(
&mut self,
id_str: &str,
) -> Option<&mut CompactMessage>
pub fn find_by_hex_id_mut( &mut self, id_str: &str, ) -> Option<&mut CompactMessage>
Find a message by ID string (mutable) - O(log n)
Sourcepub fn contains_id(&self, id: &[u8; 32]) -> bool
pub fn contains_id(&self, id: &[u8; 32]) -> bool
Check if a message with the given ID exists - O(log n)
Sourcepub fn remove_by_hex_id(&mut self, id_str: &str) -> bool
pub fn remove_by_hex_id(&mut self, id_str: &str) -> bool
Remove a message by hex ID string. Returns true if removed.
Sourcepub fn remove_by_id(&mut self, id: &[u8; 32]) -> bool
pub fn remove_by_id(&mut self, id: &[u8; 32]) -> bool
Remove a message by binary ID. Returns true if removed.
Sourcepub fn rekey(&mut self, old: &[u8; 32], new: [u8; 32]) -> bool
pub fn rekey(&mut self, old: &[u8; 32], new: [u8; 32]) -> bool
Give a message a new id in place (a pending send taking its event id), moving only
its own index entry. Returns false if old isn’t held or new already is.
Sourcepub fn contains_hex_id(&self, id_str: &str) -> bool
pub fn contains_hex_id(&self, id_str: &str) -> bool
Check if a message with the given ID string exists - O(log n)
Sourcepub fn insert(&mut self, msg: CompactMessage) -> bool
pub fn insert(&mut self, msg: CompactMessage) -> bool
Insert a message, maintaining sort order by timestamp.
Returns true if the message was added, false if duplicate ID.
Performance: append costs a binary search plus one index shift (ids are random, so the shift averages half the index); an out-of-order insert is O(n).
Sourcepub fn rebuild_index(&mut self)
pub fn rebuild_index(&mut self)
Rebuild the ID index (call after bulk modifications)
Sourcepub fn insert_batch(
&mut self,
messages: impl IntoIterator<Item = CompactMessage>,
) -> usize
pub fn insert_batch( &mut self, messages: impl IntoIterator<Item = CompactMessage>, ) -> usize
Batch insert messages - optimized for different scenarios.
Returns the number of messages actually added (excludes duplicates).
Performance:
- Append case (newer msgs): O(k log k + n)
- Prepend case (older msgs): O(k log k + n)
- Mixed: O(n log n) full sort
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Heap held by the two vectors, at capacity (not what the messages own)
Sourcepub fn retain_newest(&mut self, keep: usize) -> usize
pub fn retain_newest(&mut self, keep: usize) -> usize
Keep the newest keep messages plus any still pending (a pending send exists nowhere
else until it lands), then hand the freed capacity back. Returns how many went.
Sourcepub fn drain(&mut self, range: Range<usize>) -> Drain<'_, CompactMessage> ⓘ
pub fn drain(&mut self, range: Range<usize>) -> Drain<'_, CompactMessage> ⓘ
Drain messages from a range (rebuilds index after)
Sourcepub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
Sort messages by a key (rebuilds index after)
Trait Implementations§
Source§impl Clone for CompactMessageVec
impl Clone for CompactMessageVec
Source§impl Debug for CompactMessageVec
impl Debug for CompactMessageVec
Auto Trait Implementations§
impl Forget for CompactMessageVec
impl Freeze for CompactMessageVec
impl Move for CompactMessageVec
impl RefUnwindSafe for CompactMessageVec
impl Send for CompactMessageVec
impl Sync for CompactMessageVec
impl Unpin for CompactMessageVec
impl UnsafeUnpin for CompactMessageVec
impl UnwindSafe for CompactMessageVec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more