diff --git a/godot-core/src/builtin/packed_array.rs b/godot-core/src/builtin/packed_array.rs index 2348eed68..e07ef9752 100644 --- a/godot-core/src/builtin/packed_array.rs +++ b/godot-core/src/builtin/packed_array.rs @@ -85,15 +85,17 @@ macro_rules! impl_packed_array { pub fn to_vec(&self) -> Vec<$Element> { let len = self.len(); let mut vec = Vec::with_capacity(len); - let ptr = self.ptr(0); - for offset in 0..to_isize(len) { - // SAFETY: Packed arrays are stored contiguously in memory, so we can use - // pointer arithmetic instead of going through `$operator_index_const` for - // every index. - // Note that we do need to use `.clone()` because `GodotString` is refcounted; - // we can't just do a memcpy. - let element = unsafe { (*ptr.offset(offset)).clone() }; - vec.push(element); + if len > 0 { + let ptr = self.ptr(0); + for offset in 0..to_isize(len) { + // SAFETY: Packed arrays are stored contiguously in memory, so we can use + // pointer arithmetic instead of going through `$operator_index_const` for + // every index. + // Note that we do need to use `.clone()` because `GodotString` is refcounted; + // we can't just do a memcpy. + let element = unsafe { (*ptr.offset(offset)).clone() }; + vec.push(element); + } } vec } diff --git a/itest/rust/src/packed_array_test.rs b/itest/rust/src/packed_array_test.rs index 08e2990b1..48f073b77 100644 --- a/itest/rust/src/packed_array_test.rs +++ b/itest/rust/src/packed_array_test.rs @@ -37,6 +37,8 @@ fn packed_array_from() { #[itest] fn packed_array_to_vec() { + let array = PackedByteArray::new(); + assert_eq!(array.to_vec(), vec![]); let array = PackedByteArray::from(&[1, 2]); assert_eq!(array.to_vec(), vec![1, 2]); }