pub enum Value {
Show 30 variants
Bool(bool),
I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
F32(f32),
F64(f64),
Char(char),
Str(String),
Bytes(Vec<u8>),
None,
Some(Box<Value>),
Unit,
UnitStruct(&'static str),
UnitVariant {
name: &'static str,
variant_index: u32,
variant: &'static str,
},
NewtypeStruct(&'static str, Box<Value>),
NewtypeVariant {
name: &'static str,
variant_index: u32,
variant: &'static str,
value: Box<Value>,
},
Seq(Vec<Value>),
Tuple(Vec<Value>),
TupleStruct(&'static str, Vec<Value>),
TupleVariant {
name: &'static str,
variant_index: u32,
variant: &'static str,
fields: Vec<Value>,
},
Map(IndexMap<Value, Value>),
Struct(&'static str, IndexMap<&'static str, Value>),
StructVariant {
name: &'static str,
variant_index: u32,
variant: &'static str,
fields: IndexMap<&'static str, Value>,
},
}Expand description
Value is the internal represents of serde’s data format.
Value is the one-to-one map to serde’s data format.
Theoretically, Value can be converted from/to any serde format.
Value is a inter data represents which means:
Value should be constructed or consumed by into_value or from_value.
t.into_value()->Valueinto_value(t)->ValueT::from_value(v)->Tfrom_value(v)->T
Value also implements serde::Serialize and serde::Deserialize.
Serialize and Deserialize on Value that converted from T will be the same with T.
§Examples
§Conversion between T and Value
use anyhow::Result;
use serde_bridge::{from_value, into_value, FromValue, IntoValue, Value};
fn main() -> Result<()> {
let v = bool::from_value(Value::Bool(true))?;
assert!(v);
let v: bool = from_value(Value::Bool(true))?;
assert!(v);
let v = true.into_value()?;
assert_eq!(v, Value::Bool(true));
let v = into_value(true)?;
assert_eq!(v, Value::Bool(true));
Ok(())
}§Transparent Serialize and Deserialize
use anyhow::Result;
use serde_bridge::{from_value, into_value, FromValue, IntoValue, Value};
use serde_json;
fn main() -> Result<()> {
let raw = serde_json::to_string(&true)?;
let value = serde_json::to_string(&true.into_value()?)?;
assert_eq!(raw, value);
let raw: bool = serde_json::from_str("true")?;
let value: Value = serde_json::from_str("true")?;
assert_eq!(raw, bool::from_value(value)?);
Ok(())
}Variants§
Bool(bool)
primitive types for bool: false/true
I8(i8)
primitive types for i8
I16(i16)
primitive types for i16
I32(i32)
primitive types for i32
I64(i64)
primitive types for i64
I128(i128)
primitive types for i128
U8(u8)
primitive types for u8
U16(u16)
primitive types for u16
U32(u32)
primitive types for u32
U64(u64)
primitive types for u64
U128(u128)
primitive types for u128
F32(f32)
primitive types for f32
F64(f64)
primitive types for f64
Char(char)
primitive types for char
Str(String)
string type
UTF-8 bytes with a length and no null terminator. May contain 0-bytes.
Bytes(Vec<u8>)
byte array
Similar to strings, during deserialization byte arrays can be transient, owned, or borrowed.
None
None part of an Option
Some(Box<Value>)
Unit
The type of () in Rust.
It represents an anonymous value containing no data.
UnitStruct(&'static str)
For example struct Unit or PhantomData<T>.
It represents a named value containing no data.
UnitVariant
For example the E::A and E::B in enum E { A, B }.
NewtypeStruct(&'static str, Box<Value>)
For example struct Millimeters(u8).
NewtypeVariant
Seq(Vec<Value>)
A variably sized heterogeneous sequence of values, for example Vec<T> or HashSet<T>
Tuple(Vec<Value>)
A statically sized heterogeneous sequence of values for which the length will be known at deserialization time without looking at the serialized data.
For example (u8,) or (String, u64, Vec<T>) or [u64; 10].
TupleStruct(&'static str, Vec<Value>)
A named tuple, for example struct Rgb(u8, u8, u8).
TupleVariant
For example the E::T in enum E { T(u8, u8) }.
Map(IndexMap<Value, Value>)
A variably sized heterogeneous key-value pairing, for example BTreeMap<K, V>
Struct(&'static str, IndexMap<&'static str, Value>)
A statically sized heterogeneous key-value pairing in which the keys are compile-time constant strings and will be known at deserialization time without looking at the serialized data.
For example struct S { r: u8, g: u8, b: u8 }.
StructVariant
For example the E::S in enum E { S { r: u8, g: u8, b: u8 } }.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D>(d: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(d: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<Value> for Deserializer
impl From<Value> for Deserializer
Source§impl Serialize for Value
Implement transparent serde::Serialize for Value.
impl Serialize for Value
Implement transparent serde::Serialize for Value.
Serialize on Value that converted from T will be the same with T.
impl Eq for Value
impl StructuralPartialEq for Value
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> 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,
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.