Skip to content

Commit 43b4e8b

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 ef3f231 commit 43b4e8b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

gio/src/dbus_connection.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ impl Drop for WeakSignalSubscription {
181181
}
182182
}
183183

184+
/// An emitted D-Bus signal.
185+
#[derive(Debug, Copy, Clone)]
186+
pub struct DBusSignalRef<'a> {
187+
/// The connection the signal was emitted on.
188+
pub connection: &'a DBusConnection,
189+
/// The bus name of the sender which emitted the signal.
190+
pub sender_name: &'a str,
191+
/// The path of the object on `sender` the signal was emitted from.
192+
pub object_path: &'a str,
193+
/// The interface the signal belongs to.
194+
pub interface_name: &'a str,
195+
/// The name of the emitted signal.
196+
pub signal_name: &'a str,
197+
/// Parameters the signal was emitted with.
198+
pub parameters: &'a glib::Variant,
199+
}
200+
184201
// rustdoc-stripper-ignore-next
185202
/// Build a registered DBus object, by handling different parts of DBus.
186203
#[must_use = "The builder must be built to be used"]
@@ -508,9 +525,7 @@ impl DBusConnection {
508525
/// To avoid reference cycles you may wish to downgrade the returned
509526
/// subscription to a weak one with [`SignalSubscription::downgrade`].
510527
#[must_use]
511-
pub fn subscribe_to_signal<
512-
P: Fn(&DBusConnection, &str, &str, &str, &str, &glib::Variant) + 'static,
513-
>(
528+
pub fn subscribe_to_signal<P: Fn(DBusSignalRef) + 'static>(
514529
&self,
515530
sender: Option<&str>,
516531
interface_name: Option<&str>,
@@ -528,7 +543,16 @@ impl DBusConnection {
528543
object_path,
529544
arg0,
530545
flags,
531-
callback,
546+
move |connection, sender_name, object_path, interface_name, signal_name, parameters| {
547+
callback(DBusSignalRef {
548+
connection,
549+
sender_name,
550+
object_path,
551+
interface_name,
552+
signal_name,
553+
parameters,
554+
})
555+
},
532556
);
533557
SignalSubscription(self.clone(), Some(id))
534558
}

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)