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.
use zvariant::{to_bytes, serialized::Context, Value, LE};
// Create a Value from an i16
let v = Value::new(i16::max_value());
// Encode it
let ctxt = Context::new_dbus(LE, 0);
let encoding = to_bytes(ctxt, &v).unwrap();
// Decode it back
let v: Value = encoding.deserialize().unwrap().0;
// Check everything is as expected
assert_eq!(i16::try_from(&v).unwrap(), i16::max_value());
Now let’s try a more complicated example:
use zvariant::{to_bytes, serialized::Context, LE};
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 = Context::new_dbus(LE, 0);
let encoding = to_bytes(ctxt, &v).unwrap();
let v: Value = encoding.deserialize().unwrap().0;
// 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),
);
This method can currently only fail on Unix platforms for Value::Fd variant. This
happens when the current process exceeds the maximum number of open file descriptors.
This method can currently only fail on Unix platforms for Value::Fd variant containing
an Fd::Owned variant. This happens when the current process exceeds the maximum number
of open file descriptors.
Results in an extra allocation if the value contains borrowed data.
This method can currently only fail on Unix platforms for Value::Fd variant containing
an Fd::Owned variant. This happens when the current process exceeds the maximum number
of open file descriptors.
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).
use zvariant::{Error, Result, Value};
fn value_vec_to_type_vec<'a, T>(values: Vec<Value<'a>>) -> Result<Vec<T>>
where
T: TryFrom<Value<'a>>,
<T as TryFrom<Value<'a>>>::Error: Into<Error>,
{
let mut res = vec![];
for value in values.into_iter() {
res.push(value.downcast()?);
}
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));
use zvariant::{Error, 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>>,
<&'a T as TryFrom<&'a Value<'a>>>::Error: Into<Error>,
{
let mut res = vec![];
for value in values.into_iter() {
res.push(value.downcast_ref()?);
}
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));
This method can only fail on Unix platforms for Value::Fd variant containing an
Fd::Owned variant. This happens when the current process exceeds the limit on maximum
number of open file descriptors.