61 stable releases (4 major)

5.1.1 Nov 7, 2024
5.0.1 Oct 21, 2024
4.4.0 Jul 22, 2024
4.1.2 Feb 25, 2024
0.1.2 Jul 7, 2020

#3 in Unix APIs

Download history 162665/week @ 2024-07-31 173559/week @ 2024-08-07 155707/week @ 2024-08-14 159961/week @ 2024-08-21 161567/week @ 2024-08-28 172067/week @ 2024-09-04 148790/week @ 2024-09-11 153182/week @ 2024-09-18 167467/week @ 2024-09-25 170057/week @ 2024-10-02 163306/week @ 2024-10-09 179781/week @ 2024-10-16 176580/week @ 2024-10-23 182821/week @ 2024-10-30 181892/week @ 2024-11-06 183925/week @ 2024-11-13

759,301 downloads per month
Used in 479 crates (152 directly)

MIT license

1MB
23K SLoC

zbus

This is the main subcrate of the zbus project, that provides the API to interact with D-Bus. It takes care of the establishment of a connection, the creation, sending and receiving of different kind of D-Bus messages (method calls, signals etc) for you.

Status: Stable.

Getting Started

The best way to get started with zbus is the book, where we start with basic D-Bus concepts and explain with code samples, how zbus makes D-Bus easy.

Example code

We'll create a simple D-Bus service and client to demonstrate the usage of zbus. Note that these examples assume that a D-Bus broker is setup on your machine and you've a session bus running (DBUS_SESSION_BUS_ADDRESS environment variable must be set). This is guaranteed to be the case on a typical Linux desktop session.

Service

A simple service that politely greets whoever calls its SayHello method:

use std::{error::Error, future::pending};
use zbus::{connection, interface};

struct Greeter {
    count: u64
}

#[interface(name = "org.zbus.MyGreeter1")]
impl Greeter {
    // Can be `async` as well.
    fn say_hello(&mut self, name: &str) -> String {
        self.count += 1;
        format!("Hello {}! I have been called {} times.", name, self.count)
    }
}

// Although we use `tokio` here, you can use any async runtime of choice.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let greeter = Greeter { count: 0 };
    let _conn = connection::Builder::session()?
        .name("org.zbus.MyGreeter")?
        .serve_at("/org/zbus/MyGreeter", greeter)?
        .build()
        .await?;

    // Do other things or go to wait forever
    pending::<()>().await;

    Ok(())
}

You can use the following command to test it:

$ busctl --user call org.zbus.MyGreeter /org/zbus/MyGreeter org.zbus.MyGreeter1 SayHello s "Maria"
s "Hello Maria! I have been called 1 times."

Client

Now let's write the client-side code for MyGreeter service:

use zbus::{Connection, Result, proxy};

#[proxy(
    interface = "org.zbus.MyGreeter1",
    default_service = "org.zbus.MyGreeter",
    default_path = "/org/zbus/MyGreeter"
)]
trait MyGreeter {
    async fn say_hello(&self, name: &str) -> Result<String>;
}

// Although we use `tokio` here, you can use any async runtime of choice.
#[tokio::main]
async fn main() -> Result<()> {
    let connection = Connection::session().await?;

    // `proxy` macro creates `MyGreaterProxy` based on `Notifications` trait.
    let proxy = MyGreeterProxy::new(&connection).await?;
    let reply = proxy.say_hello("Maria").await?;
    println!("{reply}");

    Ok(())
}

Blocking API

While zbus is primarily asynchronous (since 2.0), blocking wrappers are provided for convenience. Since zbus 5.0, blocking API can be disabled by disabling the blocking-api cargo feature.

Compatibility with async runtimes

zbus is runtime-agnostic and should work out of the box with different Rust async runtimes. However, in order to achieve that, zbus spawns a thread per connection to handle various internal tasks. If that is something you would like to avoid, you need to:

Moreover, by default zbus makes use of async-io for all I/O, which also launches its own thread to run its own internal executor.

Special tokio support

Since tokio is the most popular async runtime, zbus provides an easy way to enable tight integration with it without you having to worry about any of the above: Enabling the tokio feature:

# Sample Cargo.toml snippet.
[dependencies]
# Also disable the default `async-io` feature to avoid unused dependencies.
zbus = { version = "3", default-features = false, features = ["tokio"] }

That's it! No threads launched behind your back by zbus (directly or indirectly) now and no need to tick any executors etc. 😼

Note: On Windows, the async-io feature is currently required for UNIX domain socket support, see the corresponding tokio issue on GitHub.

Dependencies

~7–21MB
~311K SLoC