pub struct NameTable { /* private fields */ }Expand description
String interning table for names and namespace URIs
Provides O(1) average-case lookup and insertion. All strings in the schema model should be interned through this table.
Uses interior mutability to allow adding strings through shared references.
§Example
use xsd_schema::namespace::NameTable;
let table = NameTable::new();
let id1 = table.add("hello");
let id2 = table.add("hello");
assert_eq!(id1, id2); // Same string -> same ID
assert_eq!(table.resolve(id1), "hello");Implementations§
Source§impl NameTable
impl NameTable
Sourcepub fn add(&self, value: &str) -> NameId
pub fn add(&self, value: &str) -> NameId
Add a string to the table, returning its NameId
If the string already exists, returns the existing NameId. Otherwise, creates a new entry and returns a new NameId.
Uses interior mutability, so this can be called through a shared reference.
Sourcepub fn resolve_ref(&self, id: NameId) -> &str
pub fn resolve_ref(&self, id: NameId) -> &str
Resolve a NameId to a string slice (zero-allocation).
Prefer this over resolve() in performance-critical paths
(e.g., DomNavigator implementations).
§Safety (internal)
Uses RefCell::as_ptr() to bypass the borrow guard. This is sound because:
Entry.textisBox<str>— heap-stable allocationEntry.textis never replaced or removed after insertion (other Entry fields likenextmay be mutated during rehashing, but onlytextstability matters here)- The returned
&strpoints to the Box’s heap data, not the Vec buffer, so it remains valid even if the Vec reallocates during a lateradd()
§Panics
Panics if the NameId is out of bounds.
Sourcepub fn try_resolve_ref(&self, id: NameId) -> Option<&str>
pub fn try_resolve_ref(&self, id: NameId) -> Option<&str>
Try to resolve a NameId to a string slice (zero-allocation).
Returns None if the NameId is out of bounds.
Sourcepub fn resolve(&self, id: NameId) -> String
pub fn resolve(&self, id: NameId) -> String
Resolve a NameId to its string value
Returns a copy of the string. For zero-allocation access, use resolve_ref().
§Panics
Panics if the NameId is invalid (out of bounds).
Sourcepub fn try_resolve(&self, id: NameId) -> Option<String>
pub fn try_resolve(&self, id: NameId) -> Option<String>
Try to resolve a NameId to its string value
Returns a copy of the string. For zero-allocation access, use try_resolve_ref().