Skip to content

Commit a1cf2a2

Browse files
committed
Rename Array::get -> at, try_get -> get (closer to Vec)
1 parent 6daa678 commit a1cf2a2

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
lines changed

godot-core/src/builtin/array.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,22 +483,26 @@ impl<T: ArrayElement + FromGodot> Array<T> {
483483
}
484484
}
485485

486-
/// Returns the value at the specified index.
486+
/// ⚠️ Returns the value at the specified index.
487+
///
488+
/// This replaces the `Index` trait, which cannot be implemented for `Array` as references are not guaranteed to remain valid.
487489
///
488490
/// # Panics
489491
///
490-
/// If `index` is out of bounds.
491-
pub fn get(&self, index: usize) -> T {
492-
// Panics on out-of-bounds
492+
/// If `index` is out of bounds. If you want to handle out-of-bounds access, use [`get()`](Self::get) instead.
493+
pub fn at(&self, index: usize) -> T {
494+
// Panics on out-of-bounds.
493495
let ptr = self.ptr(index);
494496

495497
// SAFETY: `ptr` is a live pointer to a variant since `ptr.is_null()` just verified that the index is not out of bounds.
496498
let variant = unsafe { Variant::borrow_var_sys(ptr) };
497499
T::from_variant(variant)
498500
}
499501

500-
/// Returns the value at the specified index or `None` if the index is out-of-bounds.
501-
pub fn try_get(&self, index: usize) -> Option<T> {
502+
/// Returns the value at the specified index, or `None` if the index is out-of-bounds.
503+
///
504+
/// If you know the index is correct, use [`at()`](Self::at) instead.
505+
pub fn get(&self, index: usize) -> Option<T> {
502506
let ptr = self.ptr_or_null(index);
503507
if ptr.is_null() {
504508
None

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ fn array_from_iterator() {
6262
let array = Array::from_iter([1, 2]);
6363

6464
assert_eq!(array.len(), 2);
65-
assert_eq!(array.get(0), 1);
66-
assert_eq!(array.get(1), 2);
65+
assert_eq!(array.at(0), 1);
66+
assert_eq!(array.at(1), 2);
6767
}
6868

6969
#[itest]
7070
fn array_from_slice() {
7171
let array = Array::from(&[1, 2]);
7272

7373
assert_eq!(array.len(), 2);
74-
assert_eq!(array.get(0), 1);
75-
assert_eq!(array.get(1), 2);
74+
assert_eq!(array.at(0), 1);
75+
assert_eq!(array.at(1), 2);
7676
}
7777

7878
#[itest]
@@ -108,29 +108,29 @@ fn array_share() {
108108
let mut array = array![1, 2];
109109
let shared = array.clone();
110110
array.set(0, 3);
111-
assert_eq!(shared.get(0), 3);
111+
assert_eq!(shared.at(0), 3);
112112
}
113113

114114
#[itest]
115115
fn array_duplicate_shallow() {
116116
let subarray = array![2, 3];
117117
let array = varray![1, subarray];
118118
let duplicate = array.duplicate_shallow();
119-
Array::<i64>::try_from_variant(&duplicate.get(1))
119+
Array::<i64>::try_from_variant(&duplicate.at(1))
120120
.unwrap()
121121
.set(0, 4);
122-
assert_eq!(subarray.get(0), 4);
122+
assert_eq!(subarray.at(0), 4);
123123
}
124124

125125
#[itest]
126126
fn array_duplicate_deep() {
127127
let subarray = array![2, 3];
128128
let array = varray![1, subarray];
129129
let duplicate = array.duplicate_deep();
130-
Array::<i64>::try_from_variant(&duplicate.get(1))
130+
Array::<i64>::try_from_variant(&duplicate.at(1))
131131
.unwrap()
132132
.set(0, 4);
133-
assert_eq!(subarray.get(0), 2);
133+
assert_eq!(subarray.at(0), 2);
134134
}
135135

136136
#[itest]
@@ -142,10 +142,10 @@ fn array_subarray_shallow() {
142142
let subarray = array![2, 3];
143143
let array = varray![1, subarray];
144144
let slice = array.subarray_shallow(1, 2, None);
145-
Array::<i64>::try_from_variant(&slice.get(0))
145+
Array::<i64>::try_from_variant(&slice.at(0))
146146
.unwrap()
147147
.set(0, 4);
148-
assert_eq!(subarray.get(0), 4);
148+
assert_eq!(subarray.at(0), 4);
149149
}
150150

151151
#[itest]
@@ -157,30 +157,30 @@ fn array_subarray_deep() {
157157
let subarray = array![2, 3];
158158
let array = varray![1, subarray];
159159
let slice = array.subarray_deep(1, 2, None);
160-
Array::<i64>::try_from_variant(&slice.get(0))
160+
Array::<i64>::try_from_variant(&slice.at(0))
161161
.unwrap()
162162
.set(0, 4);
163-
assert_eq!(subarray.get(0), 2);
163+
assert_eq!(subarray.at(0), 2);
164164
}
165165

166166
#[itest]
167167
fn array_get() {
168168
let array = array![1, 2];
169169

170-
assert_eq!(array.get(0), 1);
171-
assert_eq!(array.get(1), 2);
170+
assert_eq!(array.at(0), 1);
171+
assert_eq!(array.at(1), 2);
172172
expect_panic("Array index 2 out of bounds: length is 2", || {
173-
array.get(2);
173+
array.at(2);
174174
});
175175
}
176176

177177
#[itest]
178178
fn array_try_get() {
179179
let array = array![1, 2];
180180

181-
assert_eq!(array.try_get(0), Some(1));
182-
assert_eq!(array.try_get(1), Some(2));
183-
assert_eq!(array.try_get(2), None);
181+
assert_eq!(array.get(0), Some(1));
182+
assert_eq!(array.get(1), Some(2));
183+
assert_eq!(array.get(2), None);
184184
}
185185

186186
#[itest]
@@ -254,7 +254,7 @@ fn array_set() {
254254
let mut array = array![1, 2];
255255

256256
array.set(0, 3);
257-
assert_eq!(array.get(0), 3);
257+
assert_eq!(array.at(0), 3);
258258

259259
expect_panic("Array index 2 out of bounds: length is 2", move || {
260260
array.set(2, 4);
@@ -340,34 +340,34 @@ fn array_mixed_values() {
340340
user_refc,
341341
];
342342

343-
assert_eq!(i64::try_from_variant(&array.get(0)).unwrap(), int);
344-
assert_eq!(GString::try_from_variant(&array.get(1)).unwrap(), string);
343+
assert_eq!(i64::try_from_variant(&array.at(0)).unwrap(), int);
344+
assert_eq!(GString::try_from_variant(&array.at(1)).unwrap(), string);
345345
assert_eq!(
346-
PackedByteArray::try_from_variant(&array.get(2)).unwrap(),
346+
PackedByteArray::try_from_variant(&array.at(2)).unwrap(),
347347
packed_array
348348
);
349-
assert_eq!(Array::try_from_variant(&array.get(3)).unwrap(), typed_array);
349+
assert_eq!(Array::try_from_variant(&array.at(3)).unwrap(), typed_array);
350350
assert_eq!(
351-
Gd::<Object>::try_from_variant(&array.get(4))
351+
Gd::<Object>::try_from_variant(&array.at(4))
352352
.unwrap()
353353
.instance_id(),
354354
object.instance_id()
355355
);
356356
assert_eq!(
357-
Gd::<Node>::try_from_variant(&array.get(5))
357+
Gd::<Node>::try_from_variant(&array.at(5))
358358
.unwrap()
359359
.instance_id(),
360360
node.instance_id()
361361
);
362362

363363
assert_eq!(
364-
Gd::<RefCounted>::try_from_variant(&array.get(6))
364+
Gd::<RefCounted>::try_from_variant(&array.at(6))
365365
.unwrap()
366366
.instance_id(),
367367
engine_refc.instance_id()
368368
);
369369
assert_eq!(
370-
Gd::<ArrayTest>::try_from_variant(&array.get(7))
370+
Gd::<ArrayTest>::try_from_variant(&array.at(7))
371371
.unwrap()
372372
.instance_id(),
373373
user_refc.instance_id()

itest/rust/src/engine_tests/native_structures_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ fn test_native_structure_pointer_to_array_parameter() {
137137

138138
// Check the result array.
139139
assert_eq!(result.len(), 2);
140-
assert_eq!(result.get(0).get("start"), Some(Variant::from(99)));
141-
assert_eq!(result.get(1).get("start"), Some(Variant::from(700)));
140+
assert_eq!(result.at(0).get("start"), Some(Variant::from(99)));
141+
assert_eq!(result.at(1).get("start"), Some(Variant::from(700)));
142142
}
143143

144144
#[itest]

itest/rust/src/object_tests/virtual_methods_test.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,28 +381,28 @@ fn test_virtual_method_with_return() {
381381
assert_eq!(arr.len(), arr_rust.len());
382382
// can't just assert_eq because the values of some floats change slightly
383383
assert_eq_approx!(
384-
arr.get(0).to::<PackedVector3Array>().get(0),
385-
arr_rust.get(0).to::<PackedVector3Array>().get(0),
384+
arr.at(0).to::<PackedVector3Array>().get(0),
385+
arr_rust.at(0).to::<PackedVector3Array>().get(0),
386386
);
387387
assert_eq_approx!(
388-
real::from_f32(arr.get(2).to::<PackedFloat32Array>().get(3)),
389-
real::from_f32(arr_rust.get(2).to::<PackedFloat32Array>().get(3)),
388+
real::from_f32(arr.at(2).to::<PackedFloat32Array>().get(3)),
389+
real::from_f32(arr_rust.at(2).to::<PackedFloat32Array>().get(3)),
390390
);
391391
assert_eq_approx!(
392-
arr.get(3).to::<PackedColorArray>().get(0),
393-
arr_rust.get(3).to::<PackedColorArray>().get(0),
392+
arr.at(3).to::<PackedColorArray>().get(0),
393+
arr_rust.at(3).to::<PackedColorArray>().get(0),
394394
);
395395
assert_eq_approx!(
396-
arr.get(4).to::<PackedVector2Array>().get(0),
397-
arr_rust.get(4).to::<PackedVector2Array>().get(0),
396+
arr.at(4).to::<PackedVector2Array>().get(0),
397+
arr_rust.at(4).to::<PackedVector2Array>().get(0),
398398
);
399399
assert_eq!(
400-
arr.get(6).to::<PackedByteArray>(),
401-
arr_rust.get(6).to::<PackedByteArray>(),
400+
arr.at(6).to::<PackedByteArray>(),
401+
arr_rust.at(6).to::<PackedByteArray>(),
402402
);
403403
assert_eq!(
404-
arr.get(10).to::<PackedInt32Array>(),
405-
arr_rust.get(10).to::<PackedInt32Array>()
404+
arr.at(10).to::<PackedInt32Array>(),
405+
arr_rust.at(10).to::<PackedInt32Array>()
406406
);
407407
}
408408

0 commit comments

Comments
 (0)