Skip to content

Commit 6332fab

Browse files
bors[bot]chitoyuu
andauthored
Merge #819
819: Re-organize Variant conversion methods r=Bromeon a=chitoyuu Inherent conversion methods are now genericized into `new`, `to`, `try_to`, and `coerce_to`, to reduce the clutter and make other `Variant` methods more discoverable in docs. - `new` replaces the `from_*` methods. The original version that returns nil variants is renamed to `Variant::nil`, to better reflect its behavior. - `to` and `try_to` replaces the `try_to_*` methods, with naming more consistent with the rest of the Rust ecosystem. - `to_object` and `try_to_object` are kept for convenience around `Ref`. - `coerce_to` replaces the `to_*` methods. A new trait `CoerceFromVariant` is introduced and implemented for all GDScript built-in types. - Documentation is updated around convertion methods/traits to highlight what is used for exported methods. Close #774 Rare PR with negative LoC Co-authored-by: Chitose Yuuzaki <[email protected]>
2 parents deb9dbf + 9f434d0 commit 6332fab

File tree

23 files changed

+310
-589
lines changed

23 files changed

+310
-589
lines changed

bindings_generator/src/methods.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ mod varargs_call {
628628
let #name: Variant = Variant::from_object_ptr(#name);
629629
}
630630
}
631-
Ty::String => {
632-
quote! {
633-
let #name: Variant = Variant::from_godot_string(&#name);
634-
}
635-
}
636631
_ => {
637632
quote! {
638633
let #name: Variant = (&#name).to_variant();
@@ -706,11 +701,6 @@ mod varcall {
706701
let #name: Variant = Variant::from_object_ptr(#name);
707702
}
708703
}
709-
Ty::String => {
710-
quote! {
711-
let #name: Variant = Variant::from_godot_string(&#name);
712-
}
713-
}
714704
_ => {
715705
quote! {
716706
let #name: Variant = (&#name).to_variant();

examples/rpc/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl ServerPuppet {
4646

4747
#[export]
4848
fn on_connected_to_server(&mut self, owner: TRef<Node>) {
49-
owner.rpc("greet_server", &[Variant::from_str("hello")]);
49+
owner.rpc("greet_server", &[Variant::new("hello")]);
5050
}
5151

5252
#[export(rpc = "puppet")]

examples/rpc/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Server {
3838
owner.rpc_id(
3939
tree.get_rpc_sender_id(),
4040
"return_greeting",
41-
&[Variant::from_str("hello")],
41+
&[Variant::new("hello")],
4242
);
4343
}
4444
}

examples/scene_create/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,9 @@ fn update_panel(owner: &Spatial, num_children: i64) {
140140
let panel_node = unsafe { panel_node.assume_safe() };
141141

142142
// Put the Node
143-
let mut as_variant = Variant::from_object(panel_node);
144-
let result = unsafe {
145-
as_variant.call(
146-
"set_num_children",
147-
&[Variant::from_u64(num_children as u64)],
148-
)
149-
};
143+
let mut as_variant = Variant::new(panel_node);
144+
let result =
145+
unsafe { as_variant.call("set_num_children", &[Variant::new(num_children as u64)]) };
150146

151147
match result {
152148
Ok(_) => godot_print!("Called Panel OK."),

examples/signals/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl SignalEmitter {
2222
// Argument list used by the editor for GUI and generation of GDScript handlers. It can be omitted if the signal is only used from code.
2323
args: &[SignalArgument {
2424
name: "data",
25-
default: Variant::from_i64(100),
25+
default: Variant::new(100),
2626
export_info: ExportInfo::new(VariantType::I64),
2727
usage: PropertyUsage::DEFAULT,
2828
}],
@@ -48,7 +48,7 @@ impl SignalEmitter {
4848
if self.data % 2 == 0 {
4949
owner.emit_signal("tick", &[]);
5050
} else {
51-
owner.emit_signal("tick_with_data", &[Variant::from_i64(self.data)]);
51+
owner.emit_signal("tick_with_data", &[Variant::new(self.data)]);
5252
}
5353
}
5454
}
@@ -96,7 +96,7 @@ impl SignalSubscriber {
9696
fn notify_with_data(&mut self, owner: &Label, data: Variant) {
9797
let msg = format!(
9898
"Received signal \"tick_with_data\" with data {}",
99-
data.try_to_u64().unwrap()
99+
data.try_to::<u64>().unwrap()
100100
);
101101

102102
owner.set_text(msg);

gdnative-async/src/method.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,22 @@ impl<C: NativeClass, F: AsyncMethod<C>> Method<C> for Async<F> {
106106
Self::site().unwrap_or_default(),
107107
format_args!("unable to spawn future: {}", err),
108108
);
109-
Variant::new()
109+
Variant::nil()
110110
}
111111
None => {
112112
log::error(
113113
Self::site().unwrap_or_default(),
114114
format_args!("implementation did not spawn a future"),
115115
);
116-
Variant::new()
116+
Variant::nil()
117117
}
118118
}
119119
} else {
120120
log::error(
121121
Self::site().unwrap_or_default(),
122122
"a global executor must be set before any async methods can be called on this thread",
123123
);
124-
Variant::new()
124+
Variant::nil()
125125
}
126126
}
127127
}

gdnative-async/src/rt/bridge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Method<SignalBridge> for OnSignalFn {
125125
})
126126
.unwrap();
127127

128-
Variant::new()
128+
Variant::nil()
129129
}
130130

131131
fn site() -> Option<gdnative_core::log::Site<'static>> {

gdnative-async/src/rt/func_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl NativeClass for FuncState {
3636
name: "completed",
3737
args: &[SignalArgument {
3838
name: "value",
39-
default: Variant::new(),
39+
default: Variant::nil(),
4040
export_info: ExportInfo::new(VariantType::Nil),
4141
usage: PropertyUsage::DEFAULT,
4242
}],

gdnative-core/src/core_types/dictionary.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ impl<Own: Ownership> Dictionary<Own> {
9595
}
9696

9797
/// Returns a copy of the element corresponding to the key, or `Nil` if it doesn't exist.
98-
/// Shorthand for `self.get_or(key, Variant::new())`.
98+
/// Shorthand for `self.get_or(key, Variant::nil())`.
9999
#[inline]
100100
pub fn get_or_nil<K>(&self, key: K) -> Variant
101101
where
102102
K: OwnedToVariant + ToVariantEq,
103103
{
104-
self.get_or(key, Variant::new())
104+
self.get_or(key, Variant::nil())
105105
}
106106

107107
/// Update an existing element corresponding to the key.
@@ -534,12 +534,12 @@ godot_test!(test_dictionary {
534534
use std::collections::HashSet;
535535

536536
use crate::core_types::VariantType;
537-
let foo = Variant::from_str("foo");
538-
let bar = Variant::from_str("bar");
539-
let nope = Variant::from_str("nope");
537+
let foo = Variant::new("foo");
538+
let bar = Variant::new("bar");
539+
let nope = Variant::new("nope");
540540

541-
let x = Variant::from_i64(42);
542-
let y = Variant::from_i64(1337);
541+
let x = Variant::new(42);
542+
let y = Variant::new(1337);
543543

544544
let dict = Dictionary::new();
545545

@@ -551,7 +551,7 @@ godot_test!(test_dictionary {
551551
assert!(!dict.contains(&nope));
552552

553553
let keys_array = dict.keys();
554-
let baz = Variant::from_str("baz");
554+
let baz = Variant::new("baz");
555555
keys_array.push(&baz);
556556
dict.insert(&baz, &x);
557557

@@ -561,14 +561,14 @@ godot_test!(test_dictionary {
561561

562562
assert!(!dict.contains_all(&keys_array));
563563

564-
let variant = Variant::from_dictionary(&dict.duplicate().into_shared());
564+
let variant = Variant::new(&dict.duplicate().into_shared());
565565
assert!(variant.get_type() == VariantType::Dictionary);
566566

567567
let dict2 = dict.duplicate();
568568
assert!(dict2.contains(&foo));
569569
assert!(dict2.contains(&bar));
570570

571-
if let Some(dic_variant) = variant.try_to_dictionary() {
571+
if let Ok(dic_variant) = variant.try_to::<Dictionary>() {
572572
assert!(dic_variant.len() == dict.len());
573573
} else {
574574
panic!("variant should be a Dictionary");

gdnative-core/src/core_types/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,13 @@ godot_test!(test_string {
695695
assert_eq!(index_string[1], 'a');
696696
assert_eq!(index_string[2], 'r');
697697

698-
let variant = Variant::from_godot_string(&foo);
698+
let variant = Variant::new(&foo);
699699
assert!(variant.get_type() == VariantType::GodotString);
700700

701701
let variant2: Variant = "foo".to_variant();
702702
assert!(variant == variant2);
703703

704-
if let Some(foo_variant) = variant.try_to_godot_string() {
704+
if let Ok(foo_variant) = variant.try_to::<GodotString>() {
705705
assert!(foo_variant == foo);
706706
} else {
707707
panic!("variant should be a GodotString");

0 commit comments

Comments
 (0)