Skip to content

Commit

Permalink
Remove dbus_ prefix for proc macros
Browse files Browse the repository at this point in the history
Rename all attribute macros to zbus
  • Loading branch information
TTWNO committed Jan 17, 2024
1 parent 2357284 commit 3af251c
Show file tree
Hide file tree
Showing 25 changed files with 6,818 additions and 374 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ A simple service that politely greets whoever calls its `SayHello` method:

```rust,no_run
use std::{error::Error, future::pending};
use zbus::{connection, dbus_interface};
use zbus::{connection, interface};
struct Greeter {
count: u64
}
#[dbus_interface(name = "org.zbus.MyGreeter1")]
#[interface(name = "org.zbus.MyGreeter1")]
impl Greeter {
// Can be `async` as well.
fn say_hello(&mut self, name: &str) -> String {
Expand Down Expand Up @@ -79,9 +79,9 @@ s "Hello Maria! I have been called 1 times."
Now let's write the client-side code for `MyGreeter` service:

```rust,no_run
use zbus::{Connection, Result, dbus_proxy};
use zbus::{Connection, Result, proxy};
#[dbus_proxy(
#[proxy(
interface = "org.zbus.MyGreeter1",
default_service = "org.zbus.MyGreeter",
default_path = "/org/zbus/MyGreeter"
Expand All @@ -95,7 +95,7 @@ trait MyGreeter {
async fn main() -> Result<()> {
let connection = Connection::session().await?;
// `dbus_proxy` macro creates `MyGreeterProxy` based on `Notifications` trait.
// `proxy` macro creates `MyGreeterProxy` based on `Notifications` trait.
let proxy = MyGreeterProxy::new(&connection).await?;
let reply = proxy.say_hello("Maria").await?;
println!("{reply}");
Expand Down
40 changes: 20 additions & 20 deletions book/src/blocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,48 @@ except all its methods are blocking.
## Client

Similar to `blocking::Connection`, you use `blocking::Proxy` type. Its constructors require
`blocking::Connection` instead of `Connection`. Moreover, `dbus_proxy` macro generates a
`blocking::Connection` instead of `Connection`. Moreover, `proxy` macro generates a
`blocking::Proxy` wrapper for you as well. Let's convert the last example in the previous chapter,
to use the blocking connection and proxy:

```rust,no_run
use zbus::{blocking::Connection, zvariant::ObjectPath, dbus_proxy, Result};
use zbus::{blocking::Connection, zvariant::ObjectPath, proxy, Result};
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Manager",
default_path = "/org/freedesktop/GeoClue2/Manager"
)]
trait Manager {
#[dbus_proxy(object = "Client")]
#[zbus(object = "Client")]
/// The method normally returns an `ObjectPath`.
/// With the object attribute, we can make it return a `ClientProxy` directly.
fn get_client(&self);
}
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Client"
)]
trait Client {
fn start(&self) -> Result<()>;
fn stop(&self) -> Result<()>;
#[dbus_proxy(property)]
#[zbus(property)]
fn set_desktop_id(&mut self, id: &str) -> Result<()>;
#[dbus_proxy(signal)]
#[zbus(signal)]
fn location_updated(&self, old: ObjectPath<'_>, new: ObjectPath<'_>) -> Result<()>;
}
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Location"
)]
trait Location {
#[dbus_proxy(property)]
#[zbus(property)]
fn latitude(&self) -> Result<f64>;
#[dbus_proxy(property)]
#[zbus(property)]
fn longitude(&self) -> Result<f64>;
}
let conn = Connection::system().unwrap();
Expand Down Expand Up @@ -88,7 +88,7 @@ println!(
);
```

As you can see, nothing changed in the `dbus_proxy` usage here and the rest largely remained the
As you can see, nothing changed in the `proxy` usage here and the rest largely remained the
same as well. One difference that's not obvious is that the blocking API for receiving signals,
implement [`std::iter::Iterator`] trait instead of [`futures::stream::Stream`].

Expand All @@ -97,15 +97,15 @@ implement [`std::iter::Iterator`] trait instead of [`futures::stream::Stream`].
That's almost the same as receiving signals:

```rust,no_run
# use zbus::{blocking::Connection, dbus_proxy, Result};
# use zbus::{blocking::Connection, proxy, Result};
#
#[dbus_proxy(
#[proxy(
interface = "org.freedesktop.systemd1.Manager",
default_service = "org.freedesktop.systemd1",
default_path = "/org/freedesktop/systemd1"
)]
trait SystemdManager {
#[dbus_proxy(property)]
#[zbus(property)]
fn log_level(&self) -> zbus::Result<String>;
}
Expand All @@ -124,15 +124,15 @@ fn main() -> Result<()> {

Similarly here, you'd use [`blocking::ObjectServer`] that is associated with every
[`blocking::Connection`] instance. While there is no blocking version of `Interface`,
`dbus_interface` allows you to write non-async methods.
`interface` allows you to write non-async methods.

**Note:** Even though you can write non-async methods, these methods are still called from an async
context. Therefore, you can not use blocking API in the method implementation directly. See note at
the beginning of this chapter for details on why and a possible workaround.

```rust,no_run
# use std::error::Error;
# use zbus::{blocking::connection, dbus_interface, fdo, SignalContext};
# use zbus::{blocking::connection, interface, fdo, SignalContext};
#
use event_listener::Event;
Expand All @@ -141,7 +141,7 @@ struct Greeter {
done: Event,
}
#[dbus_interface(name = "org.zbus.MyGreeter1")]
#[interface(name = "org.zbus.MyGreeter1")]
impl Greeter {
fn say_hello(&self, name: &str) -> String {
format!("Hello {}!", name)
Expand All @@ -160,7 +160,7 @@ impl Greeter {
}
/// A "GreeterName" property.
#[dbus_interface(property)]
#[zbus(property)]
fn greeter_name(&self) -> &str {
&self.name
}
Expand All @@ -170,13 +170,13 @@ impl Greeter {
/// Additionally, a `greeter_name_changed` method has been generated for you if you need to
/// notify listeners that "GreeterName" was updated. It will be automatically called when
/// using this setter.
#[dbus_interface(property)]
#[zbus(property)]
fn set_greeter_name(&mut self, name: String) {
self.name = name;
}
/// A signal; the implementation is provided by the macro.
#[dbus_interface(signal)]
#[zbus(signal)]
async fn greeted_everyone(ctxt: &SignalContext<'_>) -> zbus::Result<()>;
}
Expand Down
64 changes: 32 additions & 32 deletions book/src/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Instead, we want to wrap this `Notify` D-Bus method in a Rust function. Let's se

## Trait-derived proxy call

A trait declaration `T` with a `dbus_proxy` attribute will have a derived `TProxy` and
A trait declaration `T` with a `proxy` attribute will have a derived `TProxy` and
`TProxyBlocking` (see [chapter on "blocking"][cob] for more information on that) implemented thanks
to procedural macros. The trait methods will have respective `impl` methods wrapping the D-Bus
calls:
Expand All @@ -97,9 +97,9 @@ calls:
use std::collections::HashMap;
use std::error::Error;
use zbus::{zvariant::Value, dbus_proxy, Connection};
use zbus::{zvariant::Value, proxy, Connection};
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.Notifications",
default_path = "/org/freedesktop/Notifications"
)]
Expand Down Expand Up @@ -163,21 +163,21 @@ Let's look at this API in action, with an example where we monitor started syste
# // NOTE: When changing this, please also keep `zbus/examples/watch-systemd-jobs.rs` in sync.
use async_std::stream::StreamExt;
use zbus::Connection;
use zbus_macros::dbus_proxy;
use zbus_macros::proxy;
use zvariant::OwnedObjectPath;
# fn main() {
# async_io::block_on(watch_systemd_jobs()).expect("Error listening to signal");
# }
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.systemd1",
default_path = "/org/freedesktop/systemd1",
interface = "org.freedesktop.systemd1.Manager"
)]
trait Systemd1Manager {
// Defines signature for D-Bus signal named `JobNew`
#[dbus_proxy(signal)]
#[zbus(signal)]
fn job_new(&self, id: u32, job: OwnedObjectPath, unit: String) -> zbus::Result<()>;
}
Expand Down Expand Up @@ -208,42 +208,42 @@ Here is a more elaborate example, where we get our location from
[Geoclue](https://gitlab.freedesktop.org/geoclue/geoclue/-/blob/master/README.md):

```rust,no_run
use zbus::{zvariant::ObjectPath, dbus_proxy, Connection, Result};
use zbus::{zvariant::ObjectPath, proxy, Connection, Result};
use futures_util::stream::StreamExt;
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Manager",
default_path = "/org/freedesktop/GeoClue2/Manager"
)]
trait Manager {
#[dbus_proxy(object = "Client")]
#[zbus(object = "Client")]
fn get_client(&self);
}
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Client"
)]
trait Client {
fn start(&self) -> Result<()>;
fn stop(&self) -> Result<()>;
#[dbus_proxy(property)]
#[zbus(property)]
fn set_desktop_id(&mut self, id: &str) -> Result<()>;
#[dbus_proxy(signal)]
#[zbus(signal)]
fn location_updated(&self, old: ObjectPath<'_>, new: ObjectPath<'_>) -> Result<()>;
}
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.GeoClue2",
interface = "org.freedesktop.GeoClue2.Location"
)]
trait Location {
#[dbus_proxy(property)]
#[zbus(property)]
fn latitude(&self) -> Result<f64>;
#[dbus_proxy(property)]
#[zbus(property)]
fn longitude(&self) -> Result<f64>;
}
Expand Down Expand Up @@ -308,15 +308,15 @@ LOC) code for getting our location.
### Properties

Interfaces can have associated properties, which can be read or set with the
`org.freedesktop.DBus.Properties` interface. Here again, the `#[dbus_proxy]` attribute comes to the
`org.freedesktop.DBus.Properties` interface. Here again, the `#[proxy]` attribute comes to the
rescue to help you. You can annotate a trait method to be a getter:

```rust,noplayground
# use zbus::{dbus_proxy, Result};
# use zbus::{proxy, Result};
#
#[dbus_proxy]
#[proxy]
trait MyInterface {
#[dbus_proxy(property)]
#[zbus(property)]
fn state(&self) -> Result<String>;
}
```
Expand All @@ -328,17 +328,17 @@ To set the property, prefix the name of the property with `set_`.
For a more real world example, let's try and read two properties from systemd's main service:

```rust,no_run
# use zbus::{Connection, dbus_proxy, Result};
# use zbus::{Connection, proxy, Result};
#
#[dbus_proxy(
#[proxy(
interface = "org.freedesktop.systemd1.Manager",
default_service = "org.freedesktop.systemd1",
default_path = "/org/freedesktop/systemd1"
)]
trait SystemdManager {
#[dbus_proxy(property)]
#[zbus(property)]
fn architecture(&self) -> Result<String>;
#[dbus_proxy(property)]
#[zbus(property)]
fn environment(&self) -> Result<Vec<String>>;
}
Expand Down Expand Up @@ -396,18 +396,18 @@ methods are named after the properties' names: `receive_<prop_name>_changed()`.
Here is an example:

```rust,no_run
# use zbus::{Connection, dbus_proxy, Result};
# use zbus::{Connection, proxy, Result};
# use futures_util::stream::StreamExt;
#
# #[async_std::main]
# async fn main() -> Result<()> {
#[dbus_proxy(
#[proxy(
interface = "org.freedesktop.systemd1.Manager",
default_service = "org.freedesktop.systemd1",
default_path = "/org/freedesktop/systemd1"
)]
trait SystemdManager {
#[dbus_proxy(property)]
#[zbus(property)]
fn log_level(&self) -> Result<String>;
}
Expand Down Expand Up @@ -526,9 +526,9 @@ This will give back effortlessly the corresponding Rust traits boilerplate
code:

```rust,noplayground
# use zbus::dbus_proxy;
# use zbus::proxy;
#
#[dbus_proxy(
#[proxy(
interface = "org.freedesktop.Notifications",
default_service = "org.freedesktop.Notifications",
default_path= "/org/freedesktop/Notifications",
Expand Down Expand Up @@ -557,11 +557,11 @@ trait Notifications {
) -> zbus::Result<u32>;
/// ActionInvoked signal
#[dbus_proxy(signal)]
#[zbus(signal)]
fn action_invoked(&self, arg_0: u32, arg_1: &str) -> zbus::Result<()>;
/// NotificationClosed signal
#[dbus_proxy(signal)]
#[zbus(signal)]
fn notification_closed(&self, arg_0: u32, arg_1: u32) -> zbus::Result<()>;
}
```
Expand All @@ -574,7 +574,7 @@ For example, the generated `GetServerInformation` method can be improved to a ni

```rust,noplayground
# use serde::{Serialize, Deserialize};
# use zbus::{zvariant::Type, dbus_proxy};
# use zbus::{zvariant::Type, proxy};
#
#[derive(Debug, Type, Serialize, Deserialize)]
pub struct ServerInformation {
Expand All @@ -591,7 +591,7 @@ pub struct ServerInformation {
pub spec_version: String,
}
#[dbus_proxy(
#[proxy(
interface = "org.freedesktop.Notifications",
default_service = "org.freedesktop.Notifications",
default_path= "/org/freedesktop/Notifications",
Expand Down
Loading

0 comments on commit 3af251c

Please sign in to comment.