owned_into_arg

Function owned_into_arg 

pub fn owned_into_arg<'r, T>(owned_val: T) -> impl AsArg<T> + 'r
where T: ToGodot + 'r,
Expand description

Generic abstraction over T owned values that should be passed as AsArg<T>.

Useful for generic programming: you have owned values, and want the argument conversion to benefit from moving whenever possible. You don’t care if the value can truly be moved efficiently, since you don’t need the value at the call site anymore.

Note that the pattern owned_into_arg(value.clone()) is inefficient – instead, use ref_to_arg(&value).

§Example

use godot::prelude::*;
use godot::meta::{ArrayElement, owned_into_arg};

// Creates random values, e.g. for fuzzing, property-based testing, etc.
// Assume global state for simplicity.
trait Generator {
   fn next() -> Self;
}

fn fill_randomly<T>(arr: &mut Array<T>, count: usize)
where
    T: ArrayElement + ToGodot + Generator,
{
    for _ in 0..count {
        let value = T::next();
        arr.push(owned_into_arg(value));
    }
}