[−][src]Enum packs::Value
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:
- The trait
Extract<T>
has an implementation forOption<E: Extract<T>>
to treatValue::Null
asNone
. - The traits
ExtractMut<T>
andExtractRef<T>
provide functions with default implementations to extract anyValue::Null
as aNone
and treat every otherv
asSome(v)
. - Otherwise, any extract of
Value::Null
will fail withNone
.
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
Boolean(bool)
Integer(i64)
Float(f64)
Bytes(Bytes)
String(String)
Dictionary(Dictionary<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]
pub fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self
[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]
pub fn decode_body<T: Read>(
marker: Marker,
reader: &mut T
) -> Result<Self, DecodeError>
[src]
marker: Marker,
reader: &mut T
) -> Result<Self, DecodeError>
pub fn decode<T: Read>(reader: &mut T) -> Result<Self, DecodeError>
[src]
Auto Trait Implementations
impl<S> RefUnwindSafe for Value<S> where
S: RefUnwindSafe,
S: RefUnwindSafe,
impl<S> Send for Value<S> where
S: Send,
S: Send,
impl<S> Sync for Value<S> where
S: Sync,
S: Sync,
impl<S> Unpin for Value<S> where
S: Unpin,
S: Unpin,
impl<S> UnwindSafe for Value<S> where
S: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,