Attribute Macro zbus::dbus_proxy[][src]

#[dbus_proxy]
Expand description

Attribute macro for defining a D-Bus proxy (using zbus Proxy).

The macro must be applied on a trait T. A matching impl T will provide the proxy. The proxy instance can be created with the associated new() or new_for() methods. The former doesn’t take any argument and uses the default service name and path. The later allows you to specify both.

Each trait method will be expanded to call to the associated D-Bus remote interface.

Trait methods accept dbus_proxy attributes:

  • name - override the D-Bus name (pascal case form by default)

  • property - expose the method as a property. If the method takes an argument, it must be a setter, with a set_ prefix. Otherwise, it’s a getter.

  • signal - declare a signal just like a D-Bus method. The macro will provide a method to register and deregister a handler for the signal, whose signature must match that of the signature declaration.

    NB: Any doc comments provided shall be appended to the ones added by the macro.

(the expanded impl also provides an introspect() method, for convenience)

Example

use zbus_macros::dbus_proxy;
use zbus::{Connection, Result, fdo};
use zvariant::Value;

#[dbus_proxy(
    interface = "org.test.SomeIface",
    default_service = "org.test.SomeService",
    default_path = "/org/test/SomeObject"
)]
trait SomeIface {
    fn do_this(&self, with: &str, some: u32, arg: &Value) -> Result<bool>;

    #[dbus_proxy(property)]
    fn a_property(&self) -> fdo::Result<String>;

    #[dbus_proxy(property)]
    fn set_a_property(&self, a_property: &str) -> fdo::Result<()>;

    #[dbus_proxy(signal)]
    fn some_signal(&self, arg1: &str, arg2: u32) -> fdo::Result<()>;
};

let connection = Connection::new_session()?;
let proxy = SomeIfaceProxy::new(&connection)?;
let _ = proxy.do_this("foo", 32, &Value::new(true));
let _ = proxy.set_a_property("val");

proxy.connect_some_signal(|s, u| {
    println!("arg1: {}, arg2: {}", s, u);

    Ok(())
})?;

// You'll want to make at least a call to `handle_next_signal` before disconnecting the signal.
assert!(proxy.disconnect_some_signal()?);
assert!(!proxy.disconnect_some_signal()?);

zbus_polkit is a good example of how to bind a real D-Bus API.