Skip to content

Commit 61dbd05

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 5792bef commit 61dbd05

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
@@ -187,6 +187,30 @@ impl Drop for WeakSignalSubscription {
187187
}
188188
}
189189

190+
// rustdoc-stripper-ignore-next
191+
/// An emitted D-Bus signal.
192+
#[derive(Debug, Copy, Clone)]
193+
pub struct DBusSignalRef<'a> {
194+
// rustdoc-stripper-ignore-next
195+
/// The connection the signal was emitted on.
196+
pub connection: &'a DBusConnection,
197+
// rustdoc-stripper-ignore-next
198+
/// The bus name of the sender which emitted the signal.
199+
pub sender_name: &'a str,
200+
// rustdoc-stripper-ignore-next
201+
/// The path of the object on `sender` the signal was emitted from.
202+
pub object_path: &'a str,
203+
// rustdoc-stripper-ignore-next
204+
/// The interface the signal belongs to.
205+
pub interface_name: &'a str,
206+
// rustdoc-stripper-ignore-next
207+
/// The name of the emitted signal.
208+
pub signal_name: &'a str,
209+
// rustdoc-stripper-ignore-next
210+
/// Parameters the signal was emitted with.
211+
pub parameters: &'a glib::Variant,
212+
}
213+
190214
// rustdoc-stripper-ignore-next
191215
/// Build a registered DBus object, by handling different parts of DBus.
192216
#[must_use = "The builder must be built to be used"]
@@ -515,9 +539,7 @@ impl DBusConnection {
515539
/// To avoid reference cycles you may wish to downgrade the returned
516540
/// subscription to a weak one with [`SignalSubscription::downgrade`].
517541
#[must_use]
518-
pub fn subscribe_to_signal<
519-
P: Fn(&DBusConnection, &str, &str, &str, &str, &glib::Variant) + 'static,
520-
>(
542+
pub fn subscribe_to_signal<P: Fn(DBusSignalRef) + 'static>(
521543
&self,
522544
sender: Option<&str>,
523545
interface_name: Option<&str>,
@@ -535,7 +557,16 @@ impl DBusConnection {
535557
object_path,
536558
arg0,
537559
flags,
538-
callback,
560+
move |connection, sender_name, object_path, interface_name, signal_name, parameters| {
561+
callback(DBusSignalRef {
562+
connection,
563+
sender_name,
564+
object_path,
565+
interface_name,
566+
signal_name,
567+
parameters,
568+
});
569+
},
539570
);
540571
SignalSubscription(self.clone(), Some(id))
541572
}

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)