[−][src]Enum zvariant::Value
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>)
Array(Array<'a>)
Dict(Dict<'a, 'a>)
Structure(Structure<'a>)
Fd(Fd)
Implementations
impl<'a> Value<'a>
[src]
pub fn new<T>(value: T) -> Self where
T: Into<Self> + Type,
[src]
T: Into<Self> + Type,
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]
T: TryFrom<Value<'a>>,
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]
T: ?Sized,
&'a T: TryFrom<&'a Value<'a>>,
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]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
impl<'a, '_> From<&'_ Value<'a>> for OwnedValue
[src]
impl<'a> From<&'a str> for Value<'a>
[src]
impl<'v, V> From<&'v [V]> for Value<'v> where
&'v [V]: Into<Array<'v>>,
[src]
&'v [V]: Into<Array<'v>>,
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> From<ObjectPath<'a>> for Value<'a>
[src]
fn from(v: ObjectPath<'a>) -> Self
[src]
impl<'a> From<Signature<'a>> for Value<'a>
[src]
impl<'v, 's: 'v, T> From<T> for Value<'v> where
T: Into<Structure<'s>>,
[src]
T: Into<Structure<'s>>,
impl<'a> From<Value<'a>> for OwnedValue
[src]
impl<'v, V> From<Vec<V>> for Value<'v> where
Vec<V>: Into<Array<'v>>,
[src]
Vec<V>: Into<Array<'v>>,
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 u8
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for u8
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a u32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for u32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a i64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for i64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a u64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for u64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a f64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for f64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a Fd
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Fd
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a bool
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a Str<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Str<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a Signature<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Signature<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a ObjectPath<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for ObjectPath<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a Structure<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Structure<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
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.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Dict<'a, 'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for bool
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a Array<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for Array<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a str
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for String
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a i16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for i16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a u16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for u16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for &'a i32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<&'a Value<'a>> for i32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: &'a Value) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for u8
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for bool
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Str<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Signature<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for ObjectPath<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Structure<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Dict<'a, 'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Array<'a>
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for String
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a, T> TryFrom<Value<'a>> for Vec<T> where
T: TryFrom<Value<'a>, Error = Error> + 'a,
[src]
T: TryFrom<Value<'a>, Error = Error> + 'a,
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for i16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for u16
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for i32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for u32
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for i64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for u64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for f64
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
impl<'a> TryFrom<Value<'a>> for Fd
[src]
type Error = Error
The type returned in the event of a conversion error.
fn try_from(value: Value<'a>) -> Result<Self, Self::Error>
[src]
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]
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,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
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.
fn to_owned(&self) -> T
[src]
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.
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>,