Skip to content

Commit 2e08a8e

Browse files
committed
Rename Array::first -> front, last -> back
Named after Godot methods front/back, also consistent with push_front/pop_front. Interestingly, Rust Vec has first/last, however both VecDeque and LinkedList have front/back, likely due to efficient insertion and FIFO semantics.
1 parent bdeda52 commit 2e08a8e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

godot-core/src/builtin/array.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ impl<T: ArrayElement> Array<T> {
133133
}
134134
}
135135

136+
#[deprecated = "Renamed to `get`."]
137+
pub fn try_get(&self, index: usize) -> Option<T> {
138+
self.get(index)
139+
}
140+
136141
/// Returns `true` if the array contains the given value. Equivalent of `has` in GDScript.
137142
pub fn contains(&self, value: &T) -> bool {
138143
self.as_inner().has(value.to_variant())
@@ -172,27 +177,33 @@ impl<T: ArrayElement> Array<T> {
172177
}
173178

174179
/// Returns the first element in the array, or `None` if the array is empty.
175-
///
176-
/// Equivalent of `front()` in GDScript.
177-
#[doc(alias = "front")]
178-
pub fn first(&self) -> Option<T> {
180+
#[doc(alias = "first")]
181+
pub fn front(&self) -> Option<T> {
179182
(!self.is_empty()).then(|| {
180183
let variant = self.as_inner().front();
181184
T::from_variant(&variant)
182185
})
183186
}
184187

185188
/// Returns the last element in the array, or `None` if the array is empty.
186-
///
187-
/// Equivalent of `back()` in GDScript.
188-
#[doc(alias = "back")]
189-
pub fn last(&self) -> Option<T> {
189+
#[doc(alias = "last")]
190+
pub fn back(&self) -> Option<T> {
190191
(!self.is_empty()).then(|| {
191192
let variant = self.as_inner().back();
192193
T::from_variant(&variant)
193194
})
194195
}
195196

197+
#[deprecated = "Renamed to `front`, in line with GDScript method and consistent with `push_front` and `pop_front`."]
198+
pub fn first(&self) -> Option<T> {
199+
self.front()
200+
}
201+
202+
#[deprecated = "Renamed to `back`, in line with GDScript method."]
203+
pub fn last(&self) -> Option<T> {
204+
self.back()
205+
}
206+
196207
/// Clears the array, removing all elements.
197208
pub fn clear(&mut self) {
198209
// SAFETY: No new values are written to the array, we only remove values from the array.
@@ -223,7 +234,7 @@ impl<T: ArrayElement> Array<T> {
223234
unsafe { self.as_inner_mut() }.push_back(value.to_variant());
224235
}
225236

226-
/// Adds an element at the beginning of the array, in O(n) time.
237+
/// Adds an element at the beginning of the array, in O(n).
227238
///
228239
/// On large arrays, this method is much slower than [`push()`][Self::push], as it will move all the array's elements.
229240
/// The larger the array, the slower `push_front()` will be.
@@ -243,7 +254,7 @@ impl<T: ArrayElement> Array<T> {
243254
})
244255
}
245256

246-
/// Removes and returns the first element of the array. Returns `None` if the array is empty.
257+
/// Removes and returns the first element of the array, in O(n). Returns `None` if the array is empty.
247258
///
248259
/// Note: On large arrays, this method is much slower than `pop()` as it will move all the
249260
/// array's elements. The larger the array, the slower `pop_front()` will be.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ fn array_try_get() {
187187
fn array_first_last() {
188188
let array = array![1, 2];
189189

190-
assert_eq!(array.first(), Some(1));
191-
assert_eq!(array.last(), Some(2));
190+
assert_eq!(array.front(), Some(1));
191+
assert_eq!(array.back(), Some(2));
192192

193193
let empty_array = VariantArray::new();
194194

195-
assert_eq!(empty_array.first(), None);
196-
assert_eq!(empty_array.last(), None);
195+
assert_eq!(empty_array.front(), None);
196+
assert_eq!(empty_array.back(), None);
197197
}
198198

199199
#[itest]

0 commit comments

Comments
 (0)