dbus2 / zbus-old

Rust D-Bus crate.
https://gitlab.freedesktop.org/dbus/zbus
Other
49 stars 13 forks source link

zvariant::Dict please allow iterating the key/value pairs #291

Open zeenix opened 1 year ago

zeenix commented 1 year ago

In GitLab by @estokes on Oct 12, 2022, 22:46

Hi, at the moment there is no way to just iterate the key/value pairs of a zvariant::Dict. If you want to do it you need to convert it to a hashmap, and to do that you need to read the signature and pick a concrete type that implements Basic, it's actually quite a lot of code to do this, and what I (and probably others) really want is just to iterate the values. I see that it's stored as a Vec of DictEntries, which are just the key and value, so it would be trivial to allow this iteration.

Thanks, zbus is awesome!

zeenix commented 1 year ago

Hi there,

at the moment there is no way to just iterate the key/value pairs of a zvariant::Dict. If you want to do it you need to convert it to a hashmap, and to do that you need to read the signature and pick a concrete type that implements Basic, it's actually quite a lot of code to do this,

So Dict isn't really meant to be used directly. Typically you want to:

  1. use a HashMap itself. Type is implemented for that.
  2. if you need to (de)serialize it as a Value:
    1. use your own struct by making use of SerializeDict and DeserializeDict derives.
    2. Convert from/to HashMap to/from Value or OwnedValue types.

Having said that, I don't know your exact use case and I have no objections on providing the API you're requesting.

I'm having trouble finding a lot of time these days for zbus work so it'll take a while, unless you could contribute this.

zeenix commented 1 year ago

In GitLab by @estokes on Oct 14, 2022, 24:44

I'm bridging dbus values to another quite similar dynamically typed network protocol (netidx), so I really need to just iterate and translate the values directly (e.g. I have a recursive fn dbus_value_to_netidx_value(v: &zvariant::Value) -> Value { ... }, all of the cases are quite straightforward except zvariant::Dict).

I would be happy to work on this.

zeenix commented 1 year ago

I'm bridging dbus values to another quite similar dynamically typed network protocol (netidx), so I really need to just iterate and translate the values directly (e.g. I have a recursive fn dbus_value_to_netidx_value(v: &zvariant::Value) -> Value { ... }, all of the cases are quite straightforward except zvariant::Dict).

I see. I just wanted to make sure you're not making your life harder than it needs to be. :smile:

I would be happy to work on this.

:thumbsup: I would prefer not to expose DictEntry in pub api btw and have a generic iterator so people can iterate over specific types.