5个稳定版本
| 2.0.2 | 2021年1月3日 | 
|---|---|
| 2.0.1 | 2021年1月2日 | 
| 2.0.0 | 2020年12月5日 | 
| 1.1.0 | 2020年6月30日 | 
| 1.0.0 | 2020年6月14日 | 
#1337 在 异步
64KB
 1.5K  SLoC
dbus-async-derive
dbus-async-derive 是一个proc derive宏,用于实现 dbus_async::Handler。这个crate应该用于创建DBus服务。  
用法
将以下内容添加到您的 Cargo.toml
[dependencies]
dbus-async-derive = "2.0"
dbus-async = "2.0"
dbus-message-parser = "3.1"
async-trait = "0.1"
示例
以下示例展示了如何使用接口 org.example.interface 创建DBus服务。此接口有一个方法 ExampleMethod 和一个属性 ExampleProperty。该对象在 /org/example/object/path 可用。
use dbus_async::{Binder, DBus};
use dbus_async_derive::Handler;
use dbus_message_parser::{Error, MessageHeader};
use std::convert::TryInto;
#[derive(Handler)]
#[interface(
    "org.example.interface",
    method("ExampleMethod", method),
    property("ExampleProperty", "s", get_property = "get", set_property = "set")
)]
struct DBusObject {
    property: String,
}
impl DBusObject {
    async fn method(
        &mut self,
        dbus: &DBus,
        _msg_header: &MessageHeader,
    ) -> Result<(), (Error, String)> {
        // The code of the method
        println!(
            "The DBus socket where the message came from: {}",
            dbus.get_address()
        );
        // ...
        Ok(())
    }
    async fn get_property(
        &mut self,
        _dbus: &DBus,
        _msg_header: &MessageHeader,
    ) -> Result<String, (Error, String)> {
        Ok(self.property.clone())
    }
    async fn set_property(
        &mut self,
        _dbus: &DBus,
        _msg_header: &MessageHeader,
        new_value: String,
    ) -> Result<(), (Error, String)> {
        self.property = new_value;
        Ok(())
    }
}
#[tokio::main]
async fn main() {
    let (dbus, _connection_join_handle) = DBus::session(true)
        .await
        .expect("failed to get the DBus object");
    // Create the object
    let dbus_object = DBusObject {
        property: "".to_string(),
    };
    let object_path = "/org/example/object/path".try_into().unwrap();
    // Bind the same object to the second object path
    dbus_object
        .bind(dbus, object_path)
        .await
        .expect("Something went wrong");
}
DBus ↔️ Rust类型
以下表格显示了类型转换的工作方式
| 名称 | DBus | Rust | 
|---|---|---|
| 字节 | y | u8 | 
| 布尔值 | b | bool | 
| 有符号16位整数 | n | i16 | 
| 无符号16位整数 | q | u16 | 
| 有符号32位整数 | i | i32 | 
| 无符号32位整数 | u | u32 | 
| 有符号32位整数 | x | i64 | 
| 无符号32位整数 | t | u64 | 
| IEEE 754双精度浮点数 | d | f64 | 
| 无符号32位整数文件描述符 | h | std::os::unix::io::RawFd | 
| 字符串 | s | 字符串 | 
| 对象路径 | o | 字符串 | 
| 签名 | o | 字符串 | 
| 数组 | aT | Vec<T> | 
| 结构 | (T1T2..) | (T1, T2, ..) | 
| 字典条目 | {T1T2} | (T1, T2) | 
示例
以下表格显示了类型容器类型转换的工作方式
| 名称 | DBus | Rust | 
|---|---|---|
| 数组 | ay | Vec<u8> | 
| 结构 | (isi) | (i32, 字符串, i32) | 
| 字典条目 | {ys} | (u8, 字符串) | 
依赖项
~1.5MB
~37K SLoC