File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed
itest/rust/src/builtin_tests/containers Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ use godot_ffi as sys;
9
9
10
10
use crate :: builtin:: meta:: ToGodot ;
11
11
use crate :: builtin:: * ;
12
- use std:: fmt;
12
+ use std:: { fmt, ops } ;
13
13
use sys:: types:: * ;
14
14
use sys:: { ffi_methods, interface_fn, GodotFfi } ;
15
15
@@ -394,6 +394,24 @@ macro_rules! impl_packed_array {
394
394
}
395
395
}
396
396
397
+ impl ops:: Index <usize > for $PackedArray {
398
+ type Output = $Element;
399
+
400
+ fn index( & self , index: usize ) -> & Self :: Output {
401
+ let ptr = self . ptr( index) ;
402
+ // SAFETY: `ptr` checked bounds.
403
+ unsafe { & * ptr }
404
+ }
405
+ }
406
+
407
+ impl ops:: IndexMut <usize > for $PackedArray {
408
+ fn index_mut( & mut self , index: usize ) -> & mut Self :: Output {
409
+ let ptr = self . ptr_mut( index) ;
410
+ // SAFETY: `ptr` checked bounds.
411
+ unsafe { & mut * ptr }
412
+ }
413
+ }
414
+
397
415
#[ doc = concat!( "Creates a `" , stringify!( $PackedArray) , "` from the given Rust array." ) ]
398
416
impl <const N : usize > From <& [ $Element; N ] > for $PackedArray {
399
417
fn from( arr: & [ $Element; N ] ) -> Self {
Original file line number Diff line number Diff line change @@ -130,6 +130,31 @@ fn packed_array_as_mut_slice() {
130
130
assert_eq ! ( empty. as_mut_slice( ) , & mut [ ] ) ;
131
131
}
132
132
133
+ #[ itest]
134
+ fn packed_array_index ( ) {
135
+ let array = PackedByteArray :: from ( & [ 1 , 2 ] ) ;
136
+
137
+ assert_eq ! ( array[ 0 ] , 1 ) ;
138
+ assert_eq ! ( array[ 1 ] , 2 ) ;
139
+ expect_panic ( "Array index 2 out of bounds: length is 2" , || {
140
+ let _ = array[ 2 ] ;
141
+ } ) ;
142
+
143
+ let mut array = PackedStringArray :: new ( ) ;
144
+ expect_panic ( "Array index 0 out of bounds: length is 0" , || {
145
+ let _ = array[ 0 ] ;
146
+ } ) ;
147
+
148
+ array. push ( "first" . into ( ) ) ;
149
+ array. push ( "second" . into ( ) ) ;
150
+
151
+ assert_eq ! ( array[ 0 ] , "first" . into( ) ) ;
152
+ assert_eq ! ( array[ 1 ] , "second" . into( ) ) ;
153
+
154
+ array[ 0 ] = "begin" . into ( ) ;
155
+ assert_eq ! ( array[ 0 ] , "begin" . into( ) ) ;
156
+ }
157
+
133
158
#[ itest]
134
159
fn packed_array_get ( ) {
135
160
let array = PackedByteArray :: from ( & [ 1 , 2 ] ) ;
You can’t perform that action at this time.
0 commit comments