pub struct JsValue { /* private fields */ }
Expand description

Representation of an object owned by JS.

A JsValue doesn’t actually live in Rust right now but actually in a table owned by the wasm-bindgen generated JS glue code. Eventually the ownership will transfer into wasm directly and this will likely become more efficient, but for now it may be slightly slow.

Implementations

The null JS value constant.

The undefined JS value constant.

The true JS value constant.

The false JS value constant.

Creates a new JS value which is a string.

The utf-8 string provided is copied to the JS heap and the string will be owned by the JS garbage collector.

Creates a new JS value which is a number.

This function creates a JS value representing a number (a heap allocated number) and returns a handle to the JS version of it.

Creates a new JS value which is a bigint from a string representing a number.

This function creates a JS value representing a bigint (a heap allocated large integer) and returns a handle to the JS version of it.

Creates a new JS value which is a boolean.

This function creates a JS object representing a boolean (a heap allocated boolean) and returns a handle to the JS version of it.

Creates a new JS value representing undefined.

Creates a new JS value representing null.

Creates a new JS symbol with the optional description specified.

This function will invoke the Symbol constructor in JS and return the JS object corresponding to the symbol created.

Creates a new JsValue from the JSON serialization of the object t provided.

This function will serialize the provided value t to a JSON string, send the JSON string to JS, parse it into a JS object, and then return a handle to the JS object. This is unlikely to be super speedy so it’s not recommended for large payloads, but it’s a nice to have in some situations!

Usage of this API requires activating the serde-serialize feature of the wasm-bindgen crate.

Errors

Returns any error encountered when serializing T into JSON.

Invokes JSON.stringify on this value and then parses the resulting JSON into an arbitrary Rust value.

This function will first call JSON.stringify on the JsValue itself. The resulting string is then passed into Rust which then parses it as JSON into the resulting value.

Usage of this API requires activating the serde-serialize feature of the wasm-bindgen crate.

Errors

Returns any error encountered when parsing the JSON into a T.

Returns the f64 value of this JS value if it’s an instance of a number.

If this JS value is not an instance of a number then this returns None.

Tests whether this JS value is a JS string.

If this JS value is a string value, this function copies the JS string value into wasm linear memory, encoded as UTF-8, and returns it as a Rust String.

To avoid the copying and re-encoding, consider the JsString::try_from() function from js-sys instead.

If this JS value is not an instance of a string or if it’s not valid utf-8 then this returns None.

UTF-16 vs UTF-8

JavaScript strings in general are encoded as UTF-16, but Rust strings are encoded as UTF-8. This can cause the Rust string to look a bit different than the JS string sometimes. For more details see the documentation about the str type which contains a few caveats about the encodings.

Returns the bool value of this JS value if it’s an instance of a boolean.

If this JS value is not an instance of a boolean then this returns None.

Tests whether this JS value is null

Tests whether this JS value is undefined

Tests whether the type of this JS value is symbol

Tests whether typeof self == "object" && self !== null.

Tests whether the type of this JS value is function.

Tests whether the type of this JS value is bigint.

Applies the unary typeof JS operator on a JsValue.

MDN documentation

Applies the binary in JS operator on the two JsValues.

MDN documentation

Tests whether the value is “truthy”.

Tests whether the value is “falsy”.

Compare two JsValues for equality, using the == operator in JS.

MDN documentation

Applies the unary ~ JS operator on a JsValue.

MDN documentation

Applies the binary >>> JS operator on the two JsValues.

MDN documentation

Applies the binary / JS operator on two JsValues, catching and returning any RangeError thrown.

MDN documentation

Applies the binary ** JS operator on the two JsValues.

MDN documentation

Applies the binary < JS operator on the two JsValues.

MDN documentation

Applies the binary <= JS operator on the two JsValues.

MDN documentation

Applies the binary >= JS operator on the two JsValues.

MDN documentation

Applies the binary > JS operator on the two JsValues.

MDN documentation

Applies the unary + JS operator on a JsValue. Can throw.

MDN documentation

Trait Implementations

Applies the binary + JS operator on two JsValues.

MDN documentation

The resulting type after applying the + operator.

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Converts this type into a shared reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Applies the binary & JS operator on two JsValues.

MDN documentation

The resulting type after applying the & operator.

The resulting type after applying the & operator.

Performs the & operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

Applies the binary | JS operator on two JsValues.

MDN documentation

The resulting type after applying the | operator.

The resulting type after applying the | operator.

Performs the | operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

Applies the binary ^ JS operator on two JsValues.

MDN documentation

The resulting type after applying the ^ operator.

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Applies the binary / JS operator on two JsValues.

MDN documentation

The resulting type after applying the / operator.

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Executes the destructor for this type. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

The wasm ABI type that this converts from when coming back out from the ABI boundary. Read more

Recover a Self from Self::Abi. Read more

The wasm ABI type that this converts into when crossing the ABI boundary. Read more

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary. Read more

The wasm ABI type that this converts into when crossing the ABI boundary. Read more

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary. Read more

Performs a dynamic instanceof check to see whether the JsValue provided is an instance of this type. Read more

Performs a zero-cost unchecked conversion from a JsValue into an instance of Self Read more

Performs a zero-cost unchecked conversion from a &JsValue into an instance of &Self. Read more

Test whether this JS value has a type T. Read more

Performs a dynamic cast (checked at runtime) of this value into the target type T. Read more

Performs a dynamic cast (checked at runtime) of this value into the target type T. Read more

Performs a zero-cost unchecked cast into the specified type. Read more

Performs a zero-cost unchecked cast into a reference to the specified type. Read more

Test whether this JS value is an instance of the type T. Read more

Performs a dynamic check to see whether the JsValue provided is a value of this type. Read more

Applies the binary * JS operator on two JsValues.

MDN documentation

The resulting type after applying the * operator.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Applies the unary - JS operator on a JsValue.

MDN documentation

The resulting type after applying the - operator.

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Applies the ! JS operator on a JsValue.

MDN documentation

The resulting type after applying the ! operator.

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Compares two JsValues for equality, using the === operator in JS.

MDN documentation

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The wasm ABI type references to Self are recovered from.

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous. Read more

Recover a Self::Anchor from Self::Abi. Read more

Applies the binary % JS operator on two JsValues.

MDN documentation

The resulting type after applying the % operator.

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Applies the binary << JS operator on two JsValues.

MDN documentation

The resulting type after applying the << operator.

The resulting type after applying the << operator.

Performs the << operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

Applies the binary >> JS operator on two JsValues.

MDN documentation

The resulting type after applying the >> operator.

The resulting type after applying the >> operator.

Performs the >> operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

Applies the binary - JS operator on two JsValues.

MDN documentation

The resulting type after applying the - operator.

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Applies the unary + JS operator on a JsValue. Returns the numeric result on success, or the JS error value on error.

MDN documentation

The type returned in the event of a conversion error.

Applies the unary + JS operator on a JsValue. Returns the numeric result on success, or the JS error value on error.

MDN documentation

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.