Skip to content

Add VariantArray duplicate method for deep copy #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions gdnative-core/src/core_types/variant_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ impl<Access: ThreadAccess> VariantArray<Access> {
}
}

/// Create a deep copy of the array.
///
/// This creates a new array and is **not** a cheap reference count
/// increment.
#[inline]
pub fn duplicate_deep(&self) -> VariantArray<Unique> {
unsafe {
let sys = (get_api().godot_array_duplicate)(self.sys(), true);
VariantArray::<Unique>::from_sys(sys)
}
}

/// Returns an iterator through all values in the `VariantArray`.
///
/// `VariantArray` is reference-counted and have interior mutability in Rust parlance.
Expand Down Expand Up @@ -647,6 +659,18 @@ godot_test!(test_array {
&[42, 1337, 512],
array3.iter().map(|v| v.try_to_i64().unwrap()).collect::<Vec<_>>().as_slice(),
);

let array4 = VariantArray::new(); // []
let array5 = VariantArray::new(); // []
array4.push(&foo); // [&foo]
array4.push(&bar); // [&foo, &bar]
array5.push(array4); // [[&foo, &bar]]

let array6 = array5.duplicate_deep(); // [[&foo, &bar]]
unsafe { array5.get(0).to_array().assume_unique().pop(); } // [[&foo]]

assert!(!array5.get(0).to_array().contains(&bar));
assert!(array6.get(0).to_array().contains(&bar));
});

godot_test!(
Expand Down