Enum quickjs_wasm_rs::JSValue

source ·
pub enum JSValue {
    Undefined,
    Null,
    Bool(bool),
    Int(i32),
    Float(f64),
    String(String),
    Array(Vec<JSValue>),
    ArrayBuffer(Vec<u8>),
    Object(HashMap<String, JSValue>),
}
Expand description

A safe and high level representation of a JavaScript value.

This enum implements From and TryFrom for many types, so it can be used to convert between Rust and JavaScript types.

§Example

// Convert a &str to a JSValue::String
let js_value: JSValue = "hello".into();
assert_eq!("hello", js_value.to_string());

// Convert a JSValue::String to a String
let result: String = js_value.try_into().unwrap();
assert_eq!("hello", result);

Variants§

§

Undefined

Represents the JavaScript undefined value

§

Null

Represents the JavaScript null value

§

Bool(bool)

Represents a JavaScript boolean value

§

Int(i32)

Represents a JavaScript integer

§

Float(f64)

Represents a JavaScript floating-point number

§

String(String)

Represents a JavaScript string value

§

Array(Vec<JSValue>)

Represents a JavaScript array of JSValues

§

ArrayBuffer(Vec<u8>)

Represents a JavaScript ArrayBuffer of bytes

§

Object(HashMap<String, JSValue>)

Represents a JavaScript object, with string keys and JSValue values

Implementations§

source§

impl JSValue

source

pub fn from_vec<T: Into<JSValue>>(v: Vec<T>) -> JSValue

Constructs a JSValue::Array variant from a vec of items that can be converted to JSValue.

§Arguments
  • v - A vec of items to be converted to JSValue::Array
§Example
let vec = vec![1, 2, 3];
let js_arr = JSValue::from_vec(vec);
source

pub fn from_hashmap<T: Into<JSValue>>(hm: HashMap<&str, T>) -> JSValue

Constructs a JSValue::Object variant from a HashMap of key-value pairs that can be converted to JSValue.

§Arguments
  • hm - A HashMap of key-value pairs to be converted to JSValue::Object
§Example
let mut hashmap = std::collections::HashMap::from([
  ("first_name", "John"),
  ("last_name", "Smith"),
]);

let js_obj = JSValue::from_hashmap(hashmap);

Trait Implementations§

source§

impl Clone for JSValue

source§

fn clone(&self) -> JSValue

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for JSValue

source§

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

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

impl Display for JSValue

The implementation matches the default JavaScript display format for each value.

Used http://numcalc.com/ to determine the default display format for each type.

source§

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

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

impl From<&[u8]> for JSValue

source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
source§

impl From<&str> for JSValue

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl From<HashMap<String, JSValue>> for JSValue

source§

fn from(value: HashMap<String, JSValue>) -> Self

Converts to this type from the input type.
source§

impl From<String> for JSValue

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl From<Vec<JSValue>> for JSValue

source§

fn from(value: Vec<JSValue>) -> Self

Converts to this type from the input type.
source§

impl From<Vec<u8>> for JSValue

source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl From<bool> for JSValue

source§

fn from(value: bool) -> Self

Converts to this type from the input type.
source§

impl From<f64> for JSValue

source§

fn from(value: f64) -> Self

Converts to this type from the input type.
source§

impl From<i32> for JSValue

source§

fn from(value: i32) -> Self

Converts to this type from the input type.
source§

impl From<usize> for JSValue

source§

fn from(value: usize) -> Self

Converts to this type from the input type.
source§

impl PartialEq for JSValue

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<JSValue> for HashMap<String, JSValue>

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for String

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for Vec<JSValue>

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for Vec<u8>

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for bool

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for f64

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for i32

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<JSValue> for usize

§

type Error = Error

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

fn try_from(value: JSValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for JSValue

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

§

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§

default 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>,

§

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>,

§

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.