Enum zvariant::Value[][src]

pub enum Value<'a> {
Show 18 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),
}
Expand description

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

Note that this type corresponds 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)

Tuple Fields

0: u8

Bool(bool)

Tuple Fields

0: bool

I16(i16)

Tuple Fields

0: i16

U16(u16)

Tuple Fields

0: u16

I32(i32)

Tuple Fields

0: i32

U32(u32)

Tuple Fields

0: u32

I64(i64)

Tuple Fields

0: i64

U64(u64)

Tuple Fields

0: u64

F64(f64)

Tuple Fields

0: f64

Str(Str<'a>)

Tuple Fields

0: Str<'a>

Signature(Signature<'a>)

Tuple Fields

0: Signature<'a>

ObjectPath(ObjectPath<'a>)

Tuple Fields

0: ObjectPath<'a>

Value(Box<Value<'a>>)

Tuple Fields

0: Box<Value<'a>>

Array(Array<'a>)

Tuple Fields

0: Array<'a>

Dict(Dict<'a, 'a>)

Tuple Fields

0: Dict<'a, 'a>

Structure(Structure<'a>)

Tuple Fields

0: Structure<'a>

Maybe(Maybe<'a>)

Tuple Fields

0: Maybe<'a>

Fd(Fd)

Tuple Fields

0: Fd

Implementations

Make a Value for a given value.

In general, you can use Into trait on basic types, except when you explicitly 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);

Get the signature of the enclosed value.

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Get the signature for the implementing type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.