Skip to content

Commit 7de34a0

Browse files
bors[bot]mivort
andauthored
Merge #711
711: Add VariantArray duplicate method for deep copy r=toasteater a=lufterc According to Godot's `Array` docs, `duplicate` method [can accept](https://docs.godotengine.org/en/stable/classes/class_array.html#class-array-method-duplicate) `deep` boolean parameter to perform a deep array copy. This patch adds this functionality to `VariantArray`'s interface. Simple deep copy functionality test was also added to `test_array` section. Co-authored-by: lufterc <lufterd@gmail.com>
2 parents e08ee8c + 973229f commit 7de34a0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

gdnative-core/src/core_types/variant_array.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ impl<Access: ThreadAccess> VariantArray<Access> {
157157
}
158158
}
159159

160+
/// Create a deep copy of the array.
161+
///
162+
/// This creates a new array and is **not** a cheap reference count
163+
/// increment.
164+
#[inline]
165+
pub fn duplicate_deep(&self) -> VariantArray<Unique> {
166+
unsafe {
167+
let sys = (get_api().godot_array_duplicate)(self.sys(), true);
168+
VariantArray::<Unique>::from_sys(sys)
169+
}
170+
}
171+
160172
/// Returns an iterator through all values in the `VariantArray`.
161173
///
162174
/// `VariantArray` is reference-counted and have interior mutability in Rust parlance.
@@ -647,6 +659,18 @@ godot_test!(test_array {
647659
&[42, 1337, 512],
648660
array3.iter().map(|v| v.try_to_i64().unwrap()).collect::<Vec<_>>().as_slice(),
649661
);
662+
663+
let array4 = VariantArray::new(); // []
664+
let array5 = VariantArray::new(); // []
665+
array4.push(&foo); // [&foo]
666+
array4.push(&bar); // [&foo, &bar]
667+
array5.push(array4); // [[&foo, &bar]]
668+
669+
let array6 = array5.duplicate_deep(); // [[&foo, &bar]]
670+
unsafe { array5.get(0).to_array().assume_unique().pop(); } // [[&foo]]
671+
672+
assert!(!array5.get(0).to_array().contains(&bar));
673+
assert!(array6.get(0).to_array().contains(&bar));
650674
});
651675

652676
godot_test!(

0 commit comments

Comments
 (0)