Skip to main content

CompactMessage

Struct CompactMessage 

Source
pub struct CompactMessage {
Show 16 fields pub id: [u8; 32], pub at: u64, pub expiration_secs: u32, pub flags: MessageFlags, pub npub_idx: u16, pub replied_to: Option<Box<[u8; 32]>>, pub replied_to_npub_idx: u16, pub wrapper_id: Option<Box<[u8; 32]>>, pub content: Box<str>, pub replied_to_content: Option<Box<str>>, pub attachments: TinyVec<CompactAttachment>, pub reactions: TinyVec<CompactReaction>, pub edit_history: Option<Box<Vec<EditEntry>>>, pub preview_metadata: Option<Box<SiteMetadata>>, pub emoji_tags: Option<Box<Vec<EmojiTag>>>, pub addressed_bots: Option<Box<Vec<u16>>>,
}
Expand description

Memory-efficient message with binary IDs and interned npubs.

Compared to the regular Message struct:

  • IDs use [u8; 32] instead of hex String (saves ~56 bytes each)
  • npubs use u16 index into interner (saves ~85 bytes each)
  • Booleans packed into MessageFlags (saves ~24 bytes + 2 for replied_to_has_attachment)
  • Boxed optional IDs (replied_to, wrapper_id) save ~40 bytes when None
  • Compact timestamp (u32 seconds since 2020) saves 4 bytes
  • TinyVec for attachments/reactions (8 bytes vs 24 = saves 32 bytes)
  • Box for content (8 bytes vs 24 = saves 16 bytes)
  • Total savings: ~350+ bytes per message

Fields§

§id: [u8; 32]

Message ID as binary (64 hex chars -> 32 bytes)

§at: u64

Timestamp in milliseconds (full precision for sub-second ordering)

§expiration_secs: u32

NIP-40 expiry as unix SECONDS (0 = permanent). u32 holds it until 2106 and costs 4 bytes inline — self-destruct messages are rare and short- lived, so a boxed Option would only add heap churn on the purge path.

§flags: MessageFlags

Packed boolean flags (mine, pending, failed, replied_to_has_attachment)

§npub_idx: u16

Index into NpubInterner for sender’s npub (NO_NPUB if none)

§replied_to: Option<Box<[u8; 32]>>

Replied-to message ID (boxed - None for ~70% of messages saves 24 bytes)

§replied_to_npub_idx: u16

Index into NpubInterner for replied-to author (NO_NPUB if none)

§wrapper_id: Option<Box<[u8; 32]>>

Wrapper event ID for gift-wrapped messages (boxed - saves 25 bytes when None)

§content: Box<str>

Message content (Box = 16 bytes vs String’s 24 bytes)

§replied_to_content: Option<Box<str>>

Content of replied-to message

§attachments: TinyVec<CompactAttachment>

File attachments (CompactAttachment = ~120 bytes vs Attachment’s ~320 bytes)

§reactions: TinyVec<CompactReaction>

Emoji reactions (CompactReaction = ~82 bytes vs Reaction’s ~292 bytes)

§edit_history: Option<Box<Vec<EditEntry>>>

Edit history - boxed since <1% of messages are edited (saves 16 bytes inline)

§preview_metadata: Option<Box<SiteMetadata>>

Link preview metadata - boxed since ~216 bytes but rare (saves ~208 bytes)

§emoji_tags: Option<Box<Vec<EmojiTag>>>

NIP-30 emoji tags travelling with this rumor — boxed because the vast majority of messages have none, so the cold path stays cheap.

§addressed_bots: Option<Box<Vec<u16>>>

Bot routing targets as interned npub handles — boxed because only command invocations carry any.

Implementations§

Source§

impl CompactMessage

Source

pub fn has_reply(&self) -> bool

Check if this message has a replied-to reference

Source

pub fn is_edited(&self) -> bool

Check if this message has been edited

Source

pub fn id_hex(&self) -> String

Get the message ID as a string (hex for event IDs, “pending-…” for pending)

Source

pub fn replied_to_hex(&self) -> String

Get the replied-to ID as a hex string, or empty if none

Source

pub fn wrapper_id_hex(&self) -> Option<String>

Get wrapper ID as hex string if present

Source

pub fn timestamp_ms(&self) -> u64

Get timestamp as milliseconds (for compatibility with frontend)

Source

pub fn apply_edit( &mut self, new_content: String, edited_at: u64, emoji_tags: Vec<EmojiTag>, )

Apply an edit to this message.

emoji_tags are the NIP-30 custom-emoji tags resolved from the new content; they’re adopted only when this edit is the newest revision so an out-of-order older edit can’t clobber the live content’s emoji.

Source

pub fn replied_to_has_attachment(&self) -> Option<bool>

Get replied_to_has_attachment from flags

Source

pub fn add_reaction( &mut self, reaction: Reaction, interner: &mut NpubInterner, ) -> bool

Add a reaction to this message Note: Since TinyVec is immutable, this rebuilds the entire reactions list

Source

pub fn remove_reaction(&mut self, reaction_id: &str) -> bool

Remove a reaction by its hex event id. Returns true if one was removed.

Source

pub fn is_mine(&self) -> bool

Source

pub fn is_pending(&self) -> bool

Source

pub fn is_failed(&self) -> bool

Source

pub fn set_pending(&mut self, value: bool)

Source

pub fn set_failed(&mut self, value: bool)

Source

pub fn set_mine(&mut self, value: bool)

Source§

impl CompactMessage

Source

pub fn from_message(msg: &Message, interner: &mut NpubInterner) -> Self

Convert from a regular Message (borrowed), interning npubs

Source

pub fn from_message_owned(msg: Message, interner: &mut NpubInterner) -> Self

Convert from a regular Message (owned) - ZERO-COPY for strings!

Takes ownership of the Message and moves strings directly. Use this when you don’t need the original Message anymore.

Source

pub fn to_message(&self, interner: &NpubInterner) -> Message

Convert back to a regular Message, resolving npubs from interner

Trait Implementations§

Source§

impl Clone for CompactMessage

Source§

fn clone(&self) -> CompactMessage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompactMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl DeepSize for CompactMessage

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more