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
Spanof 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
impl Value
Sourcepub fn ext<T: Extension>(value: T) -> Value
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());Sourcepub fn into_parts(self) -> (Kind, Option<Meta>)
pub fn into_parts(self) -> (Kind, Option<Meta>)
Converts the value into its kind and meta data.
Sourcepub fn meta_mut(&mut self) -> &mut Meta
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.
Sourcepub fn span(&self) -> Option<&Span>
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.
Sourcepub fn event_data(&self) -> Option<&EventData>
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.
Methods from Deref<Target = Kind>§
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if this is null.
Extension values that fall back to null count as null.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the value of a number as f64.
Integers are converted, which can lose precision.
Sourcepub fn as_str(&self) -> Option<&str>
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.
Sourcepub fn as_seq_mut(&mut self) -> Option<&mut Seq>
pub fn as_seq_mut(&mut self) -> Option<&mut Seq>
Returns the sequence mutably.
Sourcepub fn as_map_mut(&mut self) -> Option<&mut Map>
pub fn as_map_mut(&mut self) -> Option<&mut Map>
Returns the map mutably.
Sourcepub fn downcast_ext<T: Extension>(&self) -> Option<&T>
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.
Sourcepub fn downcast_ext_value<K: BorrowedExtension>(&self) -> Option<&K::Value<'_>>
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");Sourcepub fn is_str(&self) -> bool
pub fn is_str(&self) -> bool
Returns true if this is a string.
This is also true for lexical values.
Sourcepub fn is_lexical(&self) -> bool
pub fn is_lexical(&self) -> bool
Returns true if this is a lexical value.
Sourcepub fn get<I: ValueIndex>(&self, index: I) -> Option<&Value>
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);Sourcepub fn get_mut<I: ValueIndex>(&mut self, index: I) -> Option<&mut Value>
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<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize_update<'out>(
value: &'out mut Self,
state: &mut State,
) -> SinkHandle<'out, 'de>
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>
fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
impl Eq for Value
Source§impl<I: ValueIndex> Index<I> for Value
impl<I: ValueIndex> Index<I> for Value
Source§impl<I: ValueIndex> IndexMut<I> for Value
impl<I: ValueIndex> IndexMut<I> for Value
Source§fn index_mut(&mut self, index: I) -> &mut Value
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.
impl MapKey for Value
Source§impl Serialize for Value
impl Serialize for Value
Source§fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>
Source§fn container_shape(value: &Self) -> ContainerShape
fn container_shape(value: &Self) -> ContainerShape
Source§fn is_optional(value: &Self) -> bool
fn is_optional(value: &Self) -> bool
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.