Enum zvariant::Value[][src]

pub enum Value<'a> {
    U8(u8),
    Bool(bool),
    I16(i16),
    U16(u16),
    I32(i32),
    U32(u32),
    I64(i64),
    U64(u64),
    F64(f64),
    Str(Str<'a>),
    Signature(Signature<'a>),
    ObjectPath(ObjectPath<'a>),
    Value(Box<Value<'a>>),
    Array(Array<'a>),
    Dict(Dict<'a, 'a>),
    Structure(Structure<'a>),
    Maybe(Maybe<'a>),
    Fd(Fd),
}

A generic container, in the form of an enum that holds exactly one value of any of the other types.

Note that this type correponds to the VARIANT data type defined by the D-Bus specification and as such, its encoding is not the same as that of the enclosed value.

Examples

use std::convert::TryFrom;
use zvariant::{from_slice, to_bytes, EncodingContext, Value};

// Create a Value from an i16
let v = Value::new(i16::max_value());

// Encode it
let ctxt = EncodingContext::<byteorder::LE>::new_dbus(0);
let encoding = to_bytes(ctxt, &v).unwrap();

// Decode it back
let v: Value = from_slice(&encoding, ctxt).unwrap();

// Check everything is as expected
assert_eq!(i16::try_from(&v).unwrap(), i16::max_value());

Now let’s try a more complicated example:

use std::convert::TryFrom;
use zvariant::{from_slice, to_bytes, EncodingContext};
use zvariant::{Structure, Value, Str};

// Create a Value from a tuple this time
let v = Value::new((i16::max_value(), "hello", true));

// Same drill as previous example
let ctxt = EncodingContext::<byteorder::LE>::new_dbus(0);
let encoding = to_bytes(ctxt, &v).unwrap();
let v: Value = from_slice(&encoding, ctxt).unwrap();

// Check everything is as expected
let s = Structure::try_from(v).unwrap();
assert_eq!(
    <(i16, Str, bool)>::try_from(s).unwrap(),
    (i16::max_value(), Str::from("hello"), true),
);

Variants

U8(u8)
Bool(bool)
I16(i16)
U16(u16)
I32(i32)
U32(u32)
I64(i64)
U64(u64)
F64(f64)
Str(Str<'a>)
Signature(Signature<'a>)
ObjectPath(ObjectPath<'a>)
Value(Box<Value<'a>>)
Array(Array<'a>)
Dict(Dict<'a, 'a>)
Structure(Structure<'a>)
Maybe(Maybe<'a>)
Fd(Fd)

Implementations

impl<'a> Value<'a>[src]

pub fn new<T>(value: T) -> Self where
    T: Into<Self> + Type
[src]

Make a Value for a given value.

In general, you can use Into trait on basic types, except when you explicitely need to wrap Value itself, in which case this constructor comes handy.

Examples

use zvariant::Value;

let s = Value::new("hello");
let u: Value = 51.into();
assert_ne!(s, u);

pub fn value_signature(&self) -> Signature<'_>[src]

Get the signature of the enclosed value.

pub fn downcast<T: ?Sized>(self) -> Option<T> where
    T: TryFrom<Value<'a>>, 
[src]

Try to get the underlying type T.

Note that TryFrom<Value> is implemented for various types, and it’s usually best to use that instead. However, in generic code where you also want to unwrap Value::Value, you should use this function (because TryFrom<Value> can not be implemented for Value itself as From<Value> is implicitly implemented for Value).

Examples

use std::convert::TryFrom;
use zvariant::{Result, Value};

fn value_vec_to_type_vec<'a, T>(values: Vec<Value<'a>>) -> Result<Vec<T>>
where
    T: TryFrom<Value<'a>>,
{
    let mut res = vec![];
    for value in values.into_iter() {
        res.push(value.downcast().unwrap());
    }

    Ok(res)
}

// Let's try u32 values first
let v = vec![Value::U32(42), Value::U32(43)];
let v = value_vec_to_type_vec::<u32>(v).unwrap();
assert_eq!(v[0], 42);
assert_eq!(v[1], 43);

// Now try Value values
let v = vec![Value::new(Value::U32(42)), Value::new(Value::U32(43))];
let v = value_vec_to_type_vec::<Value>(v).unwrap();
assert_eq!(v[0], Value::U32(42));
assert_eq!(v[1], Value::U32(43));

pub fn downcast_ref<T>(&'a self) -> Option<&'a T> where
    T: ?Sized,
    &'a T: TryFrom<&'a Value<'a>>, 
[src]

Try to get a reference to the underlying type T.

Same as downcast except it doesn’t consume self and get a reference to the underlying value.

Examples

use std::convert::TryFrom;
use zvariant::{Result, Value};

fn value_vec_to_type_vec<'a, T>(values: &'a Vec<Value<'a>>) -> Result<Vec<&'a T>>
where
    &'a T: TryFrom<&'a Value<'a>>,
{
    let mut res = vec![];
    for value in values.into_iter() {
        res.push(value.downcast_ref().unwrap());
    }

    Ok(res)
}

// Let's try u32 values first
let v = vec![Value::U32(42), Value::U32(43)];
let v = value_vec_to_type_vec::<u32>(&v).unwrap();
assert_eq!(*v[0], 42);
assert_eq!(*v[1], 43);

// Now try Value values
let v = vec![Value::new(Value::U32(42)), Value::new(Value::U32(43))];
let v = value_vec_to_type_vec::<Value>(&v).unwrap();
assert_eq!(*v[0], Value::U32(42));
assert_eq!(*v[1], Value::U32(43));

Trait Implementations

impl<'a> Clone for Value<'a>[src]

impl<'a> Debug for Value<'a>[src]

impl<'de: 'a, 'a> Deserialize<'de> for Value<'a>[src]

impl<'a> From<&'_ Value<'a>> for OwnedValue[src]

impl<'a> From<&'a &'a str> for Value<'a>[src]

impl<'a> From<&'a Array<'a>> for Value<'a>[src]

impl<'a> From<&'a Dict<'a, 'a>> for Value<'a>[src]

impl<'a> From<&'a Fd> for Value<'a>[src]

impl<'a> From<&'a Maybe<'a>> for Value<'a>[src]

impl<'a> From<&'a ObjectPath<'a>> for Value<'a>[src]

impl<'a> From<&'a Signature<'a>> for Value<'a>[src]

impl<'a> From<&'a bool> for Value<'a>[src]

impl<'a> From<&'a f32> for Value<'a>[src]

impl<'a> From<&'a f64> for Value<'a>[src]

impl<'a> From<&'a i16> for Value<'a>[src]

impl<'a> From<&'a i32> for Value<'a>[src]

impl<'a> From<&'a i64> for Value<'a>[src]

impl<'a> From<&'a i8> for Value<'a>[src]

impl<'a> From<&'a str> for Value<'a>[src]

impl<'a> From<&'a u16> for Value<'a>[src]

impl<'a> From<&'a u32> for Value<'a>[src]

impl<'a> From<&'a u64> for Value<'a>[src]

impl<'a> From<&'a u8> for Value<'a>[src]

impl<'v, V> From<&'v [V]> for Value<'v> where
    &'v [V]: Into<Array<'v>>, 
[src]

impl<'v> From<&'v String> for Value<'v>[src]

impl<'v, V> From<&'v Vec<V, Global>> for Value<'v> where
    &'v Vec<V>: Into<Array<'v>>, 
[src]

impl<'a> From<Array<'a>> for Value<'a>[src]

impl<'a> From<Dict<'a, 'a>> for Value<'a>[src]

impl<'a> From<Fd> for Value<'a>[src]

impl<'a, 'k, 'v, K, V> From<HashMap<K, V, RandomState>> for Value<'a> where
    'k: 'a,
    'v: 'a,
    K: Type + Into<Value<'k>> + Hash + Eq,
    V: Type + Into<Value<'k>>, 
[src]

impl<'a> From<Maybe<'a>> for Value<'a>[src]

impl<'a> From<ObjectPath<'a>> for Value<'a>[src]

impl<'v, V> From<Option<V>> for Value<'v> where
    Option<V>: Into<Maybe<'v>>, 
[src]

impl<'a> From<Signature<'a>> for Value<'a>[src]

impl<'s> From<String> for Value<'s>[src]

impl<'v, 's: 'v, T> From<T> for Value<'v> where
    T: Into<Structure<'s>>, 
[src]

impl<'a> From<Value<'a>> for OwnedValue[src]

impl<'v, V> From<Vec<V, Global>> for Value<'v> where
    Vec<V>: Into<Array<'v>>, 
[src]

impl<'a> From<bool> for Value<'a>[src]

impl<'a> From<f32> for Value<'a>[src]

impl<'a> From<f64> for Value<'a>[src]

impl<'a> From<i16> for Value<'a>[src]

impl<'a> From<i32> for Value<'a>[src]

impl<'a> From<i64> for Value<'a>[src]

impl<'a> From<i8> for Value<'a>[src]

impl<'a> From<u16> for Value<'a>[src]

impl<'a> From<u32> for Value<'a>[src]

impl<'a> From<u64> for Value<'a>[src]

impl<'a> From<u8> for Value<'a>[src]

impl<'a> PartialEq<Value<'a>> for Value<'a>[src]

impl<'a> Serialize for Value<'a>[src]

impl<'a> StructuralPartialEq for Value<'a>[src]

impl<'a> TryFrom<&'a Value<'a>> for &'a Fd[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Fd[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Dict<'a, 'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Dict<'a, 'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Array<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Array<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Maybe<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Maybe<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a str[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Str<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Str<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Signature<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Signature<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a ObjectPath<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for ObjectPath<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for &'a Structure<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<&'a Value<'a>> for Structure<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value<'_>> for OwnedObjectPath[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value<'_>> for OwnedSignature[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Fd[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Str<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Signature<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for ObjectPath<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Structure<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Dict<'a, 'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Array<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> TryFrom<Value<'a>> for Maybe<'a>[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'a> Type for Value<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Value<'a>

impl<'a> Send for Value<'a>

impl<'a> Sync for Value<'a>

impl<'a> Unpin for Value<'a>

impl<'a> UnwindSafe for Value<'a>

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> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[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.