Skip to main content

NameTable

Struct NameTable 

Source
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

Source

pub fn new() -> Self

Create a new name table pre-seeded with standard namespaces

Source

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.

Source

pub fn get(&self, value: &str) -> Option<NameId>

Get the NameId for a string if it exists

Source

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.text is Box<str> — heap-stable allocation
  • Entry.text is never replaced or removed after insertion (other Entry fields like next may be mutated during rehashing, but only text stability matters here)
  • The returned &str points to the Box’s heap data, not the Vec buffer, so it remains valid even if the Vec reallocates during a later add()
§Panics

Panics if the NameId is out of bounds.

Source

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.

Source

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).

Source

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().

Source

pub fn len(&self) -> usize

Get the number of interned strings

Source

pub fn is_empty(&self) -> bool

Check if the table is empty

Trait Implementations§

Source§

impl Debug for NameTable

Source§

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

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

impl Default for NameTable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> MaybeSendSync for T

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.