Skip to content

Commit

Permalink
Merge #182
Browse files Browse the repository at this point in the history
182: Fix handling of empty packed array in `to_vec` r=Bromeon a=bluenote10

Closes #181

Co-authored-by: Fabian Keller <github.100.fkeller@spamgourmet.com>
  • Loading branch information
bors[bot] and bluenote10 authored Mar 14, 2023
2 parents f05915f + 888354b commit 6bfe53e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 11 additions & 9 deletions godot-core/src/builtin/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions itest/rust/src/packed_array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit 6bfe53e

Please sign in to comment.