ArrayFunctionalOps

Struct ArrayFunctionalOps 

pub struct ArrayFunctionalOps<'a, T>
where T: ArrayElement,
{ /* private fields */ }
Expand description

Immutable, functional-programming operations for Array, based on Godot callables.

Returned by Array::functional_ops().

These methods exist to provide parity with Godot, e.g. when porting GDScript code to Rust. However, they come with several disadvantages compared to Rust’s iterator adapters:

  • Not type-safe: callables are dynamically typed, so you need to double-check signatures. Godot may misinterpret returned values (e.g. predicates apply to any “truthy” values, not just booleans).
  • Slower: dispatching through callables is typically more costly than iterating over variants, especially since every call involves multiple variant conversions, too. Combining multiple operations like filter().map() is very expensive due to intermediate allocations.
  • Less composable/flexible: Godot’s map() always returns an untyped array, even if the input is typed and unchanged by the mapping. Rust’s collect() on the other hand gives you control over the output type. Chaining iterators can apply multiple transformations lazily.

In many cases, it is thus better to use Array::iter_shared() combined with iterator adapters. Check the individual method docs of this struct for concrete alternatives.

Implementations§

§

impl<'a, T> ArrayFunctionalOps<'a, T>
where T: ArrayElement,

pub fn filter(&self, callable: &Callable) -> Array<T>

Returns a new array containing only the elements for which the callable returns a truthy value.

Rust alternatives: Iterator::filter().

The callable has signature fn(T) -> bool.

§Example
let array = array![1, 2, 3, 4, 5];
let even = array.functional_ops().filter(&Callable::from_fn("is_even", |args| {
    args[0].to::<i64>() % 2 == 0
}));
assert_eq!(even, array![2, 4]);

pub fn map(&self, callable: &Callable) -> Array<Variant>

Returns a new untyped array with each element transformed by the callable.

Rust alternatives: Iterator::map().

The callable has signature fn(T) -> Variant. Since the transformation can change the element type, this method returns a VariantArray (untyped array).

§Example
let array = array![1.1, 1.5, 1.9];
let rounded = array.functional_ops().map(&Callable::from_fn("round", |args| {
    args[0].to::<f64>().round() as i64
}));
assert_eq!(rounded, varray![1, 2, 2]);

pub fn reduce(&self, callable: &Callable, initial: &Variant) -> Variant

Reduces the array to a single value by iteratively applying the callable.

Rust alternatives: Iterator::fold() or Iterator::reduce().

The callable takes two arguments: the accumulator and the current element. It returns the new accumulator value. The process starts with initial as the accumulator.

§Example
let array = array![1, 2, 3, 4];
let sum = array.functional_ops().reduce(
    &Callable::from_fn("sum", |args| {
        args[0].to::<i64>() + args[1].to::<i64>()
    }),
    &0.to_variant()
);
assert_eq!(sum, 10.to_variant());

pub fn any(&self, callable: &Callable) -> bool

Returns true if the callable returns a truthy value for at least one element.

Rust alternatives: Iterator::any().

The callable has signature fn(element) -> bool.

§Example
let array = array![1, 2, 3, 4];
let any_even = array.functional_ops().any(&Callable::from_fn("is_even", |args| {
    args[0].to::<i64>() % 2 == 0
}));
assert!(any_even);

pub fn all(&self, callable: &Callable) -> bool

Returns true if the callable returns a truthy value for all elements.

Rust alternatives: Iterator::all().

The callable has signature fn(element) -> bool.

§Example
let array = array![2, 4, 6];
let all_even = array.functional_ops().all(&Callable::from_fn("is_even", |args| {
    args[0].to::<i64>() % 2 == 0
}));
assert!(all_even);

pub fn find_custom( &self, callable: &Callable, from: Option<usize>, ) -> Option<usize>

Available on since_api=4.4 only.

Finds the index of the first element matching a custom predicate.

Rust alternatives: Iterator::position().

The callable has signature fn(element) -> bool.

Returns the index of the first element for which the callable returns a truthy value, starting from from. If no element matches, returns None.

§Example
let array = array![1, 2, 3, 4, 5];
let is_even = Callable::from_fn("is_even", |args| {
    args[0].to::<i64>() % 2 == 0
});
assert_eq!(array.functional_ops().find_custom(&is_even, None), Some(1)); // value 2
assert_eq!(array.functional_ops().find_custom(&is_even, Some(2)), Some(3)); // value 4

pub fn rfind_custom( &self, callable: &Callable, from: Option<usize>, ) -> Option<usize>

Available on since_api=4.4 only.

Finds the index of the last element matching a custom predicate, searching backwards.

Rust alternatives: Iterator::rposition().

The callable has signature fn(element) -> bool.

Returns the index of the last element for which the callable returns a truthy value, searching backwards from from. If no element matches, returns None.

§Example
let array = array![1, 2, 3, 4, 5];
let is_even = Callable::from_fn("is_even", |args| {
    args[0].to::<i64>() % 2 == 0
});
assert_eq!(array.functional_ops().rfind_custom(&is_even, None), Some(3)); // value 4
assert_eq!(array.functional_ops().rfind_custom(&is_even, Some(2)), Some(1)); // value 2

pub fn bsearch_custom(&self, value: impl AsArg<T>, pred: &Callable) -> usize

Finds the index of a value in a sorted array using binary search, with Callable custom predicate.

The callable pred takes two elements (a, b) and should return if a < b (strictly less). For a type-safe version, check out Array::bsearch_by().

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

Calling bsearch_custom() on an unsorted array results in unspecified behavior. Consider using Array::sort_unstable_custom() to ensure the sorting order is compatible with your callable’s ordering.

Auto Trait Implementations§

§

impl<'a, T> Freeze for ArrayFunctionalOps<'a, T>

§

impl<'a, T> !RefUnwindSafe for ArrayFunctionalOps<'a, T>

§

impl<'a, T> !Send for ArrayFunctionalOps<'a, T>

§

impl<'a, T> !Sync for ArrayFunctionalOps<'a, T>

§

impl<'a, T> Unpin for ArrayFunctionalOps<'a, T>

§

impl<'a, T> !UnwindSafe for ArrayFunctionalOps<'a, T>

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