Skip to content

Commit 6bfe53e

Browse files
Merge #182
182: Fix handling of empty packed array in `to_vec` r=Bromeon a=bluenote10 Closes #181 Co-authored-by: Fabian Keller <[email protected]>
2 parents f05915f + 888354b commit 6bfe53e

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

godot-core/src/builtin/packed_array.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ macro_rules! impl_packed_array {
8585
pub fn to_vec(&self) -> Vec<$Element> {
8686
let len = self.len();
8787
let mut vec = Vec::with_capacity(len);
88-
let ptr = self.ptr(0);
89-
for offset in 0..to_isize(len) {
90-
// SAFETY: Packed arrays are stored contiguously in memory, so we can use
91-
// pointer arithmetic instead of going through `$operator_index_const` for
92-
// every index.
93-
// Note that we do need to use `.clone()` because `GodotString` is refcounted;
94-
// we can't just do a memcpy.
95-
let element = unsafe { (*ptr.offset(offset)).clone() };
96-
vec.push(element);
88+
if len > 0 {
89+
let ptr = self.ptr(0);
90+
for offset in 0..to_isize(len) {
91+
// SAFETY: Packed arrays are stored contiguously in memory, so we can use
92+
// pointer arithmetic instead of going through `$operator_index_const` for
93+
// every index.
94+
// Note that we do need to use `.clone()` because `GodotString` is refcounted;
95+
// we can't just do a memcpy.
96+
let element = unsafe { (*ptr.offset(offset)).clone() };
97+
vec.push(element);
98+
}
9799
}
98100
vec
99101
}

itest/rust/src/packed_array_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ fn packed_array_from() {
3737

3838
#[itest]
3939
fn packed_array_to_vec() {
40+
let array = PackedByteArray::new();
41+
assert_eq!(array.to_vec(), vec![]);
4042
let array = PackedByteArray::from(&[1, 2]);
4143
assert_eq!(array.to_vec(), vec![1, 2]);
4244
}

0 commit comments

Comments
 (0)