pub struct NpubInterner { /* private fields */ }Expand description
String interner for npubs using sorted Vec + binary search.
Each unique npub is stored exactly once. Messages reference npubs by u16 index.
intern(): O(log n) lookup + O(n) insert for new stringsresolve(): O(1) by index
Memory: the npub bytes in one arena, plus a 4-byte offset and a 2-byte sorted index each.
Implementations§
Source§impl NpubInterner
impl NpubInterner
pub fn new() -> Self
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Pre-allocate capacity for expected number of unique npubs
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Room for additional more npubs, so a bulk load grows each buffer once.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Drop the growth slack, once a bulk load is done.
Sourcepub fn intern_emoji(&mut self, emoji: &str, url: Option<&str>) -> u32
pub fn intern_emoji(&mut self, emoji: &str, url: Option<&str>) -> u32
Intern a reaction’s (emoji, url) pair, returning its stable index.
Sourcepub fn emoji(&self, idx: u32) -> (&str, Option<&str>)
pub fn emoji(&self, idx: u32) -> (&str, Option<&str>)
Resolve an interned (emoji, url) pair; an unknown index reads as no emoji.
Sourcepub fn intern(&mut self, npub: &str) -> u16
pub fn intern(&mut self, npub: &str) -> u16
Intern an npub string, returning its stable index.
If the npub already exists, returns the existing index. If new, stores it and returns a new index.
Sourcepub fn intern_opt(&mut self, npub: Option<&str>) -> u16
pub fn intern_opt(&mut self, npub: Option<&str>) -> u16
Intern an optional npub, returning NO_NPUB sentinel for None.
Sourcepub fn lookup(&self, npub: &str) -> Option<u16>
pub fn lookup(&self, npub: &str) -> Option<u16>
Look up an npub without inserting. Returns its handle if already interned.
Sourcepub fn resolve(&self, idx: u16) -> Option<&str>
pub fn resolve(&self, idx: u16) -> Option<&str>
Resolve an index back to the npub string.
Returns None for NO_NPUB sentinel or out-of-bounds index.
pub fn is_empty(&self) -> bool
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Total memory used by the interner (approximate)
Trait Implementations§
Source§impl Clone for NpubInterner
impl Clone for NpubInterner
Source§impl Debug for NpubInterner
impl Debug for NpubInterner
Auto Trait Implementations§
impl Forget for NpubInterner
impl Freeze for NpubInterner
impl Move for NpubInterner
impl RefUnwindSafe for NpubInterner
impl Send for NpubInterner
impl Sync for NpubInterner
impl Unpin for NpubInterner
impl UnsafeUnpin for NpubInterner
impl UnwindSafe for NpubInterner
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