Attribute Macro zbus_macros::dbus_interface[][src]

#[dbus_interface]

Attribute macro for implementing a D-Bus interface.

The macro must be applied on an impl T. All methods will be exported, either as methods, properties or signal depending on the item attributes. It will implement the Interface trait for T on your behalf, to handle the message dispatching and introspection support.

The methods accepts the dbus_interface attributes:

  • name - override the D-Bus name (pascal case form of the method 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 - the method is a “signal”. It must be a method declaration (without body). Its code block will be expanded to emit the signal from the object path associated with the interface instance.

    You can call a signal method from a an interface method, or from an ObjectServer::with function.

  • struct_return - the method returns a structure. Although it is very rare for a D-Bus method to return a single structure, it does happen. Since it is not possible for zbus to differentiate this case from multiple out arguments put in a structure, you must either explicitly mark the case of single structure return with this attribute or wrap the structure in another structure or tuple. The latter can be achieve by using (,) syntax, for example instead of MyStruct, write (MyStruct,).

The method arguments offers some the following zbus attributes:

  • header - This marks the method argument to receive the message header associated with the D-Bus method call being handled.

Example

use zbus_macros::dbus_interface;
use zbus::MessageHeader;

struct Example {
    some_data: String,
}

#[dbus_interface(name = "org.myservice.Example")]
impl Example {
    // "Quit" method. A method may throw errors.
    fn quit(&self, #[zbus(header)] hdr: MessageHeader<'_>) -> zbus::fdo::Result<()> {
        let path = hdr.path()?.unwrap();
        let msg = format!("You are leaving me on the {} path?", path);

        Err(zbus::fdo::Error::Failed(msg))
    }

    // "TheAnswer" property (note: the "name" attribute), with its associated getter.
    #[dbus_interface(property, name = "TheAnswer")]
    fn answer(&self) -> u32 {
        2 * 3 * 7
    }

    // "Notify" signal (note: no implementation body).
    #[dbus_interface(signal)]
    fn notify(&self, message: &str) -> zbus::Result<()>;
}

See also ObjectServer documentation to learn how to export an interface over a Connection.