dbus2 / zbus

Rust D-Bus crate.
Other
394 stars 90 forks source link

Allow interface properties to receive `#[zbus(...)]` parameters #218

Open zeenix opened 3 years ago

zeenix commented 3 years ago

In GitLab by @DataTriny on Nov 1, 2021, 11:14

Hello, To implement some interface properties, I must have access to the connection that received the call in order to build the reply. Here is an example:

#[dbus_interface(name = "org.a11y.atspi.Accessible")]
impl<T> AccessibleInterfaceObject<T>
where T: AccessibleInterface + Send + Sync + 'static
{
    #[dbus_interface(property)]
    fn parent(
        &self,
        #[zbus(connection)] connection: Connection
    ) -> ObjectAddress {
        let bus_name = connection.unique_name.unwrap();
        if let Some(parent) = self.0.parent() {
            ObjectAddress::accessible(bus_name, parent)
        } else {
            ObjectAddress::null(bus_name)
        }
    }
}

This is currently not allowed, and results in an error message:

error: custom attribute panicked
  --> platforms/linux/src/atspi/interfaces/accessible.rs:30:1
   |
30 | #[dbus_interface(name = "org.a11y.atspi.Accessible")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: message: assertion failed: name.starts_with(\"set_\")

As stated in the macro documentation, property getters must not take any argument, and setters must only take the new value. It would be nice however, if arguments marked with #[zbus(...)] were allowed.

zeenix commented 3 years ago

Thanks for filing this. This is certainly something we should support and not at all an intentional limitation.

zeenix commented 11 months ago

Some conversation here regarding this.

endrift commented 4 months ago

I've been running into this issue. I have an object that sometimes has to farm out to external objects to collate some values. Right now I have to cache a Connection object inside the interface object, which makes using ConnectionBuilder::serve_at impossible without converting the Connection object into something like a OnceCell, which is...pretty inconvenient to say the very least.

zeenix commented 4 months ago

@endrift Thanks but I already know about this inconvenience. That's why this issue exists already.

What's needed is for someone to contribute the fix. The good thing is that Interface is documented as unstable, so we can modify that trait without bumping the major version.