[][src]Attribute Macro zbus::dbus_interface

#[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 - This attribute is depcrecated and a noop. If you want to return a single structure from a method, simply declare it to return a named structure or a tuple with a tuple as the only field.

    Since it is not possible for zbus to differentiate between the case of a single structure being returned from the case of multiple out arguments returned as a named structure, nor to introspect the named structure type, the latter is not supported. you must use tuples for returning multiple values from a method.

  • out_args - When returning multiple values from a method, naming the out arguments become important. You can use out_args for specifying names for your out arguments.

Example

use zbus_macros::dbus_interface;

struct Example {
    some_data: String,
}

#[dbus_interface(name = "org.myservice.Example")]
impl Example {
    // "Quit" method. A method may throw errors.
    fn quit(&self) -> zbus::fdo::Result<()> {
        Err(zbus::fdo::Error::Failed("You are leaving me?".to_string()))
    }

    // "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<()>;

    #[dbus_interface(out_args("answer", "question"))]
    fn meaning_of_life(&self) -> zbus::Result<(i32, String)> {
        Ok((42, String::from("Meaning of life")))
    }
}

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