Struct JSString

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

A UTF16 character buffer.

The fundamental string representation in JavaScript. Since this is using a UTF16 encoding and Rust strings are using UTF8 encoding, converting between string representations is not cheap.

In this crate, implementations of the conversion traits Into and From are provided for JSString. This allows conversion from &str and String into JSString:

let j: JSString = "abc".into();

Similarly, a JSString can be converted to a String via a conversion trait or directly:

let j: JSString = "abc".into();
let s: String = (&j).into(); // Requires a reference.
let s: String = j.to_string();

In this crate, functions that need a JSString use generics so that they can take anything that can be converted to a JSString instead. This allows the caller to pass an &str or String, or to cache a previously converted JSString and pass that directly.

A JSString is not a JSValue and so it can not be passed where a JSValue is expected. Instead, it must be boxed using JSValue::new_string.

Implementations§

Source§

impl JSString

Source

pub fn len(&self) -> usize

Return the number of Unicode characters in this JavaScript string.

Remember that strings in JavaScript are UTF-16 encoded.

let str = JSString::from("😄");

// The JavaScript string length is 2, since it's UTF-16 encoded.
assert_eq!(str.len(), 2);

// But once encoded into UTF-8 as a Rust string, it's 4.
assert_eq!(str.to_string().len(), 4);
Source

pub fn is_empty(&self) -> bool

Check whether the string is empty.

assert!(JSString::from("").is_empty());
assert!(!JSString::from("abc").is_empty());

Trait Implementations§

Source§

impl Debug for JSString

Source§

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

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

impl Display for JSString

Source§

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

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

impl Drop for JSString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'s> From<&'s JSString> for String

Source§

fn from(s: &'s JSString) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for JSString

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for JSString

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<'s> PartialEq<&'s str> for JSString

Source§

fn eq(&self, other: &&'s str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'s> PartialEq<JSString> for &'s str

Source§

fn eq(&self, other: &JSString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<JSString> for String

Source§

fn eq(&self, other: &JSString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for JSString

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for JSString

Source§

fn eq(&self, other: &JSString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for JSString

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