[][src]Struct zvariant::DictEntry

pub struct DictEntry { /* fields omitted */ }

A dictionary entry as a key-value pair.

This is not very useful on its own but since D-Bus defines it as its own type, a hashmap in D-Bus is encoded as an array of dictionary entries and GVariant even allows this to be used outside of an array, we provide this data type.

The key must be a basic data type.

Example:

use zvariant::{Decode, DictEntry};
use zvariant::{Encode, EncodingFormat, Structure};

let entry = DictEntry::new(
    // String key
    "hello",
    // Structure value
    Structure::new()
        .add_field(u8::max_value())
        .add_field(u32::max_value()),
);
assert!(entry.signature() == "{s(yu)}");

let format = EncodingFormat::default();
let encoding = entry.encode(format);
assert!(encoding.len() == 24);

let entry = DictEntry::decode(encoding, entry.signature(), format).unwrap();
assert!(entry.key::<String>().unwrap() == "hello");
let structure = entry.value::<Structure>().unwrap();
let fields = structure.fields();
assert!(u8::is(&fields[0]));
assert!(*u8::from_variant(&fields[0]).unwrap() == u8::max_value());
assert!(u32::is(&fields[1]));
assert!(*u32::from_variant(&fields[1]).unwrap() == u32::max_value());

Methods

impl DictEntry[src]

pub fn new<K, V>(key: K, value: V) -> Self where
    K: Encode + Basic,
    V: Encode
[src]

Create a new DictEntry

pub fn key<K>(&self) -> Result<&K, VariantError> where
    K: Decode + Basic
[src]

Get a reference to the key.

pub fn value<V>(&self) -> Result<&V, VariantError> where
    V: Decode
[src]

Get a reference to the value.

pub fn take<K, V>(self) -> Result<(K, V), VariantError> where
    K: Decode + Basic,
    V: Decode
[src]

Take the key and value, consuming self.

Trait Implementations

impl Clone for DictEntry[src]

impl Debug for DictEntry[src]

impl Decode for DictEntry[src]

impl Encode for DictEntry[src]

Auto Trait Implementations

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> 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.