Enum serde_bridge::Value
source · [−]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()
->Value
into_value(t)
->Value
T::from_value(v)
->T
from_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
sourceimpl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Hash for Value
impl Hash for Value
sourceimpl 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 RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more