[][src]Enum packs::Value

pub enum Value<S> {
    Null,
    Boolean(bool),
    Integer(i64),
    Float(f64),
    Bytes(Bytes),
    String(String),
    List(Vec<Value<S>>),
    Dictionary(Dictionary<S>),
    Structure(S),
}

A type for all possible values which can be serialized through PackStream. This type abstracts over structure types, which allows the user to define their own structures which should be part of Value. There are two standard implementations, either Value<NoStruct> to denote a value where nothing further allowed as a structure, or Value<GenericStruct> which reads any valid structure generic, see GenericStruct.

In conjunction with std_structs one can use Value<StdStruct> to use the default structs as given by the PackStream specification.

Creating a Value

For once, a Value is just a sum type, so by choosing an appropiated case, one can construct a Value directly, e.g. Value::Integer(3). But besides, there are either possibilities to use the From implementation for some types or collecting items into a Value::List or Value::Dictionary:

use packs::*;

// collect a list of values which can be converted to values
// into `Value::List`:
let value : Value<()> =
    vec!(42i64, 0, 1).into_iter().collect();

assert_eq!(
    value,
    Value::List(
        vec!(Value::Integer(42),
             Value::Integer(0),
             Value::Integer(1))));

This is also possible with pairs which are then get collected into Value::Dictionary:

use packs::*;

// collect into a dictionary:
let value : Value<()> =
    vec!(
        (String::from("name"), <&str as Into<Value<()>>>::into("Jane Doe")),
        (String::from("age"), 42.into()),
   ).into_iter().collect();

assert_eq!(
    value,
    Value::Dictionary(
        vec!(
            (String::from("name"), String::from("Jane Doe").into()),
            (String::from("age"), 42.into()),
        ).into_iter().collect()
    ));

Extracting

A Value can be extracted into a supported type by using the Extract traits (there are variants for borrowed and mutably borrowed values). This checks on the value and tries to recover the inner type from it:

use packs::*;

let value : Value<()> = Value::Float(42.42);
assert_eq!(f64::extract(value).unwrap(), 42.42);

The Null

PackStream has Value::Null as a possible value, denoting the absence of a value. From within Rust, this transforms any Value -> T into a Value -> Option<T>, returning None on Value::Null; but since this is only one way to treat Value::Null this package does not decide but provides different ways to extract values:

  1. The trait Extract<T> has an implementation for Option<E: Extract<T>> to treat Value::Null as None.
  2. The traits ExtractMut<T> and ExtractRef<T> provide functions with default implementations to extract any Value::Null as a None and treat every other v as Some(v).
  3. Otherwise, any extract of Value::Null will fail with None.
use packs::*;

// case 1:
assert_eq!(<Option<bool>>::extract(<Value<NoStruct>>::Boolean(true)), Some(Some(true)));
assert_eq!(<Option<bool>>::extract(<Value<NoStruct>>::Null), Some(None));

// case 2:
assert_eq!(<bool>::extract_opt_ref(&<Value<NoStruct>>::Boolean(true)), Some(Some(&true)));
assert_eq!(<bool>::extract_opt_ref(&<Value<NoStruct>>::Null), Some(None));

// case 3:
assert_eq!(<bool>::extract_ref(&<Value<NoStruct>>::Null), None)

Variants

Null
Boolean(bool)
Integer(i64)
Float(f64)
Bytes(Bytes)
String(String)
List(Vec<Value<S>>)
Dictionary(Dictionary<S>)
Structure(S)

Trait Implementations

impl<S: Clone> Clone for Value<S>[src]

impl<S: Debug> Debug for Value<S>[src]

impl<S, '_> From<&'_ str> for Value<S>[src]

impl<S, V: Into<Value<S>>> From<Option<V>> for Value<S>[src]

impl<S> From<String> for Value<S>[src]

impl<S> From<Vec<Value<S>>> for Value<S>[src]

impl<S> From<bool> for Value<S>[src]

impl<S> From<f64> for Value<S>[src]

impl<S> From<i64> for Value<S>[src]

impl<S, V: Into<Value<S>>> FromIterator<(String, V)> for Value<S>[src]

impl<S, V: Into<Value<S>>> FromIterator<V> for Value<S>[src]

impl<S: Pack> Pack for Value<S>[src]

impl<S: PartialEq> PartialEq<Value<S>> for Value<S>[src]

impl<S> StructuralPartialEq for Value<S>[src]

impl<S: Unpack> Unpack for Value<S>[src]

Auto Trait Implementations

impl<S> RefUnwindSafe for Value<S> where
    S: RefUnwindSafe

impl<S> Send for Value<S> where
    S: Send

impl<S> Sync for Value<S> where
    S: Sync

impl<S> Unpin for Value<S> where
    S: Unpin

impl<S> UnwindSafe for Value<S> where
    S: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.