如何使用zbus列出阻止设备?

问题描述

我想使用称为here的dbus实现列出系统中所有可用的块设备。

here提到了org.freedesktop.Udisks2.Manager接口GetBlockDevices上的方法调用,该接口接受IN a{sv} options,OUT ao block_objects作为方法参数。

我使用zbus编写:

use std::error::Error;
use std::result::Result;
use zbus::{Connection,Proxy};

fn main() -> Result<(),Box<dyn Error>> {
    let connection = Connection::new_system()?;
    let p = Proxy::new(
        &connection,"org.freedesktop.Udisks2","/org/freedesktop/Udisks2/Manager","org.freedesktop.Udisks2.Manager",)?;
    let resp: Vec<zvariant::ObjectPath> = p.call("GetBlockDevices",&std::collections::HashMap::<String,zvariant::Value>::new())?;
    dbg!(resp);
    Ok(())
}

据我了解,zbus代表DBus变体。但是我收到以下错误

error: implementation of `serde::de::Deserialize` is not general enough
   --> src/main.rs:13:45
    |
13  |       let resp: Vec<zvariant::ObjectPath> = p.call("GetBlockDevices",zvariant::Value>::new())?;
    |                                               ^^^^ implementation of `serde::de::Deserialize` is not general enough
    | 
   ::: /home/adnan338/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.115/src/de/mod.rs:531:1
    |
531 | / pub trait Deserialize<'de>: Sized {
532 | |     /// Deserialize this value from the given Serde deserializer.
533 | |     ///
534 | |     /// See the [Implementing `Deserialize`][impl-deserialize] section of the
...   |
569 | |     }
570 | | }
    | |_- trait `serde::de::Deserialize` defined here
    |
    = note: `std::vec::Vec<zvariant::object_path::ObjectPath<'_>>` must implement `serde::de::Deserialize<'0>`,for any lifetime `'0`...
    = note: ...but `std::vec::Vec<zvariant::object_path::ObjectPath<'_>>` actually implements `serde::de::Deserialize<'1>`,for some specific lifetime `'1`

是什么原因造成的?如何避免此错误

解决方法

首先,感谢您试用我们的板条箱。问题是zbus::Proxy::call期望在将反序列化为非拥有类型时返回值是拥有的。以下两项工作:

// OwnedObjectPath require zvariant >= 2.2.0
let resp: Vec<zvariant::OwnedObjectPath> = p.call(
    "GetBlockDevices",&std::collections::HashMap::<String,zvariant::Value>::new(),)?;
let resp = p.call_method(
    "GetBlockDevices",)?; 
let resp: Vec<zvariant::ObjectPath> = resp.body()?;