Skip to content

Commit 38ab162

Browse files
committed
Use "named" parameters for subscribe_to_signal
The signal_subscribe callback signature is hard to understand and easy to get subtly wrong, with its four unnamed str arguments. In subscribe_to_signal introduce a new DBusSignalRef struct and put all received parameters into this struct, to provide callback users with "named" arguments which are easier to understand and play well with e.g. editor auto-completion.
1 parent e5a7250 commit 38ab162

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

gio/src/dbus_connection.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ impl Drop for WeakSignalSubscription {
185185
}
186186
}
187187

188+
// rustdoc-stripper-ignore-next
189+
/// An emitted D-Bus signal.
190+
#[derive(Debug, Copy, Clone)]
191+
pub struct DBusSignalRef<'a> {
192+
// rustdoc-stripper-ignore-next
193+
/// The connection the signal was emitted on.
194+
pub connection: &'a DBusConnection,
195+
// rustdoc-stripper-ignore-next
196+
/// The bus name of the sender which emitted the signal.
197+
pub sender_name: &'a str,
198+
// rustdoc-stripper-ignore-next
199+
/// The path of the object on `sender` the signal was emitted from.
200+
pub object_path: &'a str,
201+
// rustdoc-stripper-ignore-next
202+
/// The interface the signal belongs to.
203+
pub interface_name: &'a str,
204+
// rustdoc-stripper-ignore-next
205+
/// The name of the emitted signal.
206+
pub signal_name: &'a str,
207+
// rustdoc-stripper-ignore-next
208+
/// Parameters the signal was emitted with.
209+
pub parameters: &'a glib::Variant,
210+
}
211+
188212
// rustdoc-stripper-ignore-next
189213
/// Build a registered DBus object, by handling different parts of DBus.
190214
#[must_use = "The builder must be built to be used"]
@@ -513,9 +537,7 @@ impl DBusConnection {
513537
/// To avoid reference cycles you may wish to downgrade the returned
514538
/// subscription to a weak one with [`SignalSubscription::downgrade`].
515539
#[must_use]
516-
pub fn subscribe_to_signal<
517-
P: Fn(&DBusConnection, &str, &str, &str, &str, &glib::Variant) + 'static,
518-
>(
540+
pub fn subscribe_to_signal<P: Fn(DBusSignalRef) + 'static>(
519541
&self,
520542
sender: Option<&str>,
521543
interface_name: Option<&str>,
@@ -533,7 +555,16 @@ impl DBusConnection {
533555
object_path,
534556
arg0,
535557
flags,
536-
callback,
558+
move |connection, sender_name, object_path, interface_name, signal_name, parameters| {
559+
callback(DBusSignalRef {
560+
connection,
561+
sender_name,
562+
object_path,
563+
interface_name,
564+
signal_name,
565+
parameters,
566+
})
567+
},
537568
);
538569
SignalSubscription(self.clone(), Some(id))
539570
}

gio/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ mod dbus;
3232
pub use self::dbus::*;
3333
mod dbus_connection;
3434
pub use self::dbus_connection::{
35-
ActionGroupExportId, FilterId, MenuModelExportId, RegistrationBuilder, RegistrationId,
36-
SignalSubscription, SignalSubscriptionId, WatcherId, WeakSignalSubscription,
35+
ActionGroupExportId, DBusSignalRef, FilterId, MenuModelExportId, RegistrationBuilder,
36+
RegistrationId, SignalSubscription, SignalSubscriptionId, WatcherId, WeakSignalSubscription,
3737
};
3838
mod dbus_message;
3939
mod dbus_method_invocation;

0 commit comments

Comments
 (0)