Skip to content

Commit 7d576c5

Browse files
authored
Merge pull request #785 from Jersonrn/master
Added implementation to convert Vec<$Element> into $PackedArray types.
2 parents 4dc2720 + 6abe4f6 commit 7d576c5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

godot-core/src/builtin/collections/packed_array.rs

+7
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ macro_rules! impl_packed_array {
455455
}
456456
}
457457

458+
#[doc = concat!("Creates a `", stringify!($PackedArray), "` from the given Rust vec.")]
459+
impl From<Vec<$Element>> for $PackedArray {
460+
fn from(vec: Vec<$Element>) -> Self{
461+
vec.into_iter().collect()
462+
}
463+
}
464+
458465
#[doc = concat!("Creates a `", stringify!($PackedArray), "` from an iterator.")]
459466
impl FromIterator<$Element> for $PackedArray {
460467
fn from_iter<I: IntoIterator<Item = $Element>>(iter: I) -> Self {

itest/rust/src/builtin_tests/containers/packed_array_test.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
use crate::framework::{expect_panic, itest};
9-
use godot::builtin::{PackedByteArray, PackedFloat32Array, PackedStringArray};
9+
use godot::builtin::{PackedByteArray, PackedFloat32Array, PackedInt32Array, PackedStringArray};
1010

1111
#[itest]
1212
fn packed_array_default() {
@@ -27,6 +27,24 @@ fn packed_array_from_iterator() {
2727
assert_eq!(array[1], 2);
2828
}
2929

30+
#[itest]
31+
fn packed_array_from_vec_str() {
32+
let string_array = PackedStringArray::from(vec!["hello".into(), "world".into()]);
33+
34+
assert_eq!(string_array.len(), 2);
35+
assert_eq!(string_array[0], "hello".into());
36+
assert_eq!(string_array[1], "world".into());
37+
}
38+
39+
#[itest]
40+
fn packed_array_from_vec_i32() {
41+
let int32_array = PackedInt32Array::from(vec![1, 2]);
42+
43+
assert_eq!(int32_array.len(), 2);
44+
assert_eq!(int32_array[0], 1);
45+
assert_eq!(int32_array[1], 2);
46+
}
47+
3048
#[itest]
3149
fn packed_array_to_vec() {
3250
let array = PackedByteArray::new();

0 commit comments

Comments
 (0)