Skip to main content

Interner

Struct Interner 

Source
pub struct Interner { /* private fields */ }
Expand description

The string interner for Boa.

Implementations§

Source§

impl Interner

Source

pub fn new() -> Self

Creates a new Interner.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
assert!(interner.resolve(sym).is_some());
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new Interner with the specified capacity.

§Examples
use boa_interner::Interner;

let mut interner = Interner::with_capacity(10);
let sym = interner.get_or_intern("hello");
assert!(interner.resolve(sym).is_some());
Source

pub fn len(&self) -> usize

Returns the number of strings interned by the interner.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let initial_len = interner.len();
interner.get_or_intern("hello");
assert_eq!(interner.len(), initial_len + 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the Interner contains no interned strings.

§Examples
use boa_interner::Interner;

let interner = Interner::new();
assert!(!interner.is_empty());
Source

pub fn get<'a, T>(&self, string: T) -> Option<Sym>
where T: Into<JStrRef<'a>>,

Returns the symbol for the given string if any.

Can be used to query if a string has already been interned without interning.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
assert!(interner.get("hello").is_none());
interner.get_or_intern("hello");
assert!(interner.get("hello").is_some());
Source

pub fn get_or_intern<'a, T>(&mut self, string: T) -> Sym
where T: Into<JStrRef<'a>>,

Interns the given string.

Returns a symbol for resolution into the original string.

§Panics

If the interner already interns the maximum number of strings possible by the chosen symbol type.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym1 = interner.get_or_intern("hello");
let sym2 = interner.get_or_intern("hello");
assert_eq!(sym1, sym2);
let sym3 = interner.get_or_intern("world");
assert_ne!(sym1, sym3);
Source

pub fn get_or_intern_static( &mut self, utf8: &'static str, utf16: &'static [u16], ) -> Sym

Interns the given 'static string.

Returns a symbol for resolution into the original string.

§Note

This is more efficient than Interner::get_or_intern, since it avoids allocating space for one string inside the Interner, with the disadvantage that you need to provide both the UTF-8 and the UTF-16 representation of the string.

§Panics

If the interner already interns the maximum number of strings possible by the chosen symbol type.

§Examples
use boa_interner::Interner;

static HELLO_UTF16: &[u16] = &[0x68, 0x65, 0x6C, 0x6C, 0x6F];

let mut interner = Interner::new();
let sym1 = interner.get_or_intern_static("hello", HELLO_UTF16);
let sym2 = interner.get_or_intern("hello");
assert_eq!(sym1, sym2);
Source

pub fn resolve(&self, symbol: Sym) -> Option<JSInternedStrRef<'_, '_>>

Returns the string for the given symbol if any.

§Panics

Panics if the size of both statics is not equal or the interners do not have the same size

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let resolved = interner.resolve(sym);
assert!(resolved.is_some());
assert_eq!(resolved.unwrap().utf8(), Some("hello"));
Source

pub fn resolve_expect(&self, symbol: Sym) -> JSInternedStrRef<'_, '_>

Returns the string for the given symbol.

§Panics

If the interner cannot resolve the given symbol.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let resolved = interner.resolve_expect(sym);
assert_eq!(resolved.utf8(), Some("hello"));
Source

pub fn is_latin1(&self, symbol: Sym) -> bool

Returns true if the string identified by symbol can be encoded as Latin1 (i.e. all code units are in the range 0x00..=0xFF).

This information is computed once when the string is first interned, so callers pay no O(n) scanning cost beyond the initial intern call.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let ascii = interner.get_or_intern("hello");
assert!(interner.is_latin1(ascii));

let non_latin1: Vec<u16> = vec![0x4e2d, 0x6587]; // "中文"
let sym = interner.get_or_intern(non_latin1.as_slice());
assert!(!interner.is_latin1(sym));

Trait Implementations§

Source§

impl Debug for Interner

Source§

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

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

impl Default for Interner

Source§

fn default() -> Interner

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.