godot_core/builtin/collections/array.rs
1/*
2 * Copyright (c) godot-rust; Bromeon and contributors.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 */
7
8use std::cell::OnceCell;
9use std::marker::PhantomData;
10use std::{cmp, fmt};
11
12use godot_ffi as sys;
13use sys::{GodotFfi, ffi_methods, interface_fn};
14
15use crate::builtin::iter::ArrayFunctionalOps;
16use crate::builtin::*;
17use crate::meta;
18use crate::meta::error::{ArrayMismatch, ConvertError, FromGodotError, FromVariantError};
19use crate::meta::inspect::ElementType;
20use crate::meta::shape::{GodotElementShape, GodotShape};
21use crate::meta::signed_range::SignedRange;
22use crate::meta::{
23 AsArg, ClassId, Element, ExtVariantType, FromGodot, GodotConvert, GodotFfiVariant, GodotType,
24 RefArg, ToGodot, element_variant_type,
25};
26use crate::obj::{Bounds, DynGd, Gd, GodotClass, bounds};
27use crate::registry::info::{ParamMetadata, PropertyHintInfo};
28use crate::registry::property::{BuiltinExport, Export, Var};
29
30/// Godot's `Array` type.
31///
32/// Versatile, linear storage container for all types that can be represented inside a `Variant`. \
33/// For space-efficient storage, consider using [`PackedArray<T>`][crate::builtin::PackedArray] or `Vec<T>`.
34///
35/// Check out the [book](https://godot-rust.github.io/book/godot-api/builtins.html#arrays-and-dictionaries) for a tutorial on arrays.
36///
37/// # Reference semantics
38/// Like in GDScript, `Array` acts as a reference type: multiple `Array` instances may
39/// refer to the same underlying array, and changes to one are visible in the other.
40///
41/// To create a copy that shares data with the original array, use [`Clone::clone()`].
42/// If you want to create a copy of the data, use [`duplicate_shallow()`][Self::duplicate_shallow]
43/// or [`duplicate_deep()`][Self::duplicate_deep].
44///
45/// # Element type
46/// Godot's `Array` builtin can be either typed or untyped. In Rust, this maps to three representations:
47///
48/// - Untyped array: `VariantArray`, a type alias for `Array<Variant>`. \
49/// An untyped array can contain any kind of [`Variant`], even different types in the same array.
50///
51/// - Typed: `Array<T>`, with any non-`Variant` type `T` such as `i64`, `GString`, `Gd<T>` etc. \
52/// Typing is enforced at compile-time in Rust, and at runtime by Godot.
53///
54/// - Either typed or untyped: [`AnyArray`]. \
55/// Represents either typed or untyped arrays. This is different from `Array<Variant>`, because the latter allows you to insert
56/// `Variant` objects. However, for an array that has dynamic type `i64`, it is not allowed to insert any variants that are not `i64`.
57///
58/// If you plan to use any integer or float types apart from `i64` and `f64`, read
59/// [this documentation](../meta/trait.Element.html#integer-and-float-types).
60///
61/// ## Conversions between arrays
62/// The following table gives an overview of useful conversions.
63///
64/// | From | To | Conversion method |
65/// |--------------------|------------------|----------------------------------------------------------|
66/// | `AnyArray` | `Array<T>` | [`AnyArray::try_cast_array::<T>`] |
67/// | `AnyArray` | `Array<Variant>` | [`AnyArray::try_cast_var_array`] |
68/// | `&Array<T>` | `&AnyArray` | Implicit via [deref coercion]; often used in class APIs. |
69/// | `Array<T>` | `AnyArray` | [`Array<T>::upcast_any_array`] |
70/// | `Array<T>` | `PackedArray<T>` | [`Array<T>::to_packed_array`] |
71/// | `PackedArray<T>` | `Array<T>` | [`PackedArray<T>::to_typed_array`] |
72/// | `PackedArray<T>` | `Array<Variant>` | [`PackedArray<T>::to_var_array`] |
73///
74/// Note that it's **not** possible to upcast `Array<Gd<Derived>>` to `Array<Gd<Base>>`. `Array<T>` is not covariant over the `T` parameter,
75/// otherwise it would be possible to insert `Base` pointers that aren't actually `Derived`.
76// Note: the above could theoretically be implemented by making AnyArray<T> generic and covariant over T.
77///
78/// [deref coercion]: struct.Array.html#deref-methods-AnyArray
79///
80/// ## Typed array example
81/// ```no_run
82/// # use godot::prelude::*;
83/// // Create typed Array<i64> and add values.
84/// let mut array = Array::<i64>::new();
85/// array.push(10);
86/// array.push(20);
87/// array.push(30);
88///
89/// // Or create the same array in a single expression.
90/// let array = iarray![10, 20, 30];
91///
92/// // Access elements.
93/// let value: i64 = array.at(0); // 10
94/// let maybe: Option<i64> = array.get(3); // None
95///
96/// // Iterate over i64 elements.
97/// for value in array.iter_shared() {
98/// println!("{value}");
99/// }
100///
101/// // Clone array (shares the reference), and overwrite elements through clone.
102/// let mut cloned = array.clone();
103/// cloned.set(0, 50); // [50, 20, 30]
104/// cloned.remove(1); // [50, 30]
105/// cloned.pop(); // [50]
106///
107/// // Changes will be reflected in the original array.
108/// assert_eq!(array.len(), 1);
109/// assert_eq!(array.front(), Some(50));
110/// ```
111///
112/// ## Untyped array example
113/// ```no_run
114/// # use godot::prelude::*;
115/// // VarArray allows dynamic element types.
116/// let mut array = VarArray::new();
117/// array.push(&10.to_variant());
118/// array.push(&"Hello".to_variant());
119///
120/// // Or equivalent, use the `varray!` macro which converts each element.
121/// let array = varray![10, "Hello"];
122///
123/// // Access elements.
124/// let value: Variant = array.at(0);
125/// let value: i64 = array.at(0).to(); // Variant::to() extracts i64.
126/// let maybe: Result<i64, _> = array.at(1).try_to(); // "Hello" is not i64 -> Err.
127/// let maybe: Option<Variant> = array.get(3);
128///
129/// // ...and so on.
130/// ```
131///
132/// # Working with signed ranges and steps
133/// For negative indices, use [`wrapped()`](crate::meta::wrapped).
134///
135/// ```no_run
136/// # use godot::builtin::{array, iarray};
137/// # use godot::meta::wrapped;
138/// let arr = iarray![0, 1, 2, 3, 4, 5];
139///
140/// // The values of `begin` (inclusive) and `end` (exclusive) will be clamped to the array size.
141/// let clamped_array = arr.subarray_deep(999..99999, None);
142/// assert_eq!(clamped_array, array![]);
143///
144/// // If either `begin` or `end` is negative, its value is relative to the end of the array.
145/// let sub = arr.subarray_shallow(wrapped(-1..-5), None);
146/// assert_eq!(sub, array![5, 3]);
147///
148/// // If `end` is not specified, the range spans through whole array.
149/// let sub = arr.subarray_deep(1.., None);
150/// assert_eq!(sub, array![1, 2, 3, 4, 5]);
151/// let other_clamped_array = arr.subarray_shallow(5.., Some(2));
152/// assert_eq!(other_clamped_array, array![5]);
153///
154/// // If specified, `step` is the relative index between source elements. It can be negative,
155/// // in which case `begin` must be higher than `end`.
156/// let sub = arr.subarray_shallow(wrapped(-1..-5), Some(-2));
157/// assert_eq!(sub, array![5, 3]);
158/// ```
159///
160/// # Thread safety
161/// Usage is safe if the `Array` is used on a single thread only. Concurrent reads on
162/// different threads are also safe, but any writes must be externally synchronized. The Rust
163/// compiler will enforce this as long as you use only Rust threads, but it cannot protect against
164/// concurrent modification on other threads (e.g. created through GDScript).
165///
166/// # Element type safety
167/// We provide a richer set of element types than Godot, for convenience and stronger invariants in your _Rust_ code.
168/// This, however, means that the Godot representation of such arrays is not capable of incorporating the additional "Rust-side" information.
169/// This can lead to situations where GDScript code or the editor UI can insert values that do not fulfill the Rust-side invariants.
170/// The library offers some best-effort protection in Debug mode, but certain errors may only occur on element access, in the form of panics.
171///
172/// Concretely, the following types lose type information when passed to Godot. If you want 100% bullet-proof arrays, avoid those.
173/// - Non-`i64` integers: `i8`, `i16`, `i32`, `u8`, `u16`, `u32`. (`u64` is unsupported).
174/// - Non-`f64` floats: `f32`.
175/// - Non-null objects: [`Gd<T>`][crate::obj::Gd].
176/// Godot generally allows `null` in arrays due to default-constructability, e.g. when using `resize()`.
177/// The Godot-faithful (but less convenient) alternative is to use `Option<Gd<T>>` element types.
178/// - Objects with dyn-trait association: [`DynGd<T, D>`][crate::obj::DynGd].
179/// Godot doesn't know Rust traits and will only see the `T` part.
180///
181/// # Differences from GDScript
182/// Unlike GDScript, all indices and sizes are unsigned, so negative indices are not supported.
183///
184/// # Godot docs
185/// [`Array[T]` (stable)](https://docs.godotengine.org/en/stable/classes/class_array.html)
186pub struct Array<T: Element> {
187 // Safety Invariant: The type of all values in `opaque` matches the type `T`.
188 opaque: sys::types::OpaqueArray,
189 _phantom: PhantomData<T>,
190
191 /// Lazily computed and cached element type information.
192 pub(super) cached_element_type: OnceCell<ElementType>,
193}
194
195/// Guard that can only call immutable methods on the array.
196pub(super) struct ImmutableInnerArray<'a> {
197 inner: inner::InnerArray<'a>,
198}
199
200impl<'a> std::ops::Deref for ImmutableInnerArray<'a> {
201 type Target = inner::InnerArray<'a>;
202
203 fn deref(&self) -> &Self::Target {
204 &self.inner
205 }
206}
207
208impl<T: Element> std::ops::Deref for Array<T> {
209 type Target = AnyArray;
210
211 fn deref(&self) -> &Self::Target {
212 self.as_any_ref()
213 }
214}
215
216impl<T: Element> std::ops::DerefMut for Array<T> {
217 fn deref_mut(&mut self) -> &mut Self::Target {
218 self.as_any_mut()
219 }
220}
221
222// Compile-time validation of layout compatibility.
223sys::static_assert_eq_size_align!(Array<i64>, VarArray);
224sys::static_assert_eq_size_align!(Array<GString>, VarArray);
225sys::static_assert_eq_size_align!(VarArray, AnyArray);
226
227/// Untyped Godot `Array`.
228pub type VarArray = Array<Variant>;
229
230// TODO check if these return a typed array
231
232// Methods that don't provide type-specific ergonomics are available through `Deref`/`DerefMut` to [`AnyArray`].
233// This includes:
234// - Read-only: `len()`, `is_empty()`, `hash_u32()`, `element_type()`
235// - Mutable: `clear()`, `reverse()`, `shuffle()`, `shrink()`, `sort_unstable()`, `sort_unstable_custom()`
236//
237// Only methods that benefit from the type parameter `T` are implemented directly on `Array<T>`:
238// - Methods returning `T` instead of `Variant`: `at()`, `get()`, `front()`, `back()`, `min()`, `max()`, `pick_random()`, `pop()`, `pop_front()`, `remove()`
239// - Methods accepting `impl AsArg<T>`: `set()`, `push()`, `insert()`, `contains()`, `find()`, `erase()`, etc.
240// - Methods with typed closures: `sort_unstable_by()`, `bsearch_by()`
241// - Methods returning `Array<T>`: `duplicate_shallow()`, `duplicate_deep()`, `subarray_shallow()`, `subarray_deep()`
242impl<T: Element> Array<T> {
243 pub(super) fn from_opaque(opaque: sys::types::OpaqueArray) -> Self {
244 // Note: type is not yet checked at this point, because array has not yet been initialized!
245 Self {
246 opaque,
247 _phantom: PhantomData,
248 cached_element_type: OnceCell::new(),
249 }
250 }
251
252 /// Constructs an empty `Array`.
253 pub fn new() -> Self {
254 Self::default()
255 }
256
257 /// ⚠️ Returns the value at the specified index.
258 ///
259 /// This replaces the `Index` trait, which cannot be implemented for `Array`, as it stores variants and not references.
260 ///
261 /// # Panics
262 /// If `index` is out of bounds. To handle out-of-bounds access fallibly, use [`get()`](Self::get) instead.
263 pub fn at(&self, index: usize) -> T {
264 // Panics on out-of-bounds.
265 let ptr = self.ptr(index);
266
267 // SAFETY: `ptr` is a live pointer to a variant since `ptr.is_null()` just verified that the index is not out of bounds.
268 let variant = unsafe { Variant::borrow_var_sys(ptr) };
269 T::from_variant(variant)
270 }
271
272 /// Returns the value at the specified index, or `None` if the index is out-of-bounds.
273 ///
274 /// If you know the index is correct, use [`at()`](Self::at) instead.
275 pub fn get(&self, index: usize) -> Option<T> {
276 let ptr = self.ptr_or_null(index);
277 if ptr.is_null() {
278 None
279 } else {
280 // SAFETY: `ptr` is a live pointer to a variant since `ptr.is_null()` just verified that the index is not out of bounds.
281 let variant = unsafe { Variant::borrow_var_sys(ptr) };
282 Some(T::from_variant(variant))
283 }
284 }
285
286 /// Returns `true` if the array contains the given value. Equivalent of `has` in GDScript.
287 pub fn contains(&self, value: impl AsArg<T>) -> bool {
288 meta::arg_into_ref!(value: T);
289 self.as_inner().has(&value.to_variant())
290 }
291
292 /// Returns the number of times a value is in the array.
293 pub fn count(&self, value: impl AsArg<T>) -> usize {
294 meta::arg_into_ref!(value: T);
295 to_usize(self.as_inner().count(&value.to_variant()))
296 }
297
298 /// Returns the first element in the array, or `None` if the array is empty.
299 #[doc(alias = "first")]
300 pub fn front(&self) -> Option<T> {
301 (!self.is_empty()).then(|| {
302 let variant = self.as_inner().front();
303 T::from_variant(&variant)
304 })
305 }
306
307 /// Returns the last element in the array, or `None` if the array is empty.
308 #[doc(alias = "last")]
309 pub fn back(&self) -> Option<T> {
310 (!self.is_empty()).then(|| {
311 let variant = self.as_inner().back();
312 T::from_variant(&variant)
313 })
314 }
315
316 /// ⚠️ Sets the value at the specified index.
317 ///
318 /// # Panics
319 ///
320 /// If `index` is out of bounds.
321 pub fn set(&mut self, index: usize, value: impl AsArg<T>) {
322 self.balanced_ensure_mutable();
323
324 let ptr_mut = self.ptr_mut(index);
325
326 meta::arg_into_ref!(value: T);
327 let variant = value.to_variant();
328
329 // SAFETY: `ptr_mut` just checked that the index is not out of bounds.
330 unsafe { variant.move_into_var_ptr(ptr_mut) };
331 }
332
333 /// Appends an element to the end of the array.
334 ///
335 /// _Godot equivalents: `append` and `push_back`_
336 #[doc(alias = "append")]
337 #[doc(alias = "push_back")]
338 pub fn push(&mut self, value: impl AsArg<T>) {
339 self.balanced_ensure_mutable();
340
341 meta::arg_into_ref!(value: T);
342
343 // SAFETY: The array has type `T` and we're writing a value of type `T` to it.
344 let mut inner = unsafe { self.as_inner_mut() };
345 inner.push_back(&value.to_variant());
346 }
347
348 /// Internal helper for the `array!` macro; uses [`AsDirectElement`][meta::AsDirectElement] for unambiguous type inference.
349 #[doc(hidden)]
350 pub fn __macro_push_direct<E: meta::AsDirectElement<T>>(&mut self, value: E) {
351 self.push(value)
352 }
353
354 /// Adds an element at the beginning of the array, in O(n).
355 ///
356 /// On large arrays, this method is much slower than [`push()`][Self::push], as it will move all the array's elements.
357 /// The larger the array, the slower `push_front()` will be.
358 pub fn push_front(&mut self, value: impl AsArg<T>) {
359 self.balanced_ensure_mutable();
360
361 meta::arg_into_ref!(value: T);
362
363 // SAFETY: The array has type `T` and we're writing a value of type `T` to it.
364 let mut inner_array = unsafe { self.as_inner_mut() };
365 inner_array.push_front(&value.to_variant());
366 }
367
368 /// Removes and returns the last element of the array. Returns `None` if the array is empty.
369 ///
370 /// _Godot equivalent: `pop_back`_
371 #[doc(alias = "pop_back")]
372 pub fn pop(&mut self) -> Option<T> {
373 self.balanced_ensure_mutable();
374
375 (!self.is_empty()).then(|| {
376 // SAFETY: We do not write any values to the array, we just remove one.
377 let variant = unsafe { self.as_inner_mut() }.pop_back();
378 T::from_variant(&variant)
379 })
380 }
381
382 /// Removes and returns the first element of the array, in O(n). Returns `None` if the array is empty.
383 ///
384 /// Note: On large arrays, this method is much slower than `pop()` as it will move all the
385 /// array's elements. The larger the array, the slower `pop_front()` will be.
386 pub fn pop_front(&mut self) -> Option<T> {
387 self.balanced_ensure_mutable();
388
389 (!self.is_empty()).then(|| {
390 // SAFETY: We do not write any values to the array, we just remove one.
391 let variant = unsafe { self.as_inner_mut() }.pop_front();
392 T::from_variant(&variant)
393 })
394 }
395
396 /// ⚠️ Inserts a new element before the index. The index must be valid or the end of the array (`index == len()`).
397 ///
398 /// On large arrays, this method is much slower than [`push()`][Self::push], as it will move all the array's elements after the inserted element.
399 /// The larger the array, the slower `insert()` will be.
400 ///
401 /// # Panics
402 /// If `index > len()`.
403 pub fn insert(&mut self, index: usize, value: impl AsArg<T>) {
404 self.balanced_ensure_mutable();
405
406 let len = self.len();
407 assert!(
408 index <= len,
409 "Array insertion index {index} is out of bounds: length is {len}",
410 );
411
412 meta::arg_into_ref!(value: T);
413
414 // SAFETY: The array has type `T` and we're writing a value of type `T` to it.
415 unsafe { self.as_inner_mut() }.insert(to_i64(index), &value.to_variant());
416 }
417
418 /// ⚠️ Removes and returns the element at the specified index. Equivalent of `pop_at` in GDScript.
419 ///
420 /// On large arrays, this method is much slower than [`pop()`][Self::pop] as it will move all the array's
421 /// elements after the removed element. The larger the array, the slower `remove()` will be.
422 ///
423 /// # Panics
424 ///
425 /// If `index` is out of bounds.
426 #[doc(alias = "pop_at")]
427 pub fn remove(&mut self, index: usize) -> T {
428 self.balanced_ensure_mutable();
429 self.check_bounds(index);
430
431 // SAFETY: We do not write any values to the array, we just remove one.
432 let variant = unsafe { self.as_inner_mut() }.pop_at(to_i64(index));
433 T::from_variant(&variant)
434 }
435
436 /// Removes the first occurrence of a value from the array.
437 ///
438 /// If the value does not exist in the array, nothing happens. To remove an element by index, use [`remove()`][Self::remove] instead.
439 ///
440 /// On large arrays, this method is much slower than [`pop()`][Self::pop], as it will move all the array's
441 /// elements after the removed element.
442 pub fn erase(&mut self, value: impl AsArg<T>) {
443 self.balanced_ensure_mutable();
444
445 meta::arg_into_ref!(value: T);
446
447 // SAFETY: We don't write anything to the array.
448 unsafe { self.as_inner_mut() }.erase(&value.to_variant());
449 }
450
451 /// Assigns the given value to all elements in the array. This can be used together with
452 /// `resize` to create an array with a given size and initialized elements.
453 pub fn fill(&mut self, value: impl AsArg<T>) {
454 self.balanced_ensure_mutable();
455
456 meta::arg_into_ref!(value: T);
457
458 // SAFETY: The array has type `T` and we're writing values of type `T` to it.
459 unsafe { self.as_inner_mut() }.fill(&value.to_variant());
460 }
461
462 /// Resizes the array to contain a different number of elements.
463 ///
464 /// If the new size is smaller than the current size, then it removes elements from the end. If the new size is bigger than the current one
465 /// then the new elements are set to `value`.
466 ///
467 /// If you know that the new size is smaller, then consider using [`shrink`][AnyArray::shrink] instead.
468 pub fn resize(&mut self, new_size: usize, value: impl AsArg<T>) {
469 self.balanced_ensure_mutable();
470
471 let original_size = self.len();
472
473 // SAFETY: While we do insert `Variant::nil()` if the new size is larger, we then fill it with `value` ensuring that all values in the
474 // array are of type `T` still.
475 unsafe { self.as_inner_mut() }.resize(to_i64(new_size));
476
477 meta::arg_into_ref!(value: T);
478
479 // If new_size < original_size then this is an empty iterator and does nothing.
480 for i in original_size..new_size {
481 // Exception safety: if to_variant() panics, the array will become inconsistent (filled with non-T nils).
482 // At the moment (Nov 2024), this can only happen for u64, which isn't a valid Array element type.
483 // This could be changed to use clone() (if that doesn't panic) or store a variant without moving.
484 let variant = value.to_variant();
485
486 let ptr_mut = self.ptr_mut(i);
487
488 // SAFETY: we iterate pointer within bounds; ptr_mut() additionally checks them.
489 // ptr_mut() lookup could be optimized if we know the internal layout.
490 unsafe { variant.move_into_var_ptr(ptr_mut) };
491 }
492 }
493
494 /// Appends another array at the end of this array. Equivalent of `append_array` in GDScript.
495 pub fn extend_array(&mut self, other: &Array<T>) {
496 self.balanced_ensure_mutable();
497
498 // SAFETY: `append_array` will only write values gotten from `other` into `self`, and all values in `other` are guaranteed
499 // to be of type `T`.
500 let mut inner_self = unsafe { self.as_inner_mut() };
501 inner_self.append_array(other);
502 }
503
504 /// Returns a shallow copy, sharing reference types (`Array`, `Dictionary`, `Object`...) with the original array.
505 ///
506 /// This operation retains the dynamic [element type][Self::element_type]: copying `Array<T>` will yield another `Array<T>`.
507 ///
508 /// To create a deep copy, use [`duplicate_deep()`][Self::duplicate_deep] instead.
509 /// To create a new reference to the same array data, use [`clone()`][Clone::clone].
510 pub fn duplicate_shallow(&self) -> Self {
511 // duplicate() returns a typed array with the same type as Self, and all values are taken from `self` so have the right type.
512 self.as_inner().duplicate(false).cast_array::<T>()
513 }
514
515 /// Returns a deep copy, duplicating nested `Array`/`Dictionary` elements but keeping `Object` elements shared.
516 ///
517 /// To create a shallow copy, use [`duplicate_shallow()`][Self::duplicate_shallow] instead.
518 /// To create a new reference to the same array data, use [`clone()`][Clone::clone].
519 pub fn duplicate_deep(&self) -> Self {
520 // duplicate() returns a typed array with the same type as Self, and all values are taken from `self` so have the right type.
521 self.as_inner().duplicate(true).cast_array::<T>()
522 }
523
524 /// Returns a sub-range `begin..end` as a new `Array`.
525 ///
526 /// Array elements are copied to the slice, but any reference types (such as `Array`,
527 /// `Dictionary` and `Object`) will still refer to the same value. To create a deep copy, use
528 /// [`subarray_deep()`][Self::subarray_deep] instead.
529 ///
530 /// _Godot equivalent: `slice`_
531 #[doc(alias = "slice")]
532 pub fn subarray_shallow(&self, range: impl SignedRange, step: Option<i32>) -> Self {
533 self.subarray_impl(range, step, false)
534 }
535
536 /// Returns a sub-range `begin..end` as a new `Array`.
537 ///
538 /// All nested arrays and dictionaries are duplicated and will not be shared with the original
539 /// array. Note that any `Object`-derived elements will still be shallow copied. To create a
540 /// shallow copy, use [`subarray_shallow()`][Self::subarray_shallow] instead.
541 ///
542 /// _Godot equivalent: `slice`_
543 #[doc(alias = "slice")]
544 pub fn subarray_deep(&self, range: impl SignedRange, step: Option<i32>) -> Self {
545 self.subarray_impl(range, step, true)
546 }
547
548 // Note: Godot will clamp values by itself.
549 fn subarray_impl(&self, range: impl SignedRange, step: Option<i32>, deep: bool) -> Self {
550 assert_ne!(step, Some(0), "subarray: step cannot be zero");
551
552 let step = step.unwrap_or(1);
553 let (begin, end) = range.signed();
554 let end = end.unwrap_or(i32::MAX as i64);
555
556 self.as_inner()
557 .slice(begin, end, step as i64, deep)
558 .cast_array::<T>()
559 }
560
561 /// Returns an non-exclusive iterator over the elements of the `Array`.
562 ///
563 /// Takes the array by reference but returns its elements by value, since they are internally converted from `Variant`.
564 ///
565 /// Notice that it's possible to modify the `Array` through another reference while iterating over it. This will not result
566 /// in unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
567 pub fn iter_shared(&self) -> ArrayIter<'_, T> {
568 ArrayIter {
569 array: self,
570 next_idx: 0,
571 }
572 }
573
574 /// Returns the minimum value contained in the array if all elements are of comparable types.
575 ///
576 /// If the elements can't be compared or the array is empty, `None` is returned.
577 pub fn min(&self) -> Option<T> {
578 let min = self.as_inner().min();
579 (!min.is_nil()).then(|| T::from_variant(&min))
580 }
581
582 /// Returns the maximum value contained in the array if all elements are of comparable types.
583 ///
584 /// If the elements can't be compared or the array is empty, `None` is returned.
585 pub fn max(&self) -> Option<T> {
586 let max = self.as_inner().max();
587 (!max.is_nil()).then(|| T::from_variant(&max))
588 }
589
590 /// Returns a random element from the array, or `None` if it is empty.
591 pub fn pick_random(&self) -> Option<T> {
592 (!self.is_empty()).then(|| {
593 let variant = self.as_inner().pick_random();
594 T::from_variant(&variant)
595 })
596 }
597
598 /// Searches the array for the first occurrence of a value and returns its index, or `None` if
599 /// not found.
600 ///
601 /// Starts searching at index `from`; pass `None` to search the entire array.
602 pub fn find(&self, value: impl AsArg<T>, from: Option<usize>) -> Option<usize> {
603 meta::arg_into_ref!(value: T);
604
605 let from = to_i64(from.unwrap_or(0));
606 let index = self.as_inner().find(&value.to_variant(), from);
607 if index >= 0 {
608 Some(index.try_into().unwrap())
609 } else {
610 None
611 }
612 }
613
614 /// Searches the array backwards for the last occurrence of a value and returns its index, or
615 /// `None` if not found.
616 ///
617 /// Starts searching at index `from`; pass `None` to search the entire array.
618 pub fn rfind(&self, value: impl AsArg<T>, from: Option<usize>) -> Option<usize> {
619 meta::arg_into_ref!(value: T);
620
621 let from = from.map(to_i64).unwrap_or(-1);
622 let index = self.as_inner().rfind(&value.to_variant(), from);
623
624 // It's not documented, but `rfind` returns -1 if not found.
625 if index >= 0 {
626 Some(to_usize(index))
627 } else {
628 None
629 }
630 }
631
632 /// Finds the index of a value in a sorted array using binary search.
633 ///
634 /// If the value is not present in the array, returns the insertion index that would maintain sorting order.
635 ///
636 /// Calling `bsearch` on an unsorted array results in unspecified behavior. Consider using `sort()` to ensure the sorting
637 /// order is compatible with your callable's ordering.
638 ///
639 /// See also: [`bsearch_by()`][Self::bsearch_by], [`functional_ops().bsearch_custom()`][ArrayFunctionalOps::bsearch_custom].
640 pub fn bsearch(&self, value: impl AsArg<T>) -> usize {
641 meta::arg_into_ref!(value: T);
642
643 to_usize(self.as_inner().bsearch(&value.to_variant(), true))
644 }
645
646 /// Finds the index of a value in a sorted array using binary search, with type-safe custom predicate.
647 ///
648 /// The comparator function should return an ordering that indicates whether its argument is `Less`, `Equal` or `Greater` the desired value.
649 /// For example, for an ascending-ordered array, a simple predicate searching for a constant value would be `|elem| elem.cmp(&4)`.
650 /// This follows the design of [`slice::binary_search_by()`].
651 ///
652 /// If the value is found, returns `Ok(index)` with its index. Otherwise, returns `Err(index)`, where `index` is the insertion index
653 /// that would maintain sorting order.
654 ///
655 /// Calling `bsearch_by` on an unsorted array results in unspecified behavior. Consider using [`sort_unstable_by()`][Self::sort_unstable_by]
656 /// to ensure the sorting order is compatible with your callable's ordering.
657 ///
658 /// See also: [`bsearch()`][Self::bsearch], [`functional_ops().bsearch_custom()`][ArrayFunctionalOps::bsearch_custom].
659 pub fn bsearch_by<F>(&self, mut func: F) -> Result<usize, usize>
660 where
661 F: FnMut(&T) -> cmp::Ordering + 'static,
662 {
663 // Early exit; later code relies on index 0 being present.
664 if self.is_empty() {
665 return Err(0);
666 }
667
668 // We need one dummy element of type T, because Godot's bsearch_custom() checks types (so Variant::nil() can't be passed).
669 // Optimization: roundtrip Variant -> T -> Variant could be avoided, but anyone needing speed would use Rust binary search...
670 let ignored_value = self.at(0);
671 let ignored_value = meta::owned_into_arg(ignored_value);
672
673 let godot_comparator = |args: &[&Variant]| {
674 let value = T::from_variant(args[0]);
675 let is_less = matches!(func(&value), cmp::Ordering::Less);
676
677 is_less.to_variant()
678 };
679
680 let debug_name = std::any::type_name::<F>();
681 let index = Callable::with_scoped_fn(debug_name, godot_comparator, |pred| {
682 self.functional_ops().bsearch_custom(ignored_value, pred)
683 });
684
685 if let Some(value_at_index) = self.get(index)
686 && func(&value_at_index) == cmp::Ordering::Equal
687 {
688 return Ok(index);
689 }
690
691 Err(index)
692 }
693
694 /// Sorts the array, using a type-safe comparator.
695 ///
696 /// The predicate expects two parameters `(a, b)` and should return an ordering relation. For example, simple ascending ordering of the
697 /// elements themselves would be achieved with `|a, b| a.cmp(b)`.
698 ///
699 /// The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
700 /// This means that values considered equal may have their order changed when using `sort_unstable_by()`. For most variant types,
701 /// this distinction should not matter though.
702 ///
703 /// See also: [`sort_unstable()`][Self::sort_unstable], [`sort_unstable_custom()`][Self::sort_unstable_custom].
704 pub fn sort_unstable_by<F>(&mut self, mut func: F)
705 where
706 F: FnMut(&T, &T) -> cmp::Ordering,
707 {
708 self.balanced_ensure_mutable();
709
710 let godot_comparator = |args: &[&Variant]| {
711 let lhs = T::from_variant(args[0]);
712 let rhs = T::from_variant(args[1]);
713 let is_less = matches!(func(&lhs, &rhs), cmp::Ordering::Less);
714
715 is_less.to_variant()
716 };
717
718 let debug_name = std::any::type_name::<F>();
719 Callable::with_scoped_fn(debug_name, godot_comparator, |pred| {
720 self.sort_unstable_custom(pred)
721 });
722 }
723
724 /// Access to Godot's functional-programming APIs based on callables.
725 ///
726 /// Exposes Godot array methods such as `filter()`, `map()`, `reduce()` and many more. See return type docs.
727 pub fn functional_ops(&self) -> ArrayFunctionalOps<'_, T> {
728 ArrayFunctionalOps::new(self)
729 }
730
731 /// Turns the array into a shallow-immutable array.
732 ///
733 /// Makes the array read-only and returns the original array. The array's elements cannot be overridden with different values, and their
734 /// order cannot change. Does not apply to nested elements, such as dictionaries. This operation is irreversible.
735 ///
736 /// In GDScript, arrays are automatically read-only if declared with the `const` keyword.
737 ///
738 /// # Semantics and alternatives
739 /// You can use this in Rust, but the behavior of mutating methods is only validated in a best-effort manner (more than in GDScript though):
740 /// some methods like `set()` panic in Debug mode, when used on a read-only array. There is no guarantee that any attempts to change result
741 /// in feedback; some may silently do nothing.
742 ///
743 /// In Rust, you can use shared references (`&Array<T>`) to prevent mutation. Note however that `Clone` can be used to create another
744 /// reference, through which mutation can still occur. For deep-immutable arrays, you'll need to keep your `Array` encapsulated or directly
745 /// use Rust data structures.
746 ///
747 /// _Godot equivalent: `make_read_only`_
748 #[doc(alias = "make_read_only")]
749 pub fn into_read_only(self) -> Self {
750 // SAFETY: Changes a per-array property, no elements.
751 unsafe { self.as_inner_mut() }.make_read_only();
752 self
753 }
754
755 /// Downgrades this typed/untyped `Array<T>` into an owned `AnyArray`.
756 ///
757 /// Typically, you can use deref coercion to convert `&Array<T>` to `&AnyArray`. This method is useful if you need `AnyArray` by value.
758 /// It consumes `self` to avoid incrementing the reference count; use `clone()` if you use the original array further.
759 pub fn upcast_any_array(self) -> AnyArray {
760 AnyArray::from_typed_or_untyped(self)
761 }
762
763 /// Converts this typed `Array<T>` into a `PackedArray<T>`.
764 pub fn to_packed_array(&self) -> PackedArray<T>
765 where
766 T: meta::PackedElement,
767 {
768 PackedArray::<T>::from_typed_array(self)
769 }
770
771 /// Returns true if the array is read-only.
772 ///
773 /// See [`into_read_only()`][Self::into_read_only].
774 /// In GDScript, arrays are automatically read-only if declared with the `const` keyword.
775 pub fn is_read_only(&self) -> bool {
776 self.as_inner().is_read_only()
777 }
778
779 /// Best-effort mutability check.
780 ///
781 /// # Panics (safeguards-balanced)
782 /// If the array is marked as read-only.
783 fn balanced_ensure_mutable(&self) {
784 sys::balanced_assert!(
785 !self.is_read_only(),
786 "mutating operation on read-only array"
787 );
788 }
789
790 /// Asserts that the given index refers to an existing element.
791 ///
792 /// # Panics
793 /// If `index` is out of bounds.
794 fn check_bounds(&self, index: usize) {
795 // Safety-relevant; explicitly *don't* use safeguards-dependent validation.
796 let len = self.len();
797 assert!(
798 index < len,
799 "Array index {index} is out of bounds: length is {len}",
800 );
801 }
802
803 /// Returns a pointer to the element at the given index.
804 ///
805 /// # Panics
806 /// If `index` is out of bounds.
807 fn ptr(&self, index: usize) -> sys::GDExtensionConstVariantPtr {
808 let ptr = self.ptr_or_null(index);
809 assert!(
810 !ptr.is_null(),
811 "Array index {index} out of bounds (len {len})",
812 len = self.len(),
813 );
814 ptr
815 }
816
817 /// Returns a pointer to the element at the given index, or null if out of bounds.
818 fn ptr_or_null(&self, index: usize) -> sys::GDExtensionConstVariantPtr {
819 let index = to_i64(index);
820
821 // SAFETY: array_operator_index_const returns null for invalid indexes.
822 let variant_ptr = unsafe { interface_fn!(array_operator_index_const)(self.sys(), index) };
823
824 // Signature is wrong in GDExtension, semantically this is a const ptr
825 sys::SysPtr::as_const(variant_ptr)
826 }
827
828 /// Returns a mutable pointer to the element at the given index.
829 ///
830 /// # Panics
831 ///
832 /// If `index` is out of bounds.
833 fn ptr_mut(&mut self, index: usize) -> sys::GDExtensionVariantPtr {
834 let ptr = self.ptr_mut_or_null(index);
835 assert!(
836 !ptr.is_null(),
837 "Array index {index} out of bounds (len {len})",
838 len = self.len(),
839 );
840 ptr
841 }
842
843 /// Returns a pointer to the element at the given index, or null if out of bounds.
844 fn ptr_mut_or_null(&mut self, index: usize) -> sys::GDExtensionVariantPtr {
845 let index = to_i64(index);
846
847 // SAFETY: array_operator_index returns null for invalid indexes.
848 unsafe { interface_fn!(array_operator_index)(self.sys_mut(), index) }
849 }
850
851 /// # Safety
852 /// This has the same safety issues as doing `self.assume_type::<Variant>()` and so the relevant safety invariants from
853 /// [`assume_type`](Self::assume_type) must be upheld.
854 ///
855 /// In particular this means that all reads are fine, since all values can be converted to `Variant`. However, writes are only OK
856 /// if they match the type `T`.
857 pub(crate) unsafe fn as_inner_mut(&self) -> inner::InnerArray<'_> {
858 // The memory layout of `Array<T>` does not depend on `T`.
859 inner::InnerArray::from_outer_typed(self)
860 }
861
862 pub(super) fn as_inner(&self) -> ImmutableInnerArray<'_> {
863 ImmutableInnerArray {
864 // SAFETY: We can only read from the array.
865 inner: unsafe { self.as_inner_mut() },
866 }
867 }
868
869 /// Changes the generic type on this array, without changing its contents. Needed for API functions
870 /// that take a variant array even though we want to pass a typed one.
871 ///
872 /// # Safety
873 /// - Any values written to the array must match the runtime type of the array.
874 /// - Any values read from the array must be convertible to the type `U`.
875 ///
876 /// If the safety invariant of `Array` is intact, which it must be for any publicly accessible arrays, then `U` must match
877 /// the runtime type of the array. This then implies that both of the conditions above hold. This means that you only need
878 /// to keep the above conditions in mind if you are intentionally violating the safety invariant of `Array`.
879 ///
880 /// Note also that any `GodotType` can be written to a `Variant` array.
881 ///
882 /// In the current implementation, both cases will produce a panic rather than undefined behavior, but this should not be relied upon.
883 #[cfg(safeguards_strict)] #[cfg_attr(published_docs, doc(cfg(safeguards_strict)))]
884 unsafe fn assume_type_ref<U: Element>(&self) -> &Array<U> {
885 // SAFETY: per function precondition.
886 unsafe { std::mem::transmute::<&Array<T>, &Array<U>>(self) }
887 }
888
889 fn as_any_ref(&self) -> &AnyArray {
890 // SAFETY:
891 // - Array<T> and Array<Variant> have identical memory layout.
892 // - AnyArray is #[repr(transparent)] around VariantArray and provides no "in" operations (moving data in) that could violate covariance.
893 unsafe { std::mem::transmute::<&Array<T>, &AnyArray>(self) }
894 }
895
896 fn as_any_mut(&mut self) -> &mut AnyArray {
897 // SAFETY:
898 // - Array<T> and Array<Variant> have identical memory layout (validated by static_assert_eq_size_align!).
899 // - AnyArray is #[repr(transparent)] around VariantArray.
900 // - Mutable operations on AnyArray work with Variant values, maintaining type safety through balanced_ensure_mutable() checks.
901 unsafe { std::mem::transmute::<&mut Array<T>, &mut AnyArray>(self) }
902 }
903
904 /// Changes the type parameter without runtime checks, consuming the array.
905 ///
906 /// # Safety
907 /// Same safety requirements as [`assume_type_ref`](Self::assume_type_ref):
908 /// - Values written to array must match runtime type.
909 /// - Values read must be convertible to type `U`.
910 /// - If runtime type matches `U`, both conditions hold automatically.
911 // TODO(v0.6): fragile manual field move + mem::forget; if a field is added, it must be moved here too.
912 // Consider transmute (requires #[repr(C)]) or ManuallyDrop + ptr::read. Same issue in Dictionary::assume_type.
913 pub(super) unsafe fn assume_type<U: Element>(self) -> Array<U> {
914 let result = Array::<U> {
915 opaque: self.opaque,
916 _phantom: PhantomData,
917 cached_element_type: OnceCell::new(),
918 };
919
920 // Transfer cached type to avoid redundant FFI calls.
921 ElementType::transfer_cache(&self.cached_element_type, &result.cached_element_type);
922
923 // Prevent drop of self since we moved opaque.
924 std::mem::forget(self);
925
926 result
927 }
928
929 /// Validates that all elements in this array can be converted to integers of type `T`.
930 #[cfg(safeguards_strict)] #[cfg_attr(published_docs, doc(cfg(safeguards_strict)))]
931 pub(crate) fn debug_validate_int_elements(&self) -> Result<(), ConvertError> {
932 // SAFETY: every element is internally represented as Variant.
933 let canonical_array = unsafe { self.assume_type_ref::<Variant>() };
934
935 // If any element is not convertible, this will return an error.
936 for elem in canonical_array.iter_shared() {
937 elem.try_to::<T>().map_err(|_err| {
938 FromGodotError::BadArrayTypeInt {
939 expected_int_type: std::any::type_name::<T>(),
940 value: elem
941 .try_to::<i64>()
942 .expect("origin must be i64 compatible; this is a bug"),
943 }
944 .into_error(self.clone()) // Context info about array, not element.
945 })?;
946 }
947
948 Ok(())
949 }
950
951 // No-op in Release. Avoids O(n) conversion checks, but still panics on access.
952 #[cfg(not(safeguards_strict))] #[cfg_attr(published_docs, doc(cfg(not(safeguards_strict))))]
953 pub(crate) fn debug_validate_int_elements(&self) -> Result<(), ConvertError> {
954 Ok(())
955 }
956
957 /// Checks that the inner array has the correct type set on it for storing elements of type `T`.
958 fn with_checked_type(self) -> Result<Self, ConvertError> {
959 let actual = self.element_type();
960 let expected = ElementType::of::<T>();
961
962 if actual.is_compatible_with(&expected) {
963 Ok(self)
964 } else {
965 let mismatch = ArrayMismatch { expected, actual };
966 Err(FromGodotError::BadArrayType(mismatch).into_error(self))
967 }
968 }
969
970 /// Sets the type of the inner array.
971 ///
972 /// # Safety
973 /// Must only be called once, directly after creation.
974 unsafe fn init_inner_type(&mut self) {
975 sys::strict_assert!(self.is_empty());
976 sys::strict_assert!(
977 self.cached_element_type.get().is_none(),
978 "init_inner_type() called twice"
979 );
980
981 // Immediately set cache to static type.
982 let elem_ty = ElementType::of::<T>();
983 let _ = self.cached_element_type.set(elem_ty);
984
985 if elem_ty.is_typed() {
986 let script = Variant::nil();
987
988 // A bit contrived because empty StringName is lazy-initialized but must also remain valid.
989 #[allow(unused_assignments)]
990 let mut empty_string_name = None;
991 let class_name = if let Some(class_id) = elem_ty.class_id() {
992 class_id.string_sys()
993 } else {
994 empty_string_name = Some(StringName::default());
995 // as_ref() crucial here -- otherwise the StringName is dropped.
996 empty_string_name.as_ref().unwrap().string_sys()
997 };
998
999 // SAFETY: Valid pointers are passed in.
1000 // Relevant for correctness, not safety: the array is a newly created, empty, untyped array.
1001 unsafe {
1002 interface_fn!(array_set_typed)(
1003 self.sys_mut(),
1004 elem_ty.variant_type().sys(),
1005 class_name, // must be empty if variant_type != OBJECT.
1006 script.var_sys(),
1007 );
1008 }
1009 }
1010 }
1011
1012 /// Creates a new array for [`GodotFfi::new_with_init()`], without setting a type yet.
1013 pub(super) fn new_uncached_type(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
1014 let mut result = unsafe {
1015 Self::new_with_uninit(|self_ptr| {
1016 let ctor = sys::builtin_fn!(array_construct_default);
1017 ctor(self_ptr, std::ptr::null_mut());
1018 })
1019 };
1020 init_fn(result.sys_mut());
1021 result
1022 }
1023
1024 /// # Safety
1025 /// Does not validate the array element type; `with_checked_type()` should be called afterward.
1026 // Visibility: shared with AnyArray.
1027 pub(super) unsafe fn unchecked_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
1028 let dest_type = Self::VARIANT_TYPE.variant_as_nil();
1029
1030 if variant.get_type() != dest_type {
1031 return Err(FromVariantError::BadType {
1032 expected: dest_type,
1033 actual: variant.get_type(),
1034 }
1035 .into_error(variant.clone()));
1036 }
1037
1038 let array = unsafe {
1039 Self::new_with_uninit(|self_ptr| {
1040 let array_from_variant = sys::builtin_fn!(array_from_variant);
1041 array_from_variant(self_ptr, sys::SysPtr::force_mut(variant.var_sys()));
1042 })
1043 };
1044
1045 Ok(array)
1046 }
1047
1048 /// Returns a clone of the array without checking the resulting type.
1049 ///
1050 /// # Safety
1051 /// Should be used only in scenarios where the caller can guarantee that the resulting array will have the correct type,
1052 /// or when an incorrect Rust type is acceptable (passing raw arrays to Godot FFI).
1053 pub(super) unsafe fn clone_unchecked(&self) -> Self {
1054 let result = unsafe {
1055 Self::new_with_uninit(|self_ptr| {
1056 let ctor = sys::builtin_fn!(array_construct_copy);
1057 let args = [self.sys()];
1058 ctor(self_ptr, args.as_ptr());
1059 })
1060 };
1061
1062 result.with_cache(self)
1063 }
1064
1065 /// Whether this array is untyped and holds `Variant` elements (compile-time check).
1066 ///
1067 /// Used as `if` statement in trait impls. Avoids defining yet another trait or non-local overridden function just for this case;
1068 /// `Variant` is the only Godot type that has variant type NIL and can be used as an array element.
1069 fn has_variant_t() -> bool {
1070 element_variant_type::<T>() == VariantType::NIL
1071 }
1072
1073 /// Execute a function that creates a new Array, transferring cached element type if available.
1074 ///
1075 /// This is a convenience helper for methods that create new Array instances and want to preserve
1076 /// cached type information to avoid redundant FFI calls.
1077 fn with_cache(self, source: &Self) -> Self {
1078 ElementType::transfer_cache(&source.cached_element_type, &self.cached_element_type);
1079 self
1080 }
1081}
1082
1083impl VarArray {
1084 /// # Safety
1085 /// - Variant must have type `VariantType::ARRAY`.
1086 /// - Subsequent operations on this array must not rely on the type of the array.
1087 pub(crate) unsafe fn from_variant_unchecked(variant: &Variant) -> Self {
1088 unsafe {
1089 // See also ffi_from_variant().
1090 Self::new_with_uninit(|self_ptr| {
1091 let array_from_variant = sys::builtin_fn!(array_from_variant);
1092 array_from_variant(self_ptr, sys::SysPtr::force_mut(variant.var_sys()));
1093 })
1094 }
1095 }
1096}
1097
1098// ----------------------------------------------------------------------------------------------------------------------------------------------
1099// Traits
1100
1101// Godot has some inconsistent behavior around NaN values. In GDScript, `NAN == NAN` is `false`,
1102// but `[NAN] == [NAN]` is `true`. If they decide to make all NaNs equal, we can implement `Eq` and
1103// `Ord`; if they decide to make all NaNs unequal, we can remove this comment.
1104//
1105// impl<T> Eq for Array<T> {}
1106//
1107// impl<T> Ord for Array<T> {
1108// ...
1109// }
1110
1111// SAFETY:
1112// - `move_return_ptr`
1113// Nothing special needs to be done beyond a `std::mem::swap` when returning an Array.
1114// So we can just use `ffi_methods`.
1115//
1116// - `from_arg_ptr`
1117// Arrays are properly initialized through a `from_sys` call, but the ref-count should be incremented
1118// as that is the callee's responsibility. Which we do by calling `std::mem::forget(array.clone())`.
1119unsafe impl<T: Element> GodotFfi for Array<T> {
1120 const VARIANT_TYPE: ExtVariantType = ExtVariantType::Concrete(VariantType::ARRAY);
1121
1122 ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
1123 fn new_from_sys;
1124 fn new_with_uninit;
1125 fn sys;
1126 fn sys_mut;
1127 fn from_arg_ptr;
1128 fn move_return_ptr;
1129 }
1130
1131 /// Constructs a valid Godot array as ptrcall destination, without caching the element type.
1132 ///
1133 /// Ptrcall may replace the underlying data with an array of a different type (e.g. `duplicate()` on a typed
1134 /// array returns `VarArray` in codegen, but the actual data is typed). The cache is lazily populated on first
1135 /// access from the actual Godot data.
1136 unsafe fn new_with_init(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
1137 Self::new_uncached_type(init_fn)
1138 }
1139}
1140
1141// Only implement for untyped arrays; typed arrays cannot be nested in Godot.
1142impl Element for VarArray {}
1143
1144impl<T: Element> GodotConvert for Array<T> {
1145 type Via = Self;
1146
1147 fn godot_shape() -> GodotShape {
1148 if Self::has_variant_t() {
1149 return GodotShape::Builtin {
1150 variant_type: VariantType::ARRAY,
1151 metadata: ParamMetadata::NONE,
1152 };
1153 }
1154
1155 GodotShape::TypedArray {
1156 element: GodotElementShape::new(T::godot_shape()),
1157 }
1158 }
1159}
1160
1161impl<T: Element> ToGodot for Array<T> {
1162 type Pass = meta::ByRef;
1163
1164 fn to_godot(&self) -> &Self::Via {
1165 self
1166 }
1167
1168 fn to_godot_owned(&self) -> Self::Via {
1169 // Overridden, because default clone() validates that before/after element types are equal, which doesn't matter when we pass to FFI.
1170 // This may however be an issue if to_godot_owned() is used by the user directly.
1171 unsafe { self.clone_unchecked() }
1172 }
1173}
1174
1175impl<T: Element> FromGodot for Array<T> {
1176 fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
1177 T::debug_validate_elements(&via)?;
1178 Ok(via)
1179 }
1180}
1181
1182impl<T: Element> fmt::Debug for Array<T> {
1183 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1184 // Going through `Variant` because there doesn't seem to be a direct way.
1185 // Reuse Display.
1186 write!(f, "{}", self.to_variant().stringify())
1187 }
1188}
1189
1190impl<T: Element + fmt::Display> fmt::Display for Array<T> {
1191 /// Formats `Array` to match Godot's string representation.
1192 ///
1193 /// # Example
1194 /// ```no_run
1195 /// # use godot::prelude::*;
1196 /// let a = iarray![1,2,3,4];
1197 /// assert_eq!(format!("{a}"), "[1, 2, 3, 4]");
1198 /// ```
1199 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1200 write!(f, "[")?;
1201 for (count, v) in self.iter_shared().enumerate() {
1202 if count != 0 {
1203 write!(f, ", ")?;
1204 }
1205 write!(f, "{v}")?;
1206 }
1207 write!(f, "]")
1208 }
1209}
1210
1211/// Creates a new reference to the data in this array. Changes to the original array will be
1212/// reflected in the copy and vice versa.
1213///
1214/// To create a (mostly) independent copy instead, see [`Array::duplicate_shallow()`] and
1215/// [`Array::duplicate_deep()`].
1216impl<T: Element> Clone for Array<T> {
1217 fn clone(&self) -> Self {
1218 // SAFETY: `self` is a valid array, since we have a reference that keeps it alive.
1219 // Type-check follows below.
1220 let copy = unsafe { self.clone_unchecked() };
1221
1222 // Double-check copy's runtime type in Debug mode.
1223 if cfg!(safeguards_strict) {
1224 copy.with_checked_type()
1225 .expect("copied array should have same type as original array")
1226 } else {
1227 copy
1228 }
1229 }
1230}
1231
1232// No Var bound on T.
1233impl<T: Element> Var for Array<T> {
1234 type PubType = Self;
1235
1236 fn var_get(field: &Self) -> Self::Via {
1237 field.to_godot_owned()
1238 }
1239
1240 fn var_set(field: &mut Self, value: Self::Via) {
1241 *field = FromGodot::from_godot(value);
1242 }
1243
1244 fn var_pub_get(field: &Self) -> Self::PubType {
1245 field.clone()
1246 }
1247
1248 fn var_pub_set(field: &mut Self, value: Self::PubType) {
1249 *field = value;
1250 }
1251}
1252
1253impl<T> Export for Array<T> where T: Element + Export {}
1254
1255impl<T: Element> BuiltinExport for Array<T> {}
1256
1257impl<T> Export for Array<Gd<T>>
1258where
1259 T: GodotClass + Bounds<Exportable = bounds::Yes>,
1260{
1261 #[doc(hidden)]
1262 fn as_node_class() -> Option<ClassId> {
1263 PropertyHintInfo::object_as_node_class::<T>()
1264 }
1265}
1266
1267/// `#[export]` for `Array<DynGd<T, D>>` is available only for `T` being Engine class (such as Node or Resource).
1268///
1269/// Consider exporting `Array<Gd<T>>` instead of `Array<DynGd<T, D>>` for user-declared GDExtension classes.
1270impl<T: GodotClass, D> Export for Array<DynGd<T, D>>
1271where
1272 T: GodotClass + Bounds<Exportable = bounds::Yes>,
1273 D: ?Sized + 'static,
1274{
1275 #[doc(hidden)]
1276 fn as_node_class() -> Option<ClassId> {
1277 PropertyHintInfo::object_as_node_class::<T>()
1278 }
1279}
1280
1281impl<T: Element> Default for Array<T> {
1282 #[inline]
1283 fn default() -> Self {
1284 let mut array = unsafe {
1285 Self::new_with_uninit(|self_ptr| {
1286 let ctor = sys::builtin_fn!(array_construct_default);
1287 ctor(self_ptr, std::ptr::null_mut())
1288 })
1289 };
1290
1291 // SAFETY: We just created this array, and haven't called `init_inner_type` before.
1292 unsafe { array.init_inner_type() };
1293 array
1294 }
1295}
1296
1297// T must be GodotType (or subtrait Element), because drop() requires sys_mut(), which is on the GodotFfi trait.
1298// Its sister method GodotFfi::from_sys_init() requires Default, which is only implemented for T: GodotType.
1299// This could be addressed by splitting up GodotFfi if desired.
1300impl<T: Element> Drop for Array<T> {
1301 #[inline]
1302 fn drop(&mut self) {
1303 unsafe {
1304 let array_destroy = sys::builtin_fn!(array_destroy);
1305 array_destroy(self.sys_mut());
1306 }
1307 }
1308}
1309
1310impl<T: Element> GodotType for Array<T> {
1311 type Ffi = Self;
1312
1313 type ToFfi<'f>
1314 = RefArg<'f, Array<T>>
1315 where
1316 Self: 'f;
1317
1318 fn to_ffi(&self) -> Self::ToFfi<'_> {
1319 RefArg::new(self)
1320 }
1321
1322 fn into_ffi(self) -> Self::Ffi {
1323 self
1324 }
1325
1326 fn try_from_ffi(ffi: Self::Ffi) -> Result<Self, ConvertError> {
1327 Ok(ffi)
1328 }
1329}
1330
1331impl<T: Element> GodotFfiVariant for Array<T> {
1332 fn ffi_to_variant(&self) -> Variant {
1333 unsafe {
1334 Variant::new_with_var_uninit(|variant_ptr| {
1335 let array_to_variant = sys::builtin_fn!(array_to_variant);
1336 array_to_variant(variant_ptr, sys::SysPtr::force_mut(self.sys()));
1337 })
1338 }
1339 }
1340
1341 fn ffi_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
1342 // SAFETY: if conversion succeeds, we call with_checked_type() afterwards.
1343 let array = unsafe { Self::unchecked_from_variant(variant) }?;
1344
1345 // Then, check the runtime type of the array.
1346 array.with_checked_type()
1347 }
1348}
1349
1350// ----------------------------------------------------------------------------------------------------------------------------------------------
1351// Conversion traits
1352
1353// From impls converting values (like [T; N] or Vec<T>) are explicitly not supported, because there's no perf benefit in submitting ownership.
1354// This is different for PackedArray, which can move values. See also related: https://github.com/godot-rust/gdext/pull/1286.
1355
1356/// Creates a `Array` from the given Rust array.
1357impl<T: Element + ToGodot, const N: usize> From<&[T; N]> for Array<T> {
1358 fn from(arr: &[T; N]) -> Self {
1359 Self::from(&arr[..])
1360 }
1361}
1362
1363/// Creates a `Array` from the given slice.
1364impl<T: Element + ToGodot> From<&[T]> for Array<T> {
1365 fn from(slice: &[T]) -> Self {
1366 let mut array = Self::new();
1367 let len = slice.len();
1368 if len == 0 {
1369 return array;
1370 }
1371
1372 // SAFETY: We fill the array with `Variant::nil()`, however since we're resizing to the size of the slice we'll end up rewriting all
1373 // the nulls with values of type `T`.
1374 unsafe { array.as_inner_mut() }.resize(to_i64(len));
1375
1376 // SAFETY: `array` has `len` elements since we just resized it, and they are all valid `Variant`s. Additionally, since
1377 // the array was created in this function, and we do not access the array while this slice exists, the slice has unique
1378 // access to the elements.
1379 let elements = unsafe { Variant::borrow_slice_mut(array.ptr_mut(0), len) };
1380 for (element, array_slot) in slice.iter().zip(elements.iter_mut()) {
1381 *array_slot = element.to_variant();
1382 }
1383
1384 array
1385 }
1386}
1387
1388/// Creates a `Array` from an iterator.
1389impl<T: Element + ToGodot> FromIterator<T> for Array<T> {
1390 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1391 let mut array = Self::new();
1392 array.extend(iter);
1393 array
1394 }
1395}
1396
1397/// Extends a `Array` with the contents of an iterator.
1398impl<T: Element> Extend<T> for Array<T> {
1399 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
1400 // Unfortunately the GDExtension API does not offer the equivalent of `Vec::reserve`.
1401 // Otherwise, we could use it to pre-allocate based on `iter.size_hint()`.
1402 //
1403 // A faster implementation using `resize()` and direct pointer writes might still be possible.
1404 // Note that this could technically also use iter(), since no moves need to happen (however Extend requires IntoIterator).
1405 for item in iter.into_iter() {
1406 // self.push(AsArg::into_arg(&item));
1407 self.push(meta::owned_into_arg(item));
1408 }
1409 }
1410}
1411
1412/// Converts this array to a strongly typed Rust vector.
1413impl<T: Element + FromGodot> From<&Array<T>> for Vec<T> {
1414 fn from(array: &Array<T>) -> Vec<T> {
1415 let len = array.len();
1416 let mut vec = Vec::with_capacity(len);
1417
1418 // SAFETY: Unless `experimental-threads` is enabled, then we cannot have concurrent access to this array.
1419 // And since we don't concurrently access the array in this function, we can create a slice to its contents.
1420 let elements = unsafe { Variant::borrow_slice(array.ptr(0), len) };
1421
1422 vec.extend(elements.iter().map(T::from_variant));
1423
1424 vec
1425 }
1426}
1427
1428// ----------------------------------------------------------------------------------------------------------------------------------------------
1429// Iterators
1430
1431/// An iterator over typed elements of an [`Array`].
1432///
1433/// Unlike dictionary iterators, this does not provide a `typed()` method to re-type an untyped `VarArray` iterator.
1434// Currently doesn't have a `typed()` method like dictionary iterators. Less useful here, as arrays are more often properly typed.
1435pub struct ArrayIter<'a, T: Element> {
1436 array: &'a Array<T>,
1437 next_idx: usize,
1438}
1439
1440impl<T: Element + FromGodot> Iterator for ArrayIter<'_, T> {
1441 type Item = T;
1442
1443 fn next(&mut self) -> Option<Self::Item> {
1444 if self.next_idx < self.array.len() {
1445 let idx = self.next_idx;
1446 self.next_idx += 1;
1447
1448 let element_ptr = self.array.ptr_or_null(idx);
1449
1450 // SAFETY: We just checked that the index is not out of bounds, so the pointer won't be null.
1451 // We immediately convert this to the right element, so barring `experimental-threads` the pointer won't be invalidated in time.
1452 let variant = unsafe { Variant::borrow_var_sys(element_ptr) };
1453 let element = T::from_variant(variant);
1454 Some(element)
1455 } else {
1456 None
1457 }
1458 }
1459
1460 fn size_hint(&self) -> (usize, Option<usize>) {
1461 let remaining = self.array.len() - self.next_idx;
1462 (remaining, Some(remaining))
1463 }
1464}
1465
1466// TODO There's a macro for this, but it doesn't support generics yet; add support and use it
1467impl<T: Element> PartialEq for Array<T> {
1468 #[inline]
1469 fn eq(&self, other: &Self) -> bool {
1470 unsafe {
1471 let mut result = false;
1472 sys::builtin_call! {
1473 array_operator_equal(self.sys(), other.sys(), result.sys_mut())
1474 }
1475 result
1476 }
1477 }
1478}
1479
1480impl<T: Element> PartialOrd for Array<T> {
1481 #[inline]
1482 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1483 let op_less = |lhs, rhs| unsafe {
1484 let mut result = false;
1485 sys::builtin_call! {
1486 array_operator_less(lhs, rhs, result.sys_mut())
1487 }
1488 result
1489 };
1490
1491 if op_less(self.sys(), other.sys()) {
1492 Some(std::cmp::Ordering::Less)
1493 } else if op_less(other.sys(), self.sys()) {
1494 Some(std::cmp::Ordering::Greater)
1495 } else if self.eq(other) {
1496 Some(std::cmp::Ordering::Equal)
1497 } else {
1498 None
1499 }
1500 }
1501}
1502
1503// ----------------------------------------------------------------------------------------------------------------------------------------------
1504// Expression macros
1505// Intra-doc-links use HTML to stay in same module (not switch to prelude when looking at godot::builtin).
1506
1507/// **Array**: constructs `Array` literals for all possible element types.
1508///
1509/// # Type inference
1510/// There are three related macros, all of which create [`Array<T>`] expressions, but they differ in how the type `T` is inferred:
1511///
1512/// - `array!` uses [`AsArg`][crate::meta::AsArg] to push elements. This works when the array's element type `T` is already
1513/// determined from context -- a type annotation, function parameter, etc. This supports all element types including `Gd<T>` and `Variant`.
1514/// - [`iarray!`](macro.iarray.html) uses [`AsDirectElement`][crate::meta::AsDirectElement] for (opinionated) type inference from literals.
1515/// This macro needs no type annotations, however is limited to common types, like `i32`, `&str` (inferred as `GString`), etc.
1516/// - [`varray!`](macro.varray.html) uses `AsArg<Variant>`, meaning it's like `array!` but inferred as [`VarArray`].
1517///
1518/// # Examples
1519/// ```no_run
1520/// # use godot::prelude::*;
1521/// // array! requires type context (e.g. annotation, return type, etc.).
1522/// // The same expression can be used to initialize different array types:
1523///
1524/// let ints: Array<i8> = array![3, 1, 4];
1525/// let ints: Array<i64> = array![3, 1, 4];
1526/// let ints: Array<Variant> = array![3, 1, 4];
1527///
1528/// let strs: Array<GString> = array!["a", "b"];
1529/// let strs: Array<StringName> = array!["a", "b"];
1530/// let strs: Array<Variant> = array!["a", "b"];
1531///
1532/// // More strict inference with iarray! and varray! macros:
1533///
1534/// let ints = iarray![3, 1, 4]; // Array<i32>.
1535/// let strs = iarray!["a", "b"]; // Array<GString>.
1536///
1537/// let strs = varray!["a", "b"]; // VarArray.
1538/// ```
1539///
1540/// # See also
1541/// For dictionaries, a similar macro [`dict!`](macro.dict.html) exists.
1542///
1543/// To construct slices of variants, use [`vslice!`](macro.vslice.html).
1544#[macro_export]
1545macro_rules! array {
1546 ($($elements:expr_2021),* $(,)?) => {
1547 {
1548 let mut array = $crate::builtin::Array::default();
1549 $(array.push($elements);)*
1550 array
1551 }
1552 };
1553}
1554
1555/// **I**nferred **array**: constructs `Array` literals without ambiguity.
1556///
1557/// See [`array!`](macro.array.html) for docs and examples.
1558#[macro_export]
1559macro_rules! iarray {
1560 ($($elements:expr_2021),* $(,)?) => {
1561 {
1562 let mut array = $crate::builtin::Array::default();
1563 $(array.__macro_push_direct($elements);)*
1564 array
1565 }
1566 };
1567}
1568
1569/// **V**ariant **array**: constructs [`VarArray`] literals.
1570///
1571/// See [`array!`](macro.array.html) for docs and examples.
1572#[macro_export]
1573macro_rules! varray {
1574 // Uses `AsArg` semantics, consistent with `array!`.
1575 ($($elements:expr_2021),* $(,)?) => {
1576 {
1577 let mut array = $crate::builtin::VarArray::default();
1578 $(
1579 array.push($elements);
1580 )*
1581 array
1582 } as $crate::builtin::VarArray
1583 // The `as` cast is necessary for Deref coercion to AnyArray; type inference doesn't seem to pick it up otherwise.
1584 };
1585}
1586
1587/// Constructs a slice of [`Variant`] literals, useful for passing to vararg functions.
1588///
1589/// Many APIs in Godot have variable-length arguments. GDScript can call such functions by simply passing more arguments, but in Rust,
1590/// the parameter type `&[Variant]` is used.
1591///
1592/// This macro creates a [slice](https://doc.rust-lang.org/std/primitive.slice.html) of `Variant` values.
1593///
1594/// # Examples
1595/// ## Variable number of arguments
1596/// ```no_run
1597/// # use godot::prelude::*;
1598/// let slice: &[Variant] = vslice![42, "hello", true];
1599/// let concat: GString = godot::global::str(slice);
1600/// ```
1601/// _In practice, you might want to use [`godot_str!`][crate::global::godot_str] instead of `str()`._
1602///
1603/// ## Dynamic function call via reflection
1604/// NIL can still be passed inside `vslice!`, just use `Variant::nil()`.
1605/// ```no_run
1606/// # use godot::prelude::*;
1607/// # fn some_object() -> Gd<Object> { unimplemented!() }
1608/// let mut obj: Gd<Object> = some_object();
1609///
1610/// obj.call("some_method", vslice![
1611/// Vector2i::new(1, 2),
1612/// Variant::nil(),
1613/// ]);
1614/// ```
1615///
1616/// # See also
1617/// To create typed and untyped `Array`s, use the [`array!`] macro and its variants.
1618///
1619/// For dictionaries, a similar macro [`vdict!`] exists.
1620#[macro_export]
1621macro_rules! vslice {
1622 // Note: use to_variant() and not Variant::from(), as that works with both references and values.
1623 ($($elements:expr_2021),* $(,)?) => {
1624 &[$( $crate::meta::ToGodot::to_variant(&$elements), )*]
1625 };
1626}
1627
1628// ----------------------------------------------------------------------------------------------------------------------------------------------
1629// Serde support
1630
1631#[cfg(feature = "serde")] #[cfg_attr(published_docs, doc(cfg(feature = "serde")))]
1632mod serialize {
1633 use std::marker::PhantomData;
1634
1635 use serde::de::{SeqAccess, Visitor};
1636 use serde::ser::SerializeSeq;
1637 use serde::{Deserialize, Deserializer, Serialize, Serializer};
1638
1639 use super::*;
1640
1641 impl<T> Serialize for Array<T>
1642 where
1643 T: Element + Serialize,
1644 {
1645 #[inline]
1646 fn serialize<S>(
1647 &self,
1648 serializer: S,
1649 ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
1650 where
1651 S: Serializer,
1652 {
1653 let mut sequence = serializer.serialize_seq(Some(self.len()))?;
1654 for e in self.iter_shared() {
1655 sequence.serialize_element(&e)?
1656 }
1657 sequence.end()
1658 }
1659 }
1660
1661 impl<'de, T> Deserialize<'de> for Array<T>
1662 where
1663 T: Element + Deserialize<'de>,
1664 {
1665 #[inline]
1666 fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
1667 where
1668 D: Deserializer<'de>,
1669 {
1670 struct ArrayVisitor<T>(PhantomData<T>);
1671 impl<'de, T> Visitor<'de> for ArrayVisitor<T>
1672 where
1673 T: Element + Deserialize<'de>,
1674 {
1675 type Value = Array<T>;
1676
1677 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1678 formatter.write_str(std::any::type_name::<Self::Value>())
1679 }
1680
1681 fn visit_seq<A>(
1682 self,
1683 mut seq: A,
1684 ) -> Result<Self::Value, <A as SeqAccess<'de>>::Error>
1685 where
1686 A: SeqAccess<'de>,
1687 {
1688 let mut vec = seq.size_hint().map_or_else(Vec::new, Vec::with_capacity);
1689 while let Some(val) = seq.next_element::<T>()? {
1690 vec.push(val);
1691 }
1692 Ok(Self::Value::from(vec.as_slice()))
1693 }
1694 }
1695
1696 deserializer.deserialize_seq(ArrayVisitor::<T>(PhantomData))
1697 }
1698 }
1699}
1700
1701// ----------------------------------------------------------------------------------------------------------------------------------------------
1702// Tests
1703
1704/// Verifies that `AnyArray::try_cast*()` cannot be called on concrete arrays.
1705///
1706/// > error[E0507]: cannot move out of dereference of `godot::prelude::Array<i64>`
1707///
1708/// ```compile_fail
1709/// use godot::prelude::*;
1710/// let a: Array<i64> = array![1, 2, 3];
1711/// a.try_cast_var_array();
1712/// ```
1713///
1714/// ```compile_fail
1715/// use godot::prelude::*;
1716/// let a: VarArray = varray![1, 2, 3];
1717/// a.try_cast_array::<i64>();
1718/// ```
1719///
1720/// This works:
1721/// ```no_run
1722/// use godot::prelude::*;
1723/// let a: VarArray = varray![1, 2, 3];
1724/// a.upcast_any_array().try_cast_array::<i64>();
1725/// ```
1726fn __cannot_downcast_from_concrete() {}
1727
1728#[test]
1729fn correct_variant_t() {
1730 assert!(Array::<Variant>::has_variant_t());
1731 assert!(!Array::<i64>::has_variant_t());
1732}