Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Undefined,
    Null,
    Bool(bool),
    Number(f64),
    String(String),
    Array(Rc<Vec<Value>>),
    Object(Rc<Map>),
}
Expand description

A JSON-like value.

Scalars are owned. Array and Object share their contents through Rc so an unchanged subtree can be returned by pointer.

Variants§

§

Undefined

JavaScript undefined. Absent root, removal sentinel, or a read-back sparse hole.

§

Null

JavaScript null. A real value, treated as a map for type coercion.

§

Bool(bool)

A boolean.

§

Number(f64)

A number. Carries the raw f64 so -0.0 and NaN survive.

§

String(String)

A string.

§

Array(Rc<Vec<Value>>)

An array. Sparse holes are stored as Undefined, since a JavaScript hole reads back as undefined.

§

Object(Rc<Map>)

A map of string keys to values.

Implementations§

Source§

impl Value

Source

pub fn is_undefined(&self) -> bool

Whether this is Undefined.

Source

pub fn as_array(&self) -> Option<&[Value]>

Borrow the array contents, if this is an array.

Source

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

Borrow the map contents, if this is an object.

Source

pub fn array(items: Vec<Value>) -> Value

Build an array value from items.

Source

pub fn object(map: Map) -> Value

Build a map value from a Map.

Source

pub fn from_pairs<K, I>(pairs: I) -> Value
where K: Into<String>, I: IntoIterator<Item = (K, Value)>,

Build a map value from key and value pairs, in order.

Convenience for tests and call sites that hold literal data.

Source

pub fn ptr_eq(&self, other: &Value) -> bool

Pointer equality for shared containers.

Returns true when both values are the same array or the same map by Rc pointer. This is the structural-sharing check: an untouched subtree returns its original pointer, so callers can detect “no change” cheaply. Scalars never share, so this returns false for them.

Source

pub fn same_value(&self, other: &Value) -> bool

SameValue equality, the Object.is algorithm.

Numbers follow SameValue: every NaN equals every NaN regardless of bit pattern, and +0.0 does not equal -0.0. Containers compare by pointer, which is what the engine’s no-op check needs. This is not deep equality. Two distinct arrays with equal contents are not SameValue equal.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 From<&str> for Value

Source§

fn from(s: &str) -> Value

Converts to this type from the input type.
Source§

impl From<Map> for Value

Source§

fn from(map: Map) -> Value

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(items: Vec<Value>) -> Value

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Value

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(n: f32) -> Value

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(n: f64) -> Value

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(n: i32) -> Value

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(n: u32) -> Value

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

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

Deep value equality.

Numbers follow SameValue, so NaN == NaN holds for any two NaN and tests can assert on it. This matches same_value on scalars and is deliberate. Do not switch the Number arm to plain ==, or NaN assertions break. Arrays and maps compare element by element, which is where this differs from same_value: two distinct arrays with equal contents are equal here but not under same_value. This is the equality assert_eq! uses in tests, not the engine’s no-op check.

1.0.0 (const: unstable) · 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.

Auto Trait Implementations§

§

impl !Send for Value

§

impl !Sync for Value

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Unpin for Value

§

impl UnsafeUnpin 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.