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>
impl<'a, 'b> JSInternedStrRef<'a, 'b>
Sourcepub const fn utf8(&self) -> Option<&'a str>
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"));Sourcepub const fn utf16(&self) -> &'b [u16]
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());Sourcepub fn join<F, G, T>(self, f: F, g: G, prioritize_utf8: bool) -> T
pub fn join<F, G, T>(self, f: F, g: G, prioritize_utf8: bool) -> 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");Sourcepub fn join_with_context<C, F, G, T>(
self,
f: F,
g: G,
ctx: C,
prioritize_utf8: bool,
) -> T
pub fn join_with_context<C, F, G, T>( self, f: F, g: G, ctx: C, prioritize_utf8: bool, ) -> 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");Sourcepub fn into_common<C>(self, prioritize_utf8: bool) -> C
pub fn into_common<C>(self, prioritize_utf8: bool) -> C
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>
impl<'a, 'b> Clone for JSInternedStrRef<'a, 'b>
Source§fn clone(&self) -> JSInternedStrRef<'a, 'b>
fn clone(&self) -> JSInternedStrRef<'a, 'b>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<'a, 'b> Copy for JSInternedStrRef<'a, 'b>
Source§impl<'a, 'b> Debug for JSInternedStrRef<'a, 'b>
impl<'a, 'b> Debug for JSInternedStrRef<'a, 'b>
Source§impl Display for JSInternedStrRef<'_, '_>
impl Display for JSInternedStrRef<'_, '_>
impl<'a, 'b> Eq for JSInternedStrRef<'a, 'b>
Source§impl<'a, 'b> Hash for JSInternedStrRef<'a, 'b>
impl<'a, 'b> Hash for JSInternedStrRef<'a, 'b>
Source§impl<'a, 'b> PartialEq for JSInternedStrRef<'a, 'b>
impl<'a, 'b> PartialEq for JSInternedStrRef<'a, 'b>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.