Skip to main content

JSInternedStrRef

Struct JSInternedStrRef 

Source
pub struct JSInternedStrRef<'a, 'b> { /* private fields */ }
Expand description

A double reference to an interned string inside Interner.

JSInternedStrRef::utf8 returns an Option, since not every UTF-16 string is fully representable as a UTF-8 string (because of unpaired surrogates). However, every UTF-8 string is representable as a UTF-16 string, so JSInternedStrRef::utf8 returns a &[u16].

Implementations§

Source§

impl<'a, 'b> JSInternedStrRef<'a, 'b>

Source

pub const fn utf8(&self) -> Option<&'a str>

Returns the inner reference to the interned string in UTF-8 encoding. if the string is not representable in UTF-8, returns None

§Examples
use boa_interner::Interner;

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

pub const fn utf16(&self) -> &'b [u16]

Returns the inner reference to the interned string in UTF-16 encoding.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let interned = interner.resolve_expect(sym);
let utf16: Vec<u16> = "hello".encode_utf16().collect();
assert_eq!(interned.utf16(), utf16.as_slice());
Source

pub fn join<F, G, T>(self, f: F, g: G, prioritize_utf8: bool) -> T
where F: FnOnce(&'a str) -> T, G: FnOnce(&'b [u16]) -> T,

Joins the result of both possible strings into a common type.

If self is representable by a UTF-8 string and the prioritize_utf8 argument is set, it will prioritize calling f, and will only call g if self is only representable by a UTF-16 string. Otherwise, it will directly call g.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let interned = interner.resolve_expect(sym);
let result = interned.join(
    |utf8| utf8.to_uppercase(),
    |utf16| String::from_utf16_lossy(utf16).to_uppercase(),
    true,
);
assert_eq!(result, "HELLO");
Source

pub fn join_with_context<C, F, G, T>( self, f: F, g: G, ctx: C, prioritize_utf8: bool, ) -> T
where F: FnOnce(&'a str, C) -> T, G: FnOnce(&'b [u16], C) -> T,

Same as join, but where you can pass an additional context.

Useful when you have a &mut Context context that cannot be borrowed by both closures at the same time.

§Examples
use boa_interner::Interner;

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let interned = interner.resolve_expect(sym);
let mut output = String::new();
interned.join_with_context(
    |utf8, buf: &mut String| buf.push_str(&utf8.to_uppercase()),
    |utf16, buf: &mut String| buf.push_str(&String::from_utf16_lossy(utf16).to_uppercase()),
    &mut output,
    true,
);
assert_eq!(output, "HELLO");
Source

pub fn into_common<C>(self, prioritize_utf8: bool) -> C
where C: From<&'a str> + From<&'b [u16]>,

Converts both string types into a common type C.

If self is representable by a UTF-8 string and the prioritize_utf8 argument is set, it will prioritize converting its UTF-8 representation first, and will only convert its UTF-16 representation if it is only representable by a UTF-16 string. Otherwise, it will directly convert its UTF-16 representation.

§Examples
use boa_interner::Interner;

enum JsString<'a> {
    Utf8(&'a str),
    Utf16(&'a [u16]),
}

impl<'a> From<&'a str> for JsString<'a> {
    fn from(s: &'a str) -> Self {
        JsString::Utf8(s)
    }
}

impl<'a> From<&'a [u16]> for JsString<'a> {
    fn from(s: &'a [u16]) -> Self {
        JsString::Utf16(s)
    }
}

let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let interned = interner.resolve_expect(sym);
let result: JsString<'_> = interned.into_common(true);
assert!(matches!(result, JsString::Utf8("hello")));

Trait Implementations§

Source§

impl<'a, 'b> Clone for JSInternedStrRef<'a, 'b>

Source§

fn clone(&self) -> JSInternedStrRef<'a, 'b>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, 'b> Copy for JSInternedStrRef<'a, 'b>

Source§

impl<'a, 'b> Debug for JSInternedStrRef<'a, 'b>

Source§

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

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

impl Display for JSInternedStrRef<'_, '_>

Source§

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

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

impl<'a, 'b> Eq for JSInternedStrRef<'a, 'b>

Source§

impl<'a, 'b> Hash for JSInternedStrRef<'a, 'b>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, 'b> PartialEq for JSInternedStrRef<'a, 'b>

Source§

fn eq(&self, other: &JSInternedStrRef<'a, 'b>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a, 'b> StructuralPartialEq for JSInternedStrRef<'a, 'b>

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> RefUnwindSafe for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> Send for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> Sync for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> Unpin for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> UnsafeUnpin for JSInternedStrRef<'a, 'b>

§

impl<'a, 'b> UnwindSafe for JSInternedStrRef<'a, 'b>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.