【问题标题】:How do I get properties through OrgFreedesktopDBusProperties?如何通过 Org Freedesktop DBus 属性获取属性?
【发布时间】:2020-03-25 08:57:23
【问题描述】:

我想使用 dbus crate 检索一些属性,但 the example 不适用于我的情况。

骨架代码为:

fn main() {
    let mut conn = Connection::new_session().expect("D-Bus connection failed");
    let p = conn.with_proxy("X.Y.Z", "/X/Y/Z", Duration::from_millis(5000));
    let elapsed = p.get("X.Y.Z", "VAR");
    //...
}

使用示例中详述的dbus::blocking::stdintf::org_freedesktop_dbus::Properties 不适用于我:

error[E0283]: type annotations required: cannot resolve `for<'b> _: dbus::arg::Get<'b>`
   --> src/main.rs:169:21
    |
169 |     let elapsed = p.get("X.Y.Z", "VAR");
    |                     ^^^

要让这个工作,我必须使用dbus-codegen-rust 并复制并粘贴org.freedesktop.DBus.Properties 接口的输出代码:

pub trait OrgFreedesktopDBusProperties {
    fn get(&self, interface_name: &str, property_name: &str) -> Result<arg::Variant<Box<dyn arg::RefArg + 'static>>, dbus::Error>;
    fn get_all(&self, interface_name: &str) -> Result<::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>, dbus::Error>;
    fn set(&self, interface_name: &str, property_name: &str, value: arg::Variant<Box<dyn arg::RefArg>>) -> Result<(), dbus::Error>;
}

impl<'a, C: ::std::ops::Deref<Target=blocking::Connection>> OrgFreedesktopDBusProperties for blocking::Proxy<'a, C> {

    fn get(&self, interface_name: &str, property_name: &str) -> Result<arg::Variant<Box<dyn arg::RefArg + 'static>>, dbus::Error> {
        self.method_call("org.freedesktop.DBus.Properties", "Get", (interface_name, property_name, ))
            .and_then(|r: (arg::Variant<Box<dyn arg::RefArg + 'static>>, )| Ok(r.0, ))
    }

    fn get_all(&self, interface_name: &str) -> Result<::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>, dbus::Error> {
        self.method_call("org.freedesktop.DBus.Properties", "GetAll", (interface_name, ))
            .and_then(|r: (::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>, )| Ok(r.0, ))
    }

    fn set(&self, interface_name: &str, property_name: &str, value: arg::Variant<Box<dyn arg::RefArg>>) -> Result<(), dbus::Error> {
        self.method_call("org.freedesktop.DBus.Properties", "Set", (interface_name, property_name, value, ))
    }
}

我在这里做错了什么?

【问题讨论】:

  • 链接的示例包括属性的类型注释,您的错误是“需要类型注释”...您是否尝试将类型添加到 elapsed
  • 你是对的。使用let elapsed: Box&lt;dyn arg::RefArg&gt; = ... 似乎工作正常

标签: rust dbus


【解决方案1】:

解决方法:

let elapsed: &dyn arg::RefArg =
    &(proxy.get("X.Y.Z", "Elapsed") as Result<Box<dyn arg::RefArg + 'static>, dbus::Error>)?;

【讨论】:

    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 2012-09-28
    • 2013-01-02
    • 2018-07-16
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多