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<'a>),
}
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 zvariant::{to_bytes, serialized::Context, Value, LE};
let v = Value::new(i16::max_value());
let ctxt = Context::new_dbus(LE, 0);
let encoding = to_bytes(ctxt, &v).unwrap();
let v: Value = encoding.deserialize().unwrap().0;
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};
let v = Value::new((i16::max_value(), "hello", true));
let ctxt = Context::new_dbus(LE, 0);
let encoding = to_bytes(ctxt, &v).unwrap();
let v: Value = encoding.deserialize().unwrap().0;
let s = Structure::try_from(v).unwrap();
assert_eq!(
<(i16, Str, bool)>::try_from(s).unwrap(),
(i16::max_value(), Str::from("hello"), true),
);
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);
Try to create an owned version of self
.
§Errors
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.
Get the signature of the enclosed value.
Try to clone the value.
§Errors
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.
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 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 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);
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 the underlying type T
.
Same as downcast
except it doesn’t consume self
and hence requires
T: TryFrom<&Value<_>>
.
§Examples
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 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);
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));
Formats the value using the given formatter.
Read more
Deserialize this value from the given Serde deserializer.
Read more
Formats the value using the given formatter.
Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Compares and returns the maximum of two values.
Read more
Compares and returns the minimum of two values.
Read more
Restrict a value to a certain interval.
Read more
This method tests for self
and other
values to be equal, and is used
by ==
.
This method tests for !=
. The default implementation is almost always
sufficient, and should not be overridden without very good reason.
This method returns an ordering between
self
and
other
values if one exists.
Read more
This method tests less than (for
self
and
other
) and is used by the
<
operator.
Read more
This method tests less than or equal to (for
self
and
other
) and is used by the
<=
operator.
Read more
This method tests greater than (for
self
and
other
) and is used by the
>
operator.
Read more
This method tests greater than or equal to (for
self
and
other
) and is used by the
>=
operator.
Read more
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.
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
Immutably borrows from an owned value.
Read more
Mutably borrows from an owned value.
Read more
Get a deserializer compatible with this signature.
Get the signature for the implementing type.
Read more
Returns the argument unchanged.
Calls U::from(self)
.
That is, this conversion is whatever the implementation of
From<T> for U
chooses to do.
Converts the given value to a
String
.
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.