Value

Enum 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>)

Some part of an Option

§Note

We use Box here to workaround recursive data type.

§

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 }.

Fields

§name: &'static str
§variant_index: u32
§variant: &'static str
§

NewtypeStruct(&'static str, Box<Value>)

For example struct Millimeters(u8).

§

NewtypeVariant

For example the E::N in enum E { N(u8) }.

§Note

We use Box here to workaround recursive data type.

Fields

§name: &'static str
§variant_index: u32
§variant: &'static str
§value: Box<Value>
§

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) }.

Fields

§name: &'static str
§variant_index: u32
§variant: &'static str
§fields: Vec<Value>
§

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 } }.

Fields

§name: &'static str
§variant_index: u32
§variant: &'static str
§fields: IndexMap<&'static str, Value>

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · 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<'de> Deserialize<'de> for Value

Source§

fn deserialize<D>(d: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Value> for Deserializer

Source§

fn from(v: Value) -> Self

Converts to this type from the input type.
Source§

impl Hash for Value

Implement Hash for Value so that we can use value as hash key.

§Notes

Not all variants supports hash.

§FIXME

does this implementation correct?

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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.
Source§

impl Serialize for Value

Implement transparent serde::Serialize for Value.

Serialize on Value that converted from T will be the same with T.

Source§

fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Value

Source§

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> 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<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> FromValue for T

Source§

fn from_value(v: Value) -> Result<T, Error>

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> IntoValue for T
where T: Serialize,

Source§

fn into_value(self) -> Result<Value, Error>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,