pub struct Dict { /* private fields */ }Expand description
A refcounted per-document string interner.
Never construct directly with Dict { ... } — use
Dict::new_refcounted which guarantees the heap allocation and
initial refcount of 1. Internally not Send/Sync despite the
atomic refcount: the RefCell guarding the table is single-
threaded. C-ABI consumers (libxml2) are GIL-protected on the
Python side and single-thread the dict accordingly.
Implementations§
Source§impl Dict
impl Dict
Sourcepub fn new_refcounted() -> *mut Dict
pub fn new_refcounted() -> *mut Dict
Allocate a fresh dict on the heap. Refcount starts at 1
(the caller’s reference). Returns a raw pointer; callers
must eventually balance with Dict::release.
Sourcepub unsafe fn new_sub(parent: *mut Dict) -> *mut Dict
pub unsafe fn new_sub(parent: *mut Dict) -> *mut Dict
Allocate a sub-dictionary chained to parent (libxml2
xmlDictCreateSub). The sub-dict shares the parent’s interned
strings — see the parent field — and takes one
reference to it, released when the sub-dict is freed. A NULL
parent is equivalent to new_refcounted.
§Safety
parent must be NULL or a live Dict* on which the caller can
take a reference.
Sourcepub fn retain_arena(&self, arena: Arc<Bump>)
pub fn retain_arena(&self, arena: Arc<Bump>)
Retain arena (an origin document’s node arena) so a node moved
out of it — whose name re-interned into this dict during a
cross-thread graft — outlives a drop of its origin document.
Deduped by pointer, so repeated grafts from one source arena keep
a single entry. The caller guarantees arena is not the
destination’s own (a same-arena move needs no retention).
Sourcepub fn add_ref(&self)
pub fn add_ref(&self)
Increment the refcount. Use when copying a *mut Dict to a
new owner. Cheap atomic op.
Sourcepub unsafe fn release(ptr: *mut Dict) -> bool
pub unsafe fn release(ptr: *mut Dict) -> bool
Decrement the refcount. When the last reference goes away,
drops every interned string and the dict itself. Returns
true if this call was the one that freed.
§Safety
ptr must be a non-null pointer previously returned by
Dict::new_refcounted, and the caller must own one
reference (i.e. exactly one outstanding add_ref not yet
paired with a release).
Sourcepub fn intern(&self, input: &[u8]) -> *const u8
pub fn intern(&self, input: &[u8]) -> *const u8
Look up input, inserting if absent. Returns the canonical
NUL-terminated pointer.
Sourcepub fn intern_str(&self, s: &str) -> *const u8
pub fn intern_str(&self, s: &str) -> *const u8
&str-keyed convenience over Self::intern.
Sourcepub fn lookup(&self, input: &[u8]) -> *const u8
pub fn lookup(&self, input: &[u8]) -> *const u8
Non-allocating lookup. Returns the canonical pointer on hit, NULL on miss.
Sourcepub fn owns(&self, ptr: *const u8) -> bool
pub fn owns(&self, ptr: *const u8) -> bool
Does ptr originate from this dict or any ancestor? libxml2’s
xmlDictOwns walks the sub-dict chain, so a string interned in
the parent is “owned” when probed through a child.