Skip to content

Commit 1f628e1

Browse files
bors[bot]Bromeon
andauthored
Merge #151
151: Rename `Array` -> `VariantArray`, `TypedArray` -> `Array` r=Bromeon a=Bromeon Suggests that `Array<T>` is the default and preferred way of using arrays (with type-safe elements), while `VariantArray` is more a special case of untyped arrays. We probably need to observe usage to see whether this makes sense. An alternative would be `VArray` instead of `VariantArray`, but that would require a frequent occurrence so that the shortened identifier pays off. bors try Co-authored-by: Jan Haller <[email protected]>
2 parents fb15aef + 719d06b commit 1f628e1

File tree

10 files changed

+97
-96
lines changed

10 files changed

+97
-96
lines changed

godot-codegen/src/class_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ fn make_enums(enums: &Option<Vec<Enum>>, _class_name: &TyName, _ctx: &Context) -
431431
fn make_special_builtin_methods(class_name: &TyName, _ctx: &Context) -> TokenStream {
432432
if class_name.godot_ty == "Array" {
433433
quote! {
434-
pub fn from_outer_typed<T>(outer: &TypedArray<T>) -> Self
434+
pub fn from_outer_typed<T>(outer: &Array<T>) -> Self
435435
where T: crate::builtin::meta::VariantMetadata
436436
{
437437
Self {

godot-codegen/src/util.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ fn to_hardcoded_rust_type(ty: &str) -> Option<&str> {
177177
"int" => "i64",
178178
"float" => "f64",
179179
"String" => "GodotString",
180+
"Array" => "VariantArray",
180181
//"enum::Error" => "GodotError",
181182
"enum::Variant.Type" => "VariantType",
182-
"enum::Variant.Operator" => "VariantOperator", // currently not used, but future-proof
183-
"enum::Vector3.Axis" => "Vector3Axis", // TODO automate this
183+
"enum::Variant.Operator" => "VariantOperator",
184+
"enum::Vector3.Axis" => "Vector3Axis",
184185
_ => return None,
185186
};
186187
Some(result)
@@ -251,10 +252,10 @@ fn to_rust_type_uncached(ty: &str, ctx: &mut Context) -> RustTy {
251252

252253
let rust_elem_ty = to_rust_type(elem_ty, ctx);
253254
return if ctx.is_builtin(elem_ty) {
254-
RustTy::BuiltinArray(quote! { TypedArray<#rust_elem_ty> })
255+
RustTy::BuiltinArray(quote! { Array<#rust_elem_ty> })
255256
} else {
256257
RustTy::EngineArray {
257-
tokens: quote! { TypedArray<#rust_elem_ty> },
258+
tokens: quote! { Array<#rust_elem_ty> },
258259
elem_class: elem_ty.to_string(),
259260
}
260261
};

godot-core/src/builtin/array.rs

Lines changed: 53 additions & 53 deletions
Large diffs are not rendered by default.

godot-core/src/builtin/dictionary.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::ptr::addr_of_mut;
1414
use sys::types::OpaqueDictionary;
1515
use sys::{ffi_methods, interface_fn, GodotFfi};
1616

17-
use super::Array;
17+
use super::VariantArray;
1818

1919
/// Godot's `Dictionary` type.
2020
///
@@ -23,7 +23,7 @@ use super::Array;
2323
///
2424
/// # Thread safety
2525
///
26-
/// The same principles apply as for [`Array`]. Consult its documentation for details.
26+
/// The same principles apply as for [`VariantArray`]. Consult its documentation for details.
2727
#[repr(C)]
2828
pub struct Dictionary {
2929
opaque: OpaqueDictionary,
@@ -131,7 +131,7 @@ impl Dictionary {
131131
/// Returns `true` if the dictionary contains all the given keys.
132132
///
133133
/// _Godot equivalent: `has_all`_
134-
pub fn contains_all_keys(&self, keys: Array) -> bool {
134+
pub fn contains_all_keys(&self, keys: VariantArray) -> bool {
135135
self.as_inner().has_all(keys)
136136
}
137137

@@ -143,14 +143,14 @@ impl Dictionary {
143143
/// Creates a new `Array` containing all the keys currently in the dictionary.
144144
///
145145
/// _Godot equivalent: `keys`_
146-
pub fn keys_array(&self) -> Array {
146+
pub fn keys_array(&self) -> VariantArray {
147147
self.as_inner().keys()
148148
}
149149

150150
/// Creates a new `Array` containing all the values currently in the dictionary.
151151
///
152152
/// _Godot equivalent: `values`_
153-
pub fn values_array(&self) -> Array {
153+
pub fn values_array(&self) -> VariantArray {
154154
self.as_inner().values()
155155
}
156156

@@ -464,7 +464,7 @@ impl<'a> Keys<'a> {
464464
}
465465

466466
/// Returns an array of the keys
467-
pub fn array(self) -> Array {
467+
pub fn array(self) -> VariantArray {
468468
// Can only be called
469469
assert!(self.iter.is_first);
470470
self.iter.dictionary.keys_array()

godot-core/src/builtin/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// Re-export macros.
3636
pub use crate::{array, dict, varray};
3737

38-
pub use array_inner::{Array, TypedArray};
38+
pub use array_inner::{Array, VariantArray};
3939
pub use basis::*;
4040
pub use color::*;
4141
pub use dictionary_inner::Dictionary;

godot-core/src/builtin/packed_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ macro_rules! impl_packed_array {
376376
}
377377
}
378378

379-
impl_builtin_froms!($PackedArray; Array => $from_array);
379+
impl_builtin_froms!($PackedArray; VariantArray => $from_array);
380380

381381
impl fmt::Debug for $PackedArray {
382382
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

itest/rust/src/array_test.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use godot::prelude::*;
99

1010
#[itest]
1111
fn array_default() {
12-
assert_eq!(Array::default().len(), 0);
12+
assert_eq!(VariantArray::default().len(), 0);
1313
}
1414

1515
#[itest]
1616
fn array_new() {
17-
assert_eq!(Array::new().len(), 0);
17+
assert_eq!(VariantArray::new().len(), 0);
1818
}
1919

2020
#[itest]
@@ -31,22 +31,22 @@ fn array_eq() {
3131
fn typed_array_from_to_variant() {
3232
let array = array![1, 2];
3333
let variant = array.to_variant();
34-
let result = TypedArray::try_from_variant(&variant);
34+
let result = Array::try_from_variant(&variant);
3535
assert_eq!(result, Ok(array));
3636
}
3737

3838
#[itest]
3939
fn untyped_array_from_to_variant() {
4040
let array = varray![1, 2];
4141
let variant = array.to_variant();
42-
let result = Array::try_from_variant(&variant);
42+
let result = VariantArray::try_from_variant(&variant);
4343
assert_eq!(result, Ok(array));
4444
}
4545

4646
#[itest]
4747
fn array_from_packed_array() {
4848
let packed_array = PackedInt32Array::from(&[42]);
49-
let mut array = Array::from(&packed_array);
49+
let mut array = VariantArray::from(&packed_array);
5050
// This tests that the resulting array doesn't secretly have a runtime type assigned to it,
5151
// which is not reflected in our static type. It would make sense if it did, but Godot decided
5252
// otherwise: we get an untyped array.
@@ -56,7 +56,7 @@ fn array_from_packed_array() {
5656

5757
#[itest]
5858
fn array_from_iterator() {
59-
let array = TypedArray::from_iter([1, 2]);
59+
let array = Array::from_iter([1, 2]);
6060

6161
assert_eq!(array.len(), 2);
6262
assert_eq!(array.get(0), 1);
@@ -65,7 +65,7 @@ fn array_from_iterator() {
6565

6666
#[itest]
6767
fn array_from_slice() {
68-
let array = TypedArray::from(&[1, 2]);
68+
let array = Array::from(&[1, 2]);
6969

7070
assert_eq!(array.len(), 2);
7171
assert_eq!(array.get(0), 1);
@@ -108,7 +108,7 @@ fn array_duplicate_shallow() {
108108
let subarray = array![2, 3];
109109
let array = varray![1, subarray];
110110
let duplicate = array.duplicate_shallow();
111-
TypedArray::<i64>::try_from_variant(&duplicate.get(1))
111+
Array::<i64>::try_from_variant(&duplicate.get(1))
112112
.unwrap()
113113
.set(0, 4);
114114
assert_eq!(subarray.get(0), 4);
@@ -119,7 +119,7 @@ fn array_duplicate_deep() {
119119
let subarray = array![2, 3];
120120
let array = varray![1, subarray];
121121
let duplicate = array.duplicate_deep();
122-
TypedArray::<i64>::try_from_variant(&duplicate.get(1))
122+
Array::<i64>::try_from_variant(&duplicate.get(1))
123123
.unwrap()
124124
.set(0, 4);
125125
assert_eq!(subarray.get(0), 2);
@@ -134,7 +134,7 @@ fn array_slice_shallow() {
134134
let subarray = array![2, 3];
135135
let array = varray![1, subarray];
136136
let slice = array.slice_shallow(1, 2, None);
137-
TypedArray::<i64>::try_from_variant(&slice.get(0))
137+
Array::<i64>::try_from_variant(&slice.get(0))
138138
.unwrap()
139139
.set(0, 4);
140140
assert_eq!(subarray.get(0), 4);
@@ -149,7 +149,7 @@ fn array_slice_deep() {
149149
let subarray = array![2, 3];
150150
let array = varray![1, subarray];
151151
let slice = array.slice_deep(1, 2, None);
152-
TypedArray::<i64>::try_from_variant(&slice.get(0))
152+
Array::<i64>::try_from_variant(&slice.get(0))
153153
.unwrap()
154154
.set(0, 4);
155155
assert_eq!(subarray.get(0), 2);
@@ -173,7 +173,7 @@ fn array_first_last() {
173173
assert_eq!(array.first(), Some(1));
174174
assert_eq!(array.last(), Some(2));
175175

176-
let empty_array = Array::new();
176+
let empty_array = VariantArray::new();
177177

178178
assert_eq!(empty_array.first(), None);
179179
assert_eq!(empty_array.last(), None);
@@ -220,15 +220,15 @@ fn array_min_max() {
220220
assert_eq!(uncomparable_array.min(), None);
221221
assert_eq!(uncomparable_array.max(), None);
222222

223-
let empty_array = Array::new();
223+
let empty_array = VariantArray::new();
224224

225225
assert_eq!(empty_array.min(), None);
226226
assert_eq!(empty_array.max(), None);
227227
}
228228

229229
#[itest]
230230
fn array_pick_random() {
231-
assert_eq!(Array::new().pick_random(), None);
231+
assert_eq!(VariantArray::new().pick_random(), None);
232232
assert_eq!(array![1].pick_random(), Some(1));
233233
}
234234

@@ -330,10 +330,7 @@ fn array_mixed_values() {
330330
PackedByteArray::try_from_variant(&array.get(2)).unwrap(),
331331
packed_array
332332
);
333-
assert_eq!(
334-
TypedArray::try_from_variant(&array.get(3)).unwrap(),
335-
typed_array
336-
);
333+
assert_eq!(Array::try_from_variant(&array.get(3)).unwrap(), typed_array);
337334
assert_eq!(
338335
Gd::<Object>::try_from_variant(&array.get(4))
339336
.unwrap()
@@ -384,7 +381,7 @@ fn untyped_array_return_from_godot_func() {
384381
assert_eq!(result, varray![child, Variant::nil(), NodePath::default()]);
385382
}
386383

387-
// TODO All API functions that take a `TypedArray` are even more obscure and not included in
384+
// TODO All API functions that take a `Array` are even more obscure and not included in
388385
// `SELECTED_CLASSES`. Decide if this test is worth having `Texture2DArray` and `Image` and their
389386
// ancestors in the list.
390387
#[itest]
@@ -431,22 +428,22 @@ struct ArrayTest;
431428
#[godot_api]
432429
impl ArrayTest {
433430
#[func]
434-
fn pass_untyped_array(&self, array: Array) -> i64 {
431+
fn pass_untyped_array(&self, array: VariantArray) -> i64 {
435432
array.len().try_into().unwrap()
436433
}
437434

438435
#[func]
439-
fn return_untyped_array(&self) -> Array {
436+
fn return_untyped_array(&self) -> VariantArray {
440437
varray![42, "answer"]
441438
}
442439

443440
#[func]
444-
fn pass_typed_array(&self, array: TypedArray<i64>) -> i64 {
441+
fn pass_typed_array(&self, array: Array<i64>) -> i64 {
445442
array.iter_shared().sum()
446443
}
447444

448445
#[func]
449-
fn return_typed_array(&self, n: i64) -> TypedArray<i64> {
446+
fn return_typed_array(&self, n: i64) -> Array<i64> {
450447
(1..(n + 1)).collect()
451448
}
452449
}

itest/rust/src/builtin_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn test_builtins_vector2() {
2525

2626
#[itest]
2727
fn test_builtins_array() {
28-
let array = Array::default();
28+
let array = VariantArray::default();
2929
let mut inner: InnerArray = array.as_inner();
3030

3131
let a = 7.to_variant();

itest/rust/src/runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use godot::bind::{godot_api, GodotClass};
8-
use godot::builtin::{Array, ToVariant, Variant};
8+
use godot::builtin::{ToVariant, Variant, VariantArray};
99

1010
use crate::RustTestCase;
1111
use std::time::{Duration, Instant};
@@ -25,7 +25,7 @@ impl IntegrationTests {
2525
#[func]
2626
fn run_all_tests(
2727
&mut self,
28-
gdscript_tests: Array,
28+
gdscript_tests: VariantArray,
2929
gdscript_file_count: i64,
3030
allow_focus: bool,
3131
) -> bool {
@@ -72,7 +72,7 @@ impl IntegrationTests {
7272
}
7373
}
7474

75-
fn run_gdscript_tests(&mut self, tests: Array) {
75+
fn run_gdscript_tests(&mut self, tests: VariantArray) {
7676
let mut last_file = None;
7777
for test in tests.iter_shared() {
7878
let result = test.call("run", &[]);

itest/rust/src/variant_test.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use godot::builtin::{
1010
};
1111
use godot::engine::Node2D;
1212
use godot::obj::InstanceId;
13-
use godot::prelude::{Array, Basis, Dictionary, VariantConversionError};
13+
use godot::prelude::{Basis, Dictionary, VariantArray, VariantConversionError};
1414
use godot::sys::{GodotFfi, VariantOperator, VariantType};
1515
use std::cmp::Ordering;
1616
use std::fmt::{Debug, Display};
@@ -303,7 +303,7 @@ fn variant_conversion_fails() {
303303
Err(VariantConversionError)
304304
);
305305
assert_eq!(
306-
Array::default().to_variant().try_to::<StringName>(),
306+
VariantArray::default().to_variant().try_to::<StringName>(),
307307
Err(VariantConversionError)
308308
);
309309
//assert_eq!(
@@ -327,7 +327,10 @@ fn variant_type_correct() {
327327
StringName::from("string_name").to_variant().get_type(),
328328
VariantType::StringName
329329
);
330-
assert_eq!(Array::default().to_variant().get_type(), VariantType::Array);
330+
assert_eq!(
331+
VariantArray::default().to_variant().get_type(),
332+
VariantType::Array
333+
);
331334
assert_eq!(
332335
Dictionary::default().to_variant().get_type(),
333336
VariantType::Dictionary

0 commit comments

Comments
 (0)