Skip to content

Commit 856842c

Browse files
committed
Treat Callable as by-ref in APIs
1 parent af8c1e1 commit 856842c

File tree

9 files changed

+23
-23
lines changed

9 files changed

+23
-23
lines changed

examples/dodge-the-creeps/rust/src/hud.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Hud {
2626
self.show_message("Game Over".into());
2727

2828
let mut timer = self.base().get_tree().unwrap().create_timer(2.0).unwrap();
29-
timer.connect("timeout", self.base().callable("show_start_button"));
29+
timer.connect("timeout", &self.base().callable("show_start_button"));
3030
}
3131

3232
#[func]

examples/dodge-the-creeps/rust/src/main_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Main {
102102
mob.set_linear_velocity(Vector2::new(range, 0.0).rotated(real::from_f32(direction)));
103103

104104
let mut hud = self.base().get_node_as::<Hud>("Hud");
105-
hud.connect("start_game", mob.callable("on_start_game"));
105+
hud.connect("start_game", &mob.callable("on_start_game"));
106106
}
107107

108108
fn music(&mut self) -> &mut AudioStreamPlayer {

godot-codegen/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,12 @@ impl<'a> Context<'a> {
247247
// Already handled separately.
248248
debug_assert!(!godot_ty.ty.starts_with("Packed"));
249249

250+
// IMPORTANT: Keep this in sync with impl_ffi_variant!() macros taking `ref` or not.
251+
250252
// Arrays are also handled separately, and use ByRef.
251253
match godot_ty.ty.as_str() {
252-
"Variant" | "Array" | "Dictionary" => ArgPassing::ByRef,
254+
// Note: Signal is currently not used in any parameter, but this may change.
255+
"Variant" | "Array" | "Dictionary" | "Callable" | "Signal" => ArgPassing::ByRef,
253256
"String" | "StringName" | "NodePath" => ArgPassing::ImplAsArg,
254257
_ => ArgPassing::ByValue,
255258
}

godot-core/src/builtin/collections/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl<T: ArrayElement> Array<T> {
614614
///
615615
/// Consider using `sort_custom()` to ensure the sorting order is compatible with
616616
/// your callable's ordering
617-
pub fn bsearch_custom(&self, value: impl AsArg<T>, func: Callable) -> usize {
617+
pub fn bsearch_custom(&self, value: impl AsArg<T>, func: &Callable) -> usize {
618618
meta::arg_into_ref!(value: T);
619619

620620
to_usize(
@@ -646,7 +646,7 @@ impl<T: ArrayElement> Array<T> {
646646
/// Note: The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
647647
/// This means that values considered equal may have their order changed when using `sort_unstable_custom`.
648648
#[doc(alias = "sort_custom")]
649-
pub fn sort_unstable_custom(&mut self, func: Callable) {
649+
pub fn sort_unstable_custom(&mut self, func: &Callable) {
650650
// SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
651651
unsafe { self.as_inner_mut() }.sort_custom(func);
652652
}

godot-core/src/builtin/signal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Signal {
7474
/// returns [`Error::ERR_INVALID_PARAMETER`] and
7575
/// pushes an error message, unless the signal is connected with [`ConnectFlags::REFERENCE_COUNTED`](crate::classes::object::ConnectFlags::REFERENCE_COUNTED).
7676
/// To prevent this, use [`Self::is_connected`] first to check for existing connections.
77-
pub fn connect(&self, callable: Callable, flags: i64) -> Error {
77+
pub fn connect(&self, callable: &Callable, flags: i64) -> Error {
7878
let error = self.as_inner().connect(callable, flags);
7979

8080
Error::from_godot(error as i32)
@@ -83,7 +83,7 @@ impl Signal {
8383
/// Disconnects this signal from the specified [`Callable`].
8484
///
8585
/// If the connection does not exist, generates an error. Use [`Self::is_connected`] to make sure that the connection exists.
86-
pub fn disconnect(&self, callable: Callable) {
86+
pub fn disconnect(&self, callable: &Callable) {
8787
self.as_inner().disconnect(callable);
8888
}
8989

@@ -142,7 +142,7 @@ impl Signal {
142142
}
143143

144144
/// Returns `true` if the specified [`Callable`] is connected to this signal.
145-
pub fn is_connected(&self, callable: Callable) -> bool {
145+
pub fn is_connected(&self, callable: &Callable) -> bool {
146146
self.as_inner().is_connected(callable)
147147
}
148148

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ fn array_should_format_with_display() {
479479
fn array_sort_custom() {
480480
let mut a = array![1, 2, 3, 4];
481481
let func = backwards_sort_callable();
482-
a.sort_unstable_custom(func);
482+
a.sort_unstable_custom(&func);
483483
assert_eq!(a, array![4, 3, 2, 1]);
484484
}
485485

@@ -488,8 +488,8 @@ fn array_sort_custom() {
488488
fn array_binary_search_custom() {
489489
let a = array![5, 4, 2, 1];
490490
let func = backwards_sort_callable();
491-
assert_eq!(a.bsearch_custom(1, func.clone()), 3);
492-
assert_eq!(a.bsearch_custom(3, func), 2);
491+
assert_eq!(a.bsearch_custom(1, &func), 3);
492+
assert_eq!(a.bsearch_custom(3, &func), 2);
493493
}
494494

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

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use godot::builtin::{Callable, GString, Signal, StringName, Variant};
8+
use godot::builtin::{Callable, GString, Signal, StringName};
99
use godot::meta::ToGodot;
1010
use godot::register::{godot_api, GodotClass};
1111
use std::cell::Cell;
@@ -71,18 +71,15 @@ fn signals() {
7171

7272
let args = [
7373
vec![],
74-
vec![Variant::from(987)],
75-
vec![
76-
Variant::from(receiver.clone()),
77-
Variant::from(SIGNAL_ARG_STRING),
78-
],
74+
vec![987.to_variant()],
75+
vec![receiver.to_variant(), SIGNAL_ARG_STRING.to_variant()],
7976
];
8077

8178
for (i, arg) in args.iter().enumerate() {
8279
let signal_name = format!("signal_{i}_arg");
8380
let receiver_name = format!("receive_{i}_arg");
8481

85-
emitter.connect(&signal_name, receiver.callable(receiver_name));
82+
emitter.connect(&signal_name, &receiver.callable(receiver_name));
8683
emitter.emit_signal(&signal_name, arg);
8784

8885
assert!(receiver.bind().used[i].get());
@@ -117,7 +114,7 @@ fn emit_signal() {
117114

118115
object.connect(
119116
&StringName::from("test_signal"), // explicit StringName
120-
Callable::from_object_method(&receiver, "receive_1_arg"),
117+
&Callable::from_object_method(&receiver, "receive_1_arg"),
121118
);
122119
assert_eq!(signal.connections().len(), 1);
123120

@@ -136,7 +133,7 @@ fn connect_signal() {
136133
let signal = Signal::from_object_signal(&object, "test_signal");
137134
let receiver = Receiver::new_alloc();
138135

139-
signal.connect(Callable::from_object_method(&receiver, "receive_1_arg"), 0);
136+
signal.connect(&Callable::from_object_method(&receiver, "receive_1_arg"), 0);
140137
assert_eq!(signal.connections().len(), 1);
141138

142139
object.emit_signal("test_signal", &[987i64.to_variant()]);
@@ -277,7 +274,7 @@ mod custom_callable {
277274

278275
let received = Arc::new(AtomicU32::new(0));
279276
let callable = callable(received.clone());
280-
signal.connect(callable, 0);
277+
signal.connect(&callable, 0);
281278

282279
emit(&mut node);
283280

itest/rust/src/object_tests/object_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ fn double_use_reference() {
10531053
emitter
10541054
.clone()
10551055
.upcast::<Object>()
1056-
.connect("do_use", double_use.callable("use_1"));
1056+
.connect("do_use", &double_use.callable("use_1"));
10571057

10581058
let guard = double_use.bind();
10591059

itest/rust/src/object_tests/reentrant_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn reentrant_emit_succeeds() {
6565
let mut class = ReentrantClass::new_alloc();
6666

6767
let callable = class.callable("second");
68-
class.connect("some_signal", callable);
68+
class.connect("some_signal", &callable);
6969

7070
assert!(!class.bind().first_called_pre);
7171
assert!(!class.bind().first_called_post);

0 commit comments

Comments
 (0)