Enum Value

Source
pub enum Value {
    Undefined,
    Null,
    Boolean(bool),
    Number(f64),
    Date(f64),
    String(String),
    Array(Array),
    Function(Function),
    Object(Object),
}
Expand description

A JavaScript value.

Values can either hold direct values (undefined, null, booleans, numbers, dates) or references (strings, arrays, functions, other objects). Cloning values (via Rust’s Clone) of the direct types defers to Rust’s Copy, while cloning values of the referential types results in a simple reference clone similar to JavaScript’s own “by-reference” semantics.

Variants§

§

Undefined

The JavaScript value undefined.

§

Null

The JavaScript value null.

§

Boolean(bool)

The JavaScript value true or false.

§

Number(f64)

A JavaScript floating point number.

§

Date(f64)

Elapsed milliseconds since Unix epoch.

§

String(String)

An immutable JavaScript string, managed by V8.

§

Array(Array)

Reference to a JavaScript arrray.

§

Function(Function)

Reference to a JavaScript function.

§

Object(Object)

Reference to a JavaScript object. If a value is a function or an array in JavaScript, it will be converted to Value::Array or Value::Function instead of Value::Object.

Implementations§

Source§

impl Value

Source

pub fn is_undefined(&self) -> bool

Returns true if this is a Value::Undefined, false otherwise.

Source

pub fn is_null(&self) -> bool

Returns true if this is a Value::Null, false otherwise.

Source

pub fn is_boolean(&self) -> bool

Returns true if this is a Value::Boolean, false otherwise.

Source

pub fn is_number(&self) -> bool

Returns true if this is a Value::Number, false otherwise.

Source

pub fn is_date(&self) -> bool

Returns true if this is a Value::Date, false otherwise.

Source

pub fn is_string(&self) -> bool

Returns true if this is a Value::String, false otherwise.

Source

pub fn is_array(&self) -> bool

Returns true if this is a Value::Array, false otherwise.

Source

pub fn is_function(&self) -> bool

Returns true if this is a Value::Function, false otherwise.

Source

pub fn is_object(&self) -> bool

Returns true if this is a Value::Object, false otherwise.

Source

pub fn as_undefined(&self) -> Option<()>

Returns Some(()) if this is a Value::Undefined, None otherwise.

Source

pub fn as_null(&self) -> Option<()>

Returns Some(()) if this is a Value::Null, None otherwise.

Source

pub fn as_boolean(&self) -> Option<bool>

Returns Some if this is a Value::Boolean, None otherwise.

Source

pub fn as_number(&self) -> Option<f64>

Returns Some if this is a Value::Number, None otherwise.

Source

pub fn as_date(&self) -> Option<f64>

Returns Some if this is a Value::Date, None otherwise.

Source

pub fn as_string(&self) -> Option<&String>

Returns Some if this is a Value::String, None otherwise.

Source

pub fn as_array(&self) -> Option<&Array>

Returns Some if this is a Value::Array, None otherwise.

Source

pub fn as_function(&self) -> Option<&Function>

Returns Some if this is a Value::Function, None otherwise.

Source

pub fn as_object(&self) -> Option<&Object>

Returns Some if this is a Value::Object, None otherwise.

Source

pub fn into<T: FromValue>(self, mv8: &MiniV8) -> Result<T>

A wrapper around FromValue::from_value.

Source

pub fn coerce_boolean(&self, mv8: &MiniV8) -> bool

Coerces a value to a boolean. Returns true if the value is “truthy”, false otherwise.

Source

pub fn coerce_number(&self, mv8: &MiniV8) -> Result<f64>

Coerces a value to a number. Nearly all JavaScript values are coercible to numbers, but this may fail with a runtime error under extraordinary circumstances (e.g. if the ECMAScript ToNumber implementation throws an error).

This will return std::f64::NAN if the value has no numerical equivalent.

Source

pub fn coerce_string(&self, mv8: &MiniV8) -> Result<String>

Coerces a value to a string. Nearly all JavaScript values are coercible to strings, but this may fail with a runtime error if toString() fails or under otherwise extraordinary circumstances (e.g. if the ECMAScript ToString implementation throws an error).

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate 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 Value

Source§

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

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

impl FromIterator<Value> for Values

Source§

fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl FromValue for Value

Source§

fn from_value(value: Value, _mv8: &MiniV8) -> Result<Self>

Performs the conversion.
Source§

impl ToValue for Value

Source§

fn to_value(self, _mv8: &MiniV8) -> Result<Value>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Value

§

impl !RefUnwindSafe for Value

§

impl !Send for Value

§

impl !Sync for Value

§

impl Unpin for Value

§

impl !UnwindSafe for Value

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