Struct godot::prelude::PackedByteArray

pub struct PackedByteArray { /* private fields */ }
Expand description

Implements Godot’s PackedByteArray type, which is an efficient array of u8s.

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 PackedByteArray 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 PackedByteArray

pub fn new() -> PackedByteArray

Constructs an empty array.

pub fn len(&self) -> usize

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

pub fn is_empty(&self) -> bool

Returns true if the array is empty.

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

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

pub fn clear(&mut self)

Clears the array, removing all elements.

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 subarray(&self, begin: usize, end: usize) -> PackedByteArray

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) -> &[u8]

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 [u8]

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.

pub fn get(&self, index: usize) -> u8

Returns a copy of the value at the specified index.

§Panics

If index is out of bounds.

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

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

Calling binary_search on an unsorted array results in unspecified behavior.

pub fn count(&self, value: u8) -> usize

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

pub fn contains(&self, value: u8) -> bool

Returns true if the array contains the given value. Equivalent of has in GDScript.

pub fn find(&self, value: u8, 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: u8, 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 set(&mut self, index: usize, value: u8)

Sets the value at the specified index.

§Panics

If index is out of bounds.

pub fn push(&mut self, value: u8)

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: u8)

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) -> u8

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: u8)

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 extend_array(&mut self, other: &PackedByteArray)

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

pub fn reverse(&mut self)

Reverses the order of the elements in the array.

pub fn sort(&mut self)

Sorts the elements of the array in ascending order.

pub fn to_float32_array(&self) -> PackedFloat32Array

Returns a copy of the data converted to a PackedFloat32Array, where each block of 4 bytes has been converted to a 32-bit float.

The size of the input array must be a multiple of 4 (size of 32-bit float). The size of the new array will be byte_array.size() / 4.

If the original data can’t be converted to 32-bit floats, the resulting data is undefined.

pub fn to_float64_array(&self) -> PackedFloat64Array

Returns a copy of the data converted to a PackedFloat64Array, where each block of 8 bytes has been converted to a 64-bit float.

The size of the input array must be a multiple of 8 (size of 64-bit float). The size of the new array will be byte_array.size() / 8.

If the original data can’t be converted to 64-bit floats, the resulting data is undefined.

pub fn to_int32_array(&self) -> PackedInt32Array

Returns a copy of the data converted to a PackedInt32Array, where each block of 4 bytes has been converted to a 32-bit integer.

The size of the input array must be a multiple of 4 (size of 32-bit integer). The size of the new array will be byte_array.size() / 4.

If the original data can’t be converted to 32-bit integers, the resulting data is undefined.

pub fn to_int64_array(&self) -> PackedInt64Array

Returns a copy of the data converted to a PackedInt64Array, where each block of 8 bytes has been converted to a 64-bit integer.

The size of the input array must be a multiple of 8 (size of 64-bit integer). The size of the new array will be byte_array.size() / 8.

If the original data can’t be converted to 64-bit integers, the resulting data is undefined.

Trait Implementations§

§

impl Clone for PackedByteArray

§

fn clone(&self) -> PackedByteArray

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl Debug for PackedByteArray

§

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

Formats the value using the given formatter. Read more
§

impl Default for PackedByteArray

§

fn default() -> PackedByteArray

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

impl Display for PackedByteArray

§

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

Formats PackedArray to match Godot’s string representation.

§

impl Drop for PackedByteArray

§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

impl Export for PackedByteArray

§

fn default_export_info() -> PropertyHintInfo

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

impl Extend<u8> for PackedByteArray

Extends aPackedByteArray with the contents of an iterator

§

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

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
§

impl From<&[u8]> for PackedByteArray

Creates a PackedByteArray from the given slice.

§

fn from(slice: &[u8]) -> PackedByteArray

Converts to this type from the input type.
§

impl<const N: usize> From<&[u8; N]> for PackedByteArray

Creates a PackedByteArray from the given Rust array.

§

fn from(arr: &[u8; N]) -> PackedByteArray

Converts to this type from the input type.
§

impl From<&Array<Variant>> for PackedByteArray

§

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

Converts to this type from the input type.
§

impl From<&PackedByteArray> for Array<Variant>

§

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

Converts to this type from the input type.
§

impl FromGodot for PackedByteArray

§

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

Performs the conversion.
§

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

⚠️ Performs the conversion. Read more
§

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

Performs the conversion from a Variant.
§

fn from_variant(variant: &Variant) -> Self

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

impl FromIterator<u8> for PackedByteArray

Creates a PackedByteArray from an iterator.

§

fn from_iter<I>(iter: I) -> PackedByteArray
where I: IntoIterator<Item = u8>,

Creates a value from an iterator. Read more
§

impl GodotConvert for PackedByteArray

§

type Via = PackedByteArray

The type through which Self is represented in Godot.
§

impl PartialEq for PackedByteArray

§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl ToGodot for PackedByteArray

§

fn to_godot(&self) -> <PackedByteArray as GodotConvert>::Via

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

fn into_godot(self) -> <PackedByteArray as GodotConvert>::Via

Converts this type to the Godot type. Read more
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl TypeStringHint for PackedByteArray

§

fn type_string() -> String

Returns the representation of this type as a type string. Read more
§

impl Var for PackedByteArray

§

impl ArrayElement for PackedByteArray

§

impl Eq for PackedByteArray

§

impl GodotType for PackedByteArray

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> 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,

§

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§

default 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>,

§

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>,

§

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.