Skip to content

Commit 98dcdda

Browse files
authored
Merge pull request #1175 from godot-rust/qol/connect-gd-by-value
`ConnectBuilder::connect_*_gd()` takes `Gd` instead of `&mut Gd`
2 parents bb39acd + 20332d2 commit 98dcdda

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

godot-codegen/src/generator/signals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn make_with_signals_impl(
131131

132132
// Used outside, to document class with links to this type.
133133
pub fn make_collection_name(class_name: &TyName) -> Ident {
134-
format_ident!("SignalsIn{}", class_name.rust_ty)
134+
format_ident!("SignalsOf{}", class_name.rust_ty)
135135
}
136136

137137
fn make_individual_struct_name(signal_name: &str) -> Ident {

godot-core/src/registry/signal/connect_builder.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ macro_rules! impl_builder_connect {
183183
F: FnMut(&mut C, $($Ps),*) -> R + 'static,
184184
{
185185
let mut gd = self.parent_sig.receiver_object();
186+
186187
let godot_fn = make_godot_fn(move |($($args,)*): ($($Ps,)*)| {
187188
let mut guard = Gd::bind_mut(&mut gd);
188189
function(&mut *guard, $($args),*);
@@ -201,11 +202,12 @@ macro_rules! impl_builder_connect {
201202
/// - If you need cross-thread signals, use [`connect_sync()`](#method.connect_sync) instead (requires feature `experimental-threads`).
202203
pub fn connect_self_gd<F, R>(self, mut function: F)
203204
where
204-
F: FnMut(&mut Gd<C>, $($Ps),*) -> R + 'static,
205+
F: FnMut(Gd<C>, $($Ps),*) -> R + 'static,
205206
{
206-
let mut gd = self.parent_sig.receiver_object();
207+
let gd = self.parent_sig.receiver_object();
208+
207209
let godot_fn = make_godot_fn(move |($($args,)*): ($($Ps,)*)| {
208-
function(&mut gd, $($args),*);
210+
function(gd.clone(), $($args),*);
209211
});
210212

211213
self.inner_connect_godot_fn::<F>(godot_fn);
@@ -253,12 +255,12 @@ macro_rules! impl_builder_connect {
253255
pub fn connect_other_gd<F, R, OtherC>(self, object: &impl ToSignalObj<OtherC>, mut method: F)
254256
where
255257
OtherC: GodotClass,
256-
F: FnMut(&mut Gd<OtherC>, $($Ps),*) -> R + 'static,
258+
F: FnMut(Gd<OtherC>, $($Ps),*) -> R + 'static,
257259
{
258-
let mut gd = object.to_signal_obj();
260+
let gd = object.to_signal_obj();
259261

260262
let godot_fn = make_godot_fn(move |($($args,)*): ($($Ps,)*)| {
261-
method(&mut gd, $($args),*);
263+
method(gd.clone(), $($args),*);
262264
});
263265

264266
self.inner_connect_godot_fn::<F>(godot_fn);

itest/rust/src/builtin_tests/containers/signal_test.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ fn signal_symbols_internal() {
5959
// Check that closure is invoked.
6060
assert_eq!(tracker.get(), 1234, "Emit failed (closure)");
6161

62-
// Check that instance method is invoked.
62+
// Check that instance methods self_receive() and self_receive_gd_inc1() are invoked.
6363
assert_eq!(
6464
emitter.bind().last_received_int,
65-
1234,
65+
1234 + 1, // self_receive_gd_inc1() increments by 1, and should be called after self_receive().
6666
"Emit failed (method)"
6767
);
6868

@@ -400,7 +400,7 @@ fn signal_symbols_connect_inferred() {
400400
.tree_exiting()
401401
.builder()
402402
.flags(ConnectFlags::DEFERRED)
403-
.connect_self_gd(|this| {
403+
.connect_self_gd(|mut this| {
404404
// Use methods that `Node` declares.
405405
let _ = this.get_path(); // ref.
406406
this.set_unique_name_in_owner(true); // mut.
@@ -422,7 +422,7 @@ fn signal_symbols_connect_inferred() {
422422
.signals()
423423
.tree_exiting()
424424
.builder()
425-
.connect_other_gd(&user, |this| {
425+
.connect_other_gd(&user, |mut this| {
426426
// Use methods that `Node` declares.
427427
let _ = this.get_path(); // ref.
428428
this.set_unique_name_in_owner(true); // mut.
@@ -513,6 +513,14 @@ mod emitter {
513513
}
514514
}
515515

516+
#[func]
517+
pub fn self_receive_gd_inc1(mut this: Gd<Self>, _arg1: i64) {
518+
#[cfg(since_api = "4.2")]
519+
{
520+
this.bind_mut().last_received_int += 1;
521+
}
522+
}
523+
516524
#[func]
517525
pub fn self_receive_constant(&mut self) {
518526
#[cfg(since_api = "4.2")]
@@ -534,6 +542,7 @@ mod emitter {
534542
sig.connect_self(Self::self_receive);
535543
sig.connect(Self::self_receive_static);
536544
sig.connect(move |i| tracker.set(i));
545+
sig.builder().connect_self_gd(Self::self_receive_gd_inc1);
537546
}
538547

539548
#[cfg(since_api = "4.2")]

0 commit comments

Comments
 (0)