Struct PackedStringArray

Source
pub struct PackedStringArray { /* private fields */ }
Expand description

Space-efficient array of GString elements.

Check out the book for a tutorial on packed arrays.

Note that, unlike Array, this type has value semantics: each copy will be independent of the original. Under the hood, Godot uses copy-on-write, so copies are still cheap to make.

§Registering properties

You can use both #[var] and #[export] with packed arrays. However, since they use copy-on-write, GDScript (for #[var]) and the editor (for #[export]) will effectively keep an independent copy of the array. Writes to the packed array from Rust are thus not reflected on the other side – you may need to replace the entire array.

See also godot/#76150 for details.

§Thread safety

Usage is safe if the PackedStringArray is used on a single thread only. Concurrent reads on different threads are also safe, but any writes must be externally synchronized. The Rust compiler will enforce this as long as you use only Rust threads, but it cannot protect against concurrent modification on other threads (e.g. created through GDScript).

Implementations§

Source§

impl PackedStringArray

Source

pub fn new() -> PackedStringArray

Constructs an empty array.

Source

pub fn get(&self, index: usize) -> Option<GString>

Returns a copy of the value at the specified index, or None if out-of-bounds.

If you know the index is valid, use the [] operator (Index/IndexMut traits) instead.

Source

pub fn contains(&self, value: impl AsArg<GString>) -> bool

Returns true if the array contains the given value.

Godot equivalent: has

Source

pub fn count(&self, value: impl AsArg<GString>) -> usize

Returns the number of times a value is in the array.

Source

pub fn len(&self) -> usize

Returns the number of elements in the array. Equivalent of size() in Godot.

Source

pub fn is_empty(&self) -> bool

Returns true if the array is empty.

Source

pub fn clear(&mut self)

Clears the array, removing all elements.

Source

pub fn push(&mut self, value: impl AsArg<GString>)

Appends an element to the end of the array. Equivalent of append and push_back in GDScript.

Source

pub fn insert(&mut self, index: usize, value: impl AsArg<GString>)

Inserts a new element at a given index in the array. The index must be valid, or at the end of the array (index == len()).

Note: On large arrays, this method is much slower than push as it will move all the array’s elements after the inserted element. The larger the array, the slower insert will be.

Source

pub fn remove(&mut self, index: usize) -> GString

Removes and returns the element at the specified index. Similar to remove_at in GDScript, but also returns the removed value.

On large arrays, this method is much slower than pop_back as it will move all the array’s elements after the removed element. The larger the array, the slower remove will be.

§Panics

If index is out of bounds.

Source

pub fn fill(&mut self, value: impl AsArg<GString>)

Assigns the given value to all elements in the array. This can be used together with resize to create an array with a given size and initialized elements.

Source

pub fn resize(&mut self, size: usize)

Resizes the array to contain a different number of elements. If the new size is smaller, elements are removed from the end. If the new size is larger, new elements are set to Default::default().

Source

pub fn extend_array(&mut self, other: &PackedStringArray)

Appends another array at the end of this array. Equivalent of append_array in GDScript.

Source

pub fn to_vec(&self) -> Vec<GString>

Converts this array to a Rust vector, making a copy of its contents.

Source

pub fn subarray(&self, begin: usize, end: usize) -> PackedStringArray

Returns a sub-range begin..end, as a new packed array.

This method is called slice() in Godot. The values of begin (inclusive) and end (exclusive) will be clamped to the array size.

To obtain Rust slices, see as_slice and as_mut_slice.

Source

pub fn as_slice(&self) -> &[GString]

Returns a shared Rust slice of the array.

The resulting slice can be further subdivided or converted into raw pointers.

See also as_mut_slice to get exclusive slices, and subarray to get a sub-array as a copy.

Source

pub fn as_mut_slice(&mut self) -> &mut [GString]

Returns an exclusive Rust slice of the array.

The resulting slice can be further subdivided or converted into raw pointers.

See also as_slice to get shared slices, and subarray to get a sub-array as a copy.

Source

pub fn find( &self, value: impl AsArg<GString>, from: Option<usize>, ) -> Option<usize>

Searches the array for the first occurrence of a value and returns its index, or None if not found. Starts searching at index from; pass None to search the entire array.

Source

pub fn rfind( &self, value: impl AsArg<GString>, from: Option<usize>, ) -> Option<usize>

Searches the array backwards for the last occurrence of a value and returns its index, or None if not found. Starts searching at index from; pass None to search the entire array.

Source

pub fn bsearch(&self, value: impl AsArg<GString>) -> usize

Finds the index of an existing value in a sorted array using binary search.

If the value is not present in the array, returns the insertion index that would maintain sorting order.

Calling bsearch() on an unsorted array results in unspecified (but safe) behavior.

Source

pub fn reverse(&mut self)

Reverses the order of the elements in the array.

Source

pub fn sort(&mut self)

Sorts the elements of the array in ascending order.

This sort is stable, since elements inside packed arrays are indistinguishable. Relative order between equal elements thus isn’t observable.

Source

pub fn to_byte_array(&self) -> PackedByteArray

Returns a PackedByteArray with each value encoded as bytes.

Trait Implementations§

Source§

impl Clone for PackedStringArray

Source§

fn clone(&self) -> PackedStringArray

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PackedStringArray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for PackedStringArray

Source§

fn default() -> PackedStringArray

Returns the “default value” for a type. Read more
Source§

impl Display for PackedStringArray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats PackedArray to match Godot’s string representation.

Source§

impl Drop for PackedStringArray

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Export for PackedStringArray

Source§

fn export_hint() -> PropertyHintInfo

The export info to use for an exported field of this type, if no other export info is specified.
Source§

impl Extend<GString> for PackedStringArray

Extends aPackedStringArray with the contents of an iterator.

§Performance note

This uses the lower bound from Iterator::size_hint() to allocate memory up front. If the iterator returns more than that number of elements, it falls back to reading elements into a fixed-size buffer before adding them all efficiently as a batch.

§Panics

  • If the iterator’s size_hint() returns an incorrect lower bound (which is a breach of the Iterator protocol).
Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = GString>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<&[GString]> for PackedStringArray

Creates a PackedStringArray from the given slice.

Source§

fn from(slice: &[GString]) -> PackedStringArray

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[GString; N]> for PackedStringArray

Creates a PackedStringArray from the given Rust array.

Source§

fn from(arr: &[GString; N]) -> PackedStringArray

Converts to this type from the input type.
Source§

impl From<&Array<Variant>> for PackedStringArray

Source§

fn from(other: &Array<Variant>) -> PackedStringArray

Converts to this type from the input type.
Source§

impl From<&PackedStringArray> for Array<Variant>

Source§

fn from(other: &PackedStringArray) -> Array<Variant>

Converts to this type from the input type.
Source§

impl<const N: usize> From<[GString; N]> for PackedStringArray

Creates a PackedStringArray from the given Rust array.

Source§

fn from(arr: [GString; N]) -> PackedStringArray

Converts to this type from the input type.
Source§

impl From<Vec<GString>> for PackedStringArray

Creates a PackedStringArray from the given Rust vec.

Source§

fn from(vec: Vec<GString>) -> PackedStringArray

Converts to this type from the input type.
Source§

impl FromGodot for PackedStringArray

Source§

fn try_from_godot( via: <PackedStringArray as GodotConvert>::Via, ) -> Result<PackedStringArray, ConvertError>

Converts the Godot representation to this type, returning Err on failure.
Source§

fn from_godot(via: Self::Via) -> Self

⚠️ Converts the Godot representation to this type. Read more
Source§

fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>

Performs the conversion from a Variant, returning Err on failure.
Source§

fn from_variant(variant: &Variant) -> Self

⚠️ Performs the conversion from a Variant. Read more
Source§

impl FromIterator<GString> for PackedStringArray

Creates a PackedStringArray from an iterator.

§Performance note

This uses the lower bound from Iterator::size_hint() to allocate memory up front. If the iterator returns more than that number of elements, it falls back to reading elements into a fixed-size buffer before adding them all efficiently as a batch.

§Panics

  • If the iterator’s size_hint() returns an incorrect lower bound (which is a breach of the Iterator protocol).
Source§

fn from_iter<I>(iter: I) -> PackedStringArray
where I: IntoIterator<Item = GString>,

Creates a value from an iterator. Read more
Source§

impl GodotConvert for PackedStringArray

Source§

type Via = PackedStringArray

The type through which Self is represented in Godot.
Source§

impl Index<usize> for PackedStringArray

Source§

type Output = GString

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &<PackedStringArray as Index<usize>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for PackedStringArray

Source§

fn index_mut( &mut self, index: usize, ) -> &mut <PackedStringArray as Index<usize>>::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IntoDynamicSend for PackedStringArray

Source§

impl ParamType for PackedStringArray

Source§

type ArgPassing = ByRef

Source§

fn owned_to_arg(self) -> impl AsArg<Self>

👎Deprecated since 0.3.2: This method is no longer needed and will be removed in 0.4
Source§

impl PartialEq for PackedStringArray

Source§

fn eq(&self, other: &PackedStringArray) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl ToGodot for PackedStringArray

Source§

type ToVia<'v> = <PackedStringArray as GodotConvert>::Via

Target type of to_godot(), which can differ from Via for pass-by-reference types. Read more
Source§

fn to_godot(&self) -> <PackedStringArray as ToGodot>::ToVia<'_>

Converts this type to the Godot type by reference, usually by cloning.
Source§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
Source§

impl Var for PackedStringArray

Source§

fn get_property(&self) -> <PackedStringArray as GodotConvert>::Via

Source§

fn set_property(&mut self, value: <PackedStringArray as GodotConvert>::Via)

Source§

fn var_hint() -> PropertyHintInfo

Specific property hints, only override if they deviate from GodotType::property_info, e.g. for enums/newtypes.
Source§

impl ArrayElement for PackedStringArray

Source§

impl Eq for PackedStringArray

Source§

impl GodotType for PackedStringArray

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.