dbus2 / zbus-old

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

Value macro not able to handle `Option` in Dict #321

Open zeenix opened 1 year ago

zeenix commented 1 year ago

In GitLab by @seiwill on Feb 1, 2023, 17:41

Deriving Value on signature = dict Structs that have Option values fails to compile.

use zbus::zvariant::{DeserializeDict, Type, Value};

#[derive(Debug, Clone, PartialEq, Default, Type, DeserializeDict, Value)]
#[zvariant(signature = "dict")]
pub struct ConnectionSettings {
    pub val_bool: bool,
    pub opt_string: Option<String>,
}

fn main() {
    println!("{:?}", ConnectionSettings::default());
}

Gives error:

error[E0277]: the trait bound `std::option::Option<std::string::String>: From<zbus::zvariant::Value<'_>>` is not satisfied
   --> src/main.rs:44:67
    |
44  | #[derive(Debug, Clone, PartialEq, Default, Type, DeserializeDict, Value)]
    |                                                                   ^^^^^ the trait `From<zbus::zvariant::Value<'_>>` is not implemented for `std::option::Option<std::string::String>`
    |
    = help: the following other types implement trait `From<T>`:
              <std::option::Option<&'a T> as From<&'a std::option::Option<T>>>
              <std::option::Option<&'a mut T> as From<&'a mut std::option::Option<T>>>
              <std::option::Option<&'a tracing_core::span::Id> as From<&'a tracing::span::EnteredSpan>>
              <std::option::Option<&'a tracing_core::span::Id> as From<&'a tracing::span::Span>>
              <std::option::Option<&'a tracing_core::span::Id> as From<&'a tracing_core::span::Current>>
              <std::option::Option<&'static tracing_core::metadata::Metadata<'static>> as From<&'a tracing_core::span::Current>>
              <std::option::Option<T> as From<Optional<T>>>
              <std::option::Option<T> as From<T>>
            and 7 others
    = note: required because of the requirements on the impl of `Into<std::option::Option<std::string::String>>` for `zbus::zvariant::Value<'_>`
    = note: required because of the requirements on the impl of `TryFrom<zbus::zvariant::Value<'_>>` for `std::option::Option<std::string::String>`
note: required by a bound in `zbus::zvariant::Value::<'a>::downcast`
   --> /home/sw/.cargo/registry/src/github.com-1ecc6299db9ec823/zvariant-3.10.0/src/value.rs:310:12
    |
310 |         T: TryFrom<Value<'a>>,
    |            ^^^^^^^^^^^^^^^^^^ required by this bound in `zbus::zvariant::Value::<'a>::downcast`
    = note: this error originates in the derive macro `Value` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
zeenix commented 1 year ago

In GitLab by @seiwill on Feb 14, 2023, 18:02

Workaround for Dict structs that need to derive Value for use in property getters is to not derive Value, and implement a custom TryFrom.