[][src]Struct zvariant::Dict

pub struct Dict(_);

A dictionary as an Array of DictEntry.

It would have been best to implement From<Vec<DictEntry>> for HashMap but since both From trait and HashMap are external to our crate, we need this intermediate type. We can't implement Into either as Vec isn't our type either. API is provided to transform this into, and from a HashMap though.

Example:

use core::convert::{TryFrom, TryInto};
use std::collections::HashMap;

use zvariant::{Array, Dict};
use zvariant::{Decode, Encode, EncodingFormat};

// Create a Dict from a HashMap
let mut map: HashMap<i64, &str> = HashMap::new();
map.insert(1, "123");
map.insert(2, "456");
let dict: Dict = map.into();

// Then we turn it into an Array so we can encode it
let array = Array::try_from(dict).unwrap();
let format = EncodingFormat::default();
let encoding = array.encode(format);
assert!(encoding.len() == 40);

// Then we do the opposite
let array = Array::decode(encoding, array.signature(), format).unwrap();
let dict = Dict::try_from(array).unwrap();
let map: HashMap<i64, String> = dict.try_into().unwrap();

// Check we got the right thing back
assert!(map[&1] == "123");
assert!(map[&2] == "456");

Methods

impl Dict[src]

pub fn new() -> Self[src]

Create a new Dict.

Same as calling Dict::default().

pub fn new_from_vec(vec: Vec<DictEntry>) -> Self[src]

Create a new Dict from vec.

Same as calling Dict::from(vec).

pub fn get(&self) -> &Vec<DictEntry>[src]

Get a reference to the underlying Vec<DictEntry>.

pub fn get_mut(&mut self) -> &mut Vec<DictEntry>[src]

Get a mutable reference to the underlying Vec<DictEntry>.

pub fn into_inner(self) -> Vec<DictEntry>[src]

Unwraps the Dict, returning the underlying Vec<DictEntry>.

Trait Implementations

impl Default for Dict[src]

impl From<Dict> for Array[src]

impl<K, V> From<HashMap<K, V, RandomState>> for Dict where
    K: Encode + Basic,
    V: Encode
[src]

impl TryFrom<Array> for Dict[src]

type Error = VariantError

The type returned in the event of a conversion error.

impl<'a, K, V> TryInto<HashMap<&'a K, &'a V, RandomState>> for &'a Dict where
    K: Decode + Basic,
    V: Decode
[src]

type Error = VariantError

The type returned in the event of a conversion error.

impl<K, V> TryInto<HashMap<K, V, RandomState>> for Dict where
    K: Decode + Basic,
    V: Decode
[src]

type Error = VariantError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl RefUnwindSafe for Dict

impl Send for Dict

impl Sync for Dict

impl Unpin for Dict

impl UnwindSafe for Dict

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