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: ~2 bytes per npub for the sorted index, plus the strings themselves.
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 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§fn clone(&self) -> NpubInterner
fn clone(&self) -> NpubInterner
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for NpubInterner
impl Debug for NpubInterner
Source§impl Default for NpubInterner
impl Default for NpubInterner
Source§fn default() -> NpubInterner
fn default() -> NpubInterner
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze 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
Mutably borrows from an owned value. Read more
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>
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 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>
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