Struct PackedColorArray
pub struct PackedColorArray { /* private fields */ }
Expand description
Implements Godot’s PackedColorArray
type,
which is a space-efficient array of Color
s.
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 PackedColorArray
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§
§impl PackedColorArray
impl PackedColorArray
pub fn new() -> PackedColorArray
pub fn new() -> PackedColorArray
Constructs an empty array.
pub fn get(&self, index: usize) -> Option<Color>
pub fn get(&self, index: usize) -> Option<Color>
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.
pub fn contains(&self, value: impl AsArg<Color>) -> bool
pub fn contains(&self, value: impl AsArg<Color>) -> bool
Returns true
if the array contains the given value.
Godot equivalent: has
pub fn count(&self, value: impl AsArg<Color>) -> usize
pub fn count(&self, value: impl AsArg<Color>) -> usize
Returns the number of times a value is in the array.
pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the array. Equivalent of size()
in Godot.
pub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array, removing all elements.
pub fn push(&mut self, value: impl AsArg<Color>)
pub fn push(&mut self, value: impl AsArg<Color>)
Appends an element to the end of the array. Equivalent of append
and push_back
in GDScript.
pub fn insert(&mut self, index: usize, value: impl AsArg<Color>)
pub fn insert(&mut self, index: usize, value: impl AsArg<Color>)
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.
pub fn remove(&mut self, index: usize) -> Color
pub fn remove(&mut self, index: usize) -> Color
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.
pub fn fill(&mut self, value: impl AsArg<Color>)
pub fn fill(&mut self, value: impl AsArg<Color>)
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.
pub fn resize(&mut self, size: usize)
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()
.
pub fn extend_array(&mut self, other: &PackedColorArray)
pub fn extend_array(&mut self, other: &PackedColorArray)
Appends another array at the end of this array. Equivalent of append_array
in GDScript.
pub fn to_vec(&self) -> Vec<Color>
pub fn to_vec(&self) -> Vec<Color>
Converts this array to a Rust vector, making a copy of its contents.
pub fn subarray(&self, begin: usize, end: usize) -> PackedColorArray
pub fn subarray(&self, begin: usize, end: usize) -> PackedColorArray
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
.
pub fn as_slice(&self) -> &[Color]
pub fn as_slice(&self) -> &[Color]
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.
pub fn as_mut_slice(&mut self) -> &mut [Color]
pub fn as_mut_slice(&mut self) -> &mut [Color]
pub fn find(
&self,
value: impl AsArg<Color>,
from: Option<usize>,
) -> Option<usize>
pub fn find( &self, value: impl AsArg<Color>, 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.
pub fn rfind(
&self,
value: impl AsArg<Color>,
from: Option<usize>,
) -> Option<usize>
pub fn rfind( &self, value: impl AsArg<Color>, 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.
pub fn bsearch(&self, value: impl AsArg<Color>) -> usize
pub fn bsearch(&self, value: impl AsArg<Color>) -> 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.
pub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the elements in the array.
pub fn sort(&mut self)
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.
pub fn to_byte_array(&self) -> PackedByteArray
pub fn to_byte_array(&self) -> PackedByteArray
Returns a PackedByteArray
with each value encoded as bytes.
Trait Implementations§
§impl Clone for PackedColorArray
impl Clone for PackedColorArray
§fn clone(&self) -> PackedColorArray
fn clone(&self) -> PackedColorArray
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl Debug for PackedColorArray
impl Debug for PackedColorArray
§impl Default for PackedColorArray
impl Default for PackedColorArray
§fn default() -> PackedColorArray
fn default() -> PackedColorArray
§impl Display for PackedColorArray
impl Display for PackedColorArray
§impl Export for PackedColorArray
impl Export for PackedColorArray
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§impl Extend<Color> for PackedColorArray
Extends aPackedColorArray
with the contents of an iterator
impl Extend<Color> for PackedColorArray
Extends aPackedColorArray
with the contents of an iterator
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Color>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Color>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl From<&[Color]> for PackedColorArray
Creates a PackedColorArray
from the given slice.
impl From<&[Color]> for PackedColorArray
Creates a PackedColorArray
from the given slice.
§fn from(slice: &[Color]) -> PackedColorArray
fn from(slice: &[Color]) -> PackedColorArray
§impl<const N: usize> From<&[Color; N]> for PackedColorArray
Creates a PackedColorArray
from the given Rust array.
impl<const N: usize> From<&[Color; N]> for PackedColorArray
Creates a PackedColorArray
from the given Rust array.
§fn from(arr: &[Color; N]) -> PackedColorArray
fn from(arr: &[Color; N]) -> PackedColorArray
§impl From<&Array<Variant>> for PackedColorArray
impl From<&Array<Variant>> for PackedColorArray
§fn from(other: &Array<Variant>) -> PackedColorArray
fn from(other: &Array<Variant>) -> PackedColorArray
§impl From<&PackedColorArray> for Array<Variant>
impl From<&PackedColorArray> for Array<Variant>
§fn from(other: &PackedColorArray) -> Array<Variant>
fn from(other: &PackedColorArray) -> Array<Variant>
§impl<const N: usize> From<[Color; N]> for PackedColorArray
Creates a PackedColorArray
from the given Rust array.
impl<const N: usize> From<[Color; N]> for PackedColorArray
Creates a PackedColorArray
from the given Rust array.
§fn from(arr: [Color; N]) -> PackedColorArray
fn from(arr: [Color; N]) -> PackedColorArray
§impl From<Vec<Color>> for PackedColorArray
Creates a PackedColorArray
from the given Rust vec.
impl From<Vec<Color>> for PackedColorArray
Creates a PackedColorArray
from the given Rust vec.
§fn from(vec: Vec<Color>) -> PackedColorArray
fn from(vec: Vec<Color>) -> PackedColorArray
§impl FromGodot for PackedColorArray
impl FromGodot for PackedColorArray
§fn try_from_godot(
via: <PackedColorArray as GodotConvert>::Via,
) -> Result<PackedColorArray, ConvertError>
fn try_from_godot( via: <PackedColorArray as GodotConvert>::Via, ) -> Result<PackedColorArray, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl FromIterator<Color> for PackedColorArray
Creates a PackedColorArray
from an iterator.
impl FromIterator<Color> for PackedColorArray
Creates a PackedColorArray
from an iterator.
§fn from_iter<I>(iter: I) -> PackedColorArraywhere
I: IntoIterator<Item = Color>,
fn from_iter<I>(iter: I) -> PackedColorArraywhere
I: IntoIterator<Item = Color>,
§impl GodotConvert for PackedColorArray
impl GodotConvert for PackedColorArray
§type Via = PackedColorArray
type Via = PackedColorArray
Self
is represented in Godot.§impl Index<usize> for PackedColorArray
impl Index<usize> for PackedColorArray
§impl IndexMut<usize> for PackedColorArray
impl IndexMut<usize> for PackedColorArray
§impl ParamType for PackedColorArray
impl ParamType for PackedColorArray
§fn owned_to_arg<'v>(self) -> <PackedColorArray as ParamType>::Arg<'v>
fn owned_to_arg<'v>(self) -> <PackedColorArray as ParamType>::Arg<'v>
impl AsArg<T>
. Read more§impl PartialEq for PackedColorArray
impl PartialEq for PackedColorArray
§impl ToGodot for PackedColorArray
impl ToGodot for PackedColorArray
§type ToVia<'v> = <PackedColorArray as GodotConvert>::Via
type ToVia<'v> = <PackedColorArray as GodotConvert>::Via
§fn to_godot(&self) -> <PackedColorArray as ToGodot>::ToVia<'_>
fn to_godot(&self) -> <PackedColorArray as ToGodot>::ToVia<'_>
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl Var for PackedColorArray
impl Var for PackedColorArray
fn get_property(&self) -> <PackedColorArray as GodotConvert>::Via
fn set_property(&mut self, value: <PackedColorArray as GodotConvert>::Via)
§fn var_hint() -> PropertyHintInfo
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.