tokio-dbus-codegen 0.2.0

Build time code generation of D-Bus clients and servers for tokio-dbus.
Documentation

tokio-dbus-codegen

Generate asynchronous D-Bus clients and servers from interface files at build time.

The generated code speaks in owned Rust types [String], [Vec], and HashMap and tuples and is driven by a tokio_dbus_runtime::Connection, which both crates a consumer needs to depend on.

Using it from a build script

Add the generator as a build dependency and the runtime as a regular one:

[dependencies]
tokio-dbus-runtime = "0.2.0"

[build-dependencies]
tokio-dbus-codegen = "0.2.0"

Then write a build.rs which names the interface files to read and what to generate for each interface in them:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tokio_dbus_codegen::Builder::new()
        .file("interfaces/org.freedesktop.Notifications.xml")
        .client("org.freedesktop.Notifications")
        .generate("notifications.rs")?;

    Ok(())
}

Finally include the generated file:

include!(concat!(env!("OUT_DIR"), "/notifications.rs"));

What is generated

For an interface com.example.Example, a module example is generated holding:

  • INTERFACE and MATCH_RULE constants.
  • A Signal enum with a variant per signal, which can be decoded from an incoming message and emitted from an outgoing one. Present whenever the interface has signals.
  • With client(), an Example struct with an async method per D-Bus method, a getter and setter per property, and all_properties().
  • With server(), an ExampleServer trait with an async method per D-Bus method and per property accessor, and a dispatch() function which routes an incoming call to it. dispatch() also answers org.freedesktop.DBus.Properties for the interface.

Type mapping

D-Bus Rust
y [u8]
b [bool]
n / q [i16] / [u16]
i / u [i32] / [u32]
x / t [i64] / [u64]
d [f64]
s [String]
o ObjectPathBuf
g SignatureBuf
v Value
aT Vec<T>
a{KV} HashMap<K, V>
(T1 T2) (T1, T2)

Client methods take the borrowed form of each argument where borrowing is free, so an s is passed as a [&str] and an as as a &[String].

Passing a file descriptor (h) is not supported.