rust - 如何使用 zbus 列出块设备?
问题描述
我想使用名为zbus的 dbus 实现列出系统中所有可用的块设备。
UDisks2 文档提到了对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(())
}
据我了解,zvariant 值代表一个 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", &std::collections::HashMap::<String, 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",
&std::collections::HashMap::<String, zvariant::Value>::new(),
)?;
let resp: Vec<zvariant::ObjectPath> = resp.body()?;
推荐阅读
- python - 使用 Python Locust 进行负载测试时,reqs/sec 不正确
- css - 当浏览器不支持功能查询时究竟会发生什么?
- ethereum - Geth 在最后 80 个区块上卡住同步
- c# - 如果所有文本框都成功完成,我如何显示此消息框
- vim - "mo" 使用默认应用程序打开文件,
- android - React-native 应用程序中的业务逻辑在哪里?
- wordpress - 您能帮我处理这个具有多个 301 重定向的 301 重定向正则表达式代码吗?会按顺序评估吗?
- ios - RX 按钮点击处理实际上是如何实现的?
- azure-devops - 获取/修改 Azure DevOps Services 中的付费用户数和配额
- c - 使用 malloc 作为双指针