File tree Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -85,15 +85,17 @@ macro_rules! impl_packed_array {
85
85
pub fn to_vec( & self ) -> Vec <$Element> {
86
86
let len = self . len( ) ;
87
87
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
+ }
97
99
}
98
100
vec
99
101
}
Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ fn packed_array_from() {
37
37
38
38
#[ itest]
39
39
fn packed_array_to_vec ( ) {
40
+ let array = PackedByteArray :: new ( ) ;
41
+ assert_eq ! ( array. to_vec( ) , vec![ ] ) ;
40
42
let array = PackedByteArray :: from ( & [ 1 , 2 ] ) ;
41
43
assert_eq ! ( array. to_vec( ) , vec![ 1 , 2 ] ) ;
42
44
}
You can’t perform that action at this time.
0 commit comments