Skip to main content

Value

Struct Value 

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

A dynamic value.

A value is made of its Kind, which holds the actual data, and optional Meta data. The value dereferences to its kind, which is where the accessors are defined and what is matched on:

use deser_value::{value, Kind};

let value = value!({"name": "Jane", "roles": ["admin"]});
assert_eq!(value["name"].as_str(), Some("Jane"));

match &*value["roles"] {
    Kind::Seq(roles) => assert_eq!(roles.len(), 1),
    _ => unreachable!(),
}

§Meta Data

Values retain information that is not part of the data model. This is held in the Meta of a value, which is only allocated if there is such information:

  • The event data of the value, for instance CBOR tags or formatting hints. It’s captured when the value is deserialized and attached again when it’s serialized.
  • The Span of the value in the input, if the format tracks locations. Values that are deserialized from a value with spans report errors at the original location.

Meta data is ignored when values are compared or hashed and it’s not shown in the debug output.

§Numbers

Integers are held as Kind::U64 and Kind::I64. Values created by this crate hold non-negative integers as U64 and integers which do not fit into 64 bits (u128 and i128) as Kind::Ext. Integers compare by their value: I64(1) is equal to U64(1). Floats compare by their bits, which means that NaN is equal to itself and -0.0 is not equal to 0.0.

§Extensions

Values which extend the data model (see deser::ext) are held as Kind::Ext. They retain their type and are serialized as extension values again. The accessors (such as as_str) look at them through their fallback.

§Nesting

All operations on values (including dropping, cloning, comparing and formatting) are implemented without recursion, deeply nested values do not overflow the stack.

Implementations§

Source§

impl Value

Source

pub const fn null() -> Value

Creates a null value.

Source

pub const fn new(kind: Kind) -> Value

Creates a value from its kind.

Source

pub fn ext<T: Extension>(value: T) -> Value

Creates a value holding an extension value.

use deser::ext::Uuid;
use deser_value::Value;

let value = Value::ext(Uuid([0; 16]));
assert!(value.downcast_ext::<Uuid>().is_some());
Source

pub fn bytes<B: Into<Vec<u8>>>(data: B) -> Value

Creates a value holding bytes.

Source

pub fn kind(&self) -> &Kind

Returns the kind of the value.

Source

pub fn kind_mut(&mut self) -> &mut Kind

Returns the kind of the value mutably.

Source

pub fn into_kind(self) -> Kind

Converts the value into its kind, discarding the meta data.

Source

pub fn into_parts(self) -> (Kind, Option<Meta>)

Converts the value into its kind and meta data.

Source

pub fn meta(&self) -> Option<&Meta>

Returns the meta data of the value, if there is any.

Source

pub fn meta_mut(&mut self) -> &mut Meta

Returns the meta data of the value mutably.

Empty meta data is created if the value has none.

Source

pub fn take_meta(&mut self) -> Option<Meta>

Removes the meta data of the value and returns it.

Source

pub fn set_meta(&mut self, meta: Option<Meta>)

Sets the meta data of the value.

Source

pub fn span(&self) -> Option<&Span>

Returns the span of the value in the input.

This is a shortcut for the span of the Meta.

Source

pub fn event_data(&self) -> Option<&EventData>

Returns the event data of the value.

This is a shortcut for the event data of the Meta.

Source

pub fn take(&mut self) -> Value

Takes the value out, leaving null in its place.

Methods from Deref<Target = Kind>§

Source

pub fn name(&self) -> &str

Returns the human readable name of the kind.

Source

pub fn is_null(&self) -> bool

Returns true if this is null.

Extension values that fall back to null count as null.

Source

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

Returns the value of a bool.

Source

pub fn as_u64(&self) -> Option<u64>

Returns the value of an integer if it fits into u64.

Source

pub fn as_i64(&self) -> Option<i64>

Returns the value of an integer if it fits into i64.

Source

pub fn as_u128(&self) -> Option<u128>

Returns the value of an integer if it fits into u128.

Source

pub fn as_i128(&self) -> Option<i128>

Returns the value of an integer if it fits into i128.

Source

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

Returns the value of a number as f64.

Integers are converted, which can lose precision.

Source

pub fn as_char(&self) -> Option<char>

Returns the value of a char.

Source

pub fn as_str(&self) -> Option<&str>

Returns the value of a string.

For extension values this returns the fallback if it’s a string that borrows from the value.

Source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the data of bytes.

Source

pub fn as_seq(&self) -> Option<&Seq>

Returns the sequence.

Source

pub fn as_seq_mut(&mut self) -> Option<&mut Seq>

Returns the sequence mutably.

Source

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

Returns the map.

Source

pub fn as_map_mut(&mut self) -> Option<&mut Map>

Returns the map mutably.

Source

pub fn as_ext(&self) -> Option<&ExtValue<'static>>

Returns the extension value.

Source

pub fn downcast_ext<T: Extension>(&self) -> Option<&T>

Returns the extension value if it’s of type T.

For extensions that borrow, use downcast_ext_value.

Source

pub fn downcast_ext_value<K: BorrowedExtension>(&self) -> Option<&K::Value<'_>>

Returns the extension value if it’s of the extension with the key K.

See BorrowedExtension.

use deser::ext::Number;
use deser_value::Value;

let value: Value =
    deser_json::from_str("0.10000000000000000001").unwrap();
let number = value.downcast_ext_value::<Number>().unwrap();
assert_eq!(number.as_str(), "0.10000000000000000001");
Source

pub fn is_str(&self) -> bool

Returns true if this is a string.

This is also true for lexical values.

Source

pub fn is_lexical(&self) -> bool

Returns true if this is a lexical value.

Source

pub fn is_seq(&self) -> bool

Returns true if this is a sequence.

Source

pub fn is_map(&self) -> bool

Returns true if this is a map.

Source

pub fn get<I: ValueIndex>(&self, index: I) -> Option<&Value>

Looks up a value in a sequence (by index) or a map (by key).

use deser_value::value;

let value = value!({"items": [1, 2], 42: "answer"});
assert_eq!(value.get("items").and_then(|x| x.get(1)), Some(&value!(2)));
assert_eq!(value.get(&value!(42)), Some(&value!("answer")));
assert_eq!(value.get("missing"), None);
Source

pub fn get_mut<I: ValueIndex>(&mut self, index: I) -> Option<&mut Value>

Looks up a value in a sequence (by index) or a map (by key) mutably.

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 Default for Value

Source§

fn default() -> Value

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

impl Deref for Value

Source§

type Target = Kind

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Kind

Dereferences the value.
Source§

impl DerefMut for Value

Source§

fn deref_mut(&mut self) -> &mut Kind

Mutably dereferences the value.
Source§

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize_update<'out>( value: &'out mut Self, state: &mut State, ) -> SinkHandle<'out, 'de>

Updates the value: a map merges the data into it if it’s a map (the values of keys that exist are replaced), all other values are replaced.

Source§

fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>

Creates a sink that deserializes the value into the given slot. Read more
Source§

fn expecting() -> Cow<'static, str>

Returns what the value expects, for error messages. Read more
Source§

fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom into the slot. Read more
Source§

fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

fn describe_type(d: &mut dyn Describe)

Describes the Rust shape of the type. Read more
Source§

fn initial_value() -> Option<T>

Provides the value of a missing struct field. Read more
Source§

impl Eq for Value

Source§

impl From<&String> for Value

Source§

fn from(value: &String) -> Value

Converts to this type from the input type.
Source§

impl<T: Clone + Into<Value>> From<&[T]> for Value

Source§

fn from(value: &[T]) -> Value

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(value: &str) -> Value

Converts to this type from the input type.
Source§

impl From<()> for Value

Source§

fn from(_: ()) -> Value

Converts to this type from the input type.
Source§

impl From<Bytes<'_>> for Value

Source§

fn from(value: Bytes<'_>) -> Value

Converts to this type from the input type.
Source§

impl From<Cow<'_, str>> for Value

Source§

fn from(value: Cow<'_, str>) -> Value

Converts to this type from the input type.
Source§

impl From<ExtValue<'_>> for Value

Source§

fn from(value: ExtValue<'_>) -> Value

Creates a value from an extension value.

Integers of extensions (u128 and i128) which fit into 64 bits are converted into U64 and I64.

Source§

impl From<Kind> for Value

Source§

fn from(kind: Kind) -> Value

Converts to this type from the input type.
Source§

impl From<Map> for Value

Source§

fn from(value: Map) -> Value

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Option<T>> for Value

Source§

fn from(value: Option<T>) -> Value

Converts to this type from the input type.
Source§

impl From<Seq> for Value

Source§

fn from(value: Seq) -> Value

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(value: String) -> Value

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Vec<T>> for Value

Source§

fn from(value: Vec<T>) -> Value

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(value: bool) -> Value

Converts to this type from the input type.
Source§

impl From<char> for Value

Source§

fn from(value: char) -> Value

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(value: f32) -> Value

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(value: f64) -> Value

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(value: i8) -> Value

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(value: i16) -> Value

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(value: i32) -> Value

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(value: i64) -> Value

Converts to this type from the input type.
Source§

impl From<i128> for Value

Source§

fn from(value: i128) -> Value

Converts to this type from the input type.
Source§

impl From<isize> for Value

Source§

fn from(value: isize) -> Value

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(value: u8) -> Value

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(value: u16) -> Value

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(value: u32) -> Value

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(value: u64) -> Value

Converts to this type from the input type.
Source§

impl From<u128> for Value

Source§

fn from(value: u128) -> Value

Converts to this type from the input type.
Source§

impl From<usize> for Value

Source§

fn from(value: usize) -> Value

Converts to this type from the input type.
Source§

impl<K: Into<Value>, V: Into<Value>> FromIterator<(K, V)> for Value

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Value

Creates a value from an iterator. Read more
Source§

impl<T: Into<Value>> FromIterator<T> for Value

Source§

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

Creates a value from an iterator. Read more
Source§

impl Hash for Value

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I: ValueIndex> Index<I> for Value

Source§

fn index(&self, index: I) -> &Value

Looks up a value in a sequence (by index) or a map (by key).

Returns null if the value does not exist.

Source§

type Output = Value

The returned type after indexing.
Source§

impl<I: ValueIndex> IndexMut<I> for Value

Source§

fn index_mut(&mut self, index: I) -> &mut Value

Looks up a value in a sequence (by index) or a map (by key) mutably.

Keys that do not exist are inserted with null and a null value turns into a map if it’s indexed by a key.

§Panics

Panics if the index is out of bounds, or if the value cannot be indexed by the index.

Source§

impl MapKey for Value

Source§

impl PartialEq for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<&str> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<String> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<bool> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<f32> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<f64> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<i8> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<i16> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<i32> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<i64> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<isize> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<str> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<u8> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<u16> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<u32> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<u64> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<usize> for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Value

Source§

fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn container_shape(value: &Self) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

fn is_optional(value: &Self) -> bool

Checks if the value represents an optional value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

impl ValueIndex for Value

Auto Trait Implementations§

§

impl !RefUnwindSafe for Value

§

impl !UnwindSafe for Value

§

impl Freeze for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.