@@ -6,31 +6,22 @@ use crate::{
66 sys:: * ,
77 values:: Val ,
88} ;
9- use std:: { borrow:: Cow , mem:: zeroed} ;
9+ use derive_more:: From ;
10+ use std:: mem:: zeroed;
1011
1112/// Key for [Array].
12- #[ derive( Debug , Clone , PartialEq ) ]
13+ #[ derive( Debug , Clone , PartialEq , From ) ]
1314pub enum Key < ' a > {
1415 Index ( u64 ) ,
15- Str ( Cow < ' a , str > ) ,
16+ Str ( & ' a str ) ,
1617}
1718
18- impl From < u64 > for Key < ' _ > {
19- fn from ( i : u64 ) -> Self {
20- Key :: Index ( i)
21- }
22- }
23-
24- impl < ' a > From < & ' a str > for Key < ' a > {
25- fn from ( s : & ' a str ) -> Self {
26- Key :: Str ( Cow :: Borrowed ( s) )
27- }
28- }
29-
30- impl From < String > for Key < ' _ > {
31- fn from ( s : String ) -> Self {
32- Key :: Str ( Cow :: Owned ( s) )
33- }
19+ /// Insert key for [Array].
20+ #[ derive( Debug , Clone , PartialEq , From ) ]
21+ pub enum InsertKey < ' a > {
22+ NextIndex ,
23+ Index ( u64 ) ,
24+ Str ( & ' a str ) ,
3425}
3526
3627/// Wrapper of [crate::sys::zend_array].
@@ -53,14 +44,14 @@ impl Array {
5344 }
5445 }
5546
56- pub unsafe fn from_ptr < ' a > ( ptr : * const zend_array ) -> & ' a Array {
47+ pub unsafe fn from_ptr < ' a > ( ptr : * const zend_array ) -> Option < & ' a Array > {
5748 let ptr = ptr as * const Array ;
58- ptr. as_ref ( ) . expect ( "ptr shouldn't be null" )
49+ ptr. as_ref ( )
5950 }
6051
61- pub unsafe fn from_mut_ptr < ' a > ( ptr : * mut zend_array ) -> & ' a mut Array {
52+ pub unsafe fn from_mut_ptr < ' a > ( ptr : * mut zend_array ) -> Option < & ' a mut Array > {
6253 let ptr = ptr as * mut Array ;
63- ptr. as_mut ( ) . expect ( "ptr shouldn't be null" )
54+ ptr. as_mut ( )
6455 }
6556
6657 pub fn as_ptr ( & self ) -> * const zend_array {
@@ -72,15 +63,21 @@ impl Array {
7263 }
7364
7465 // Add or update item by key.
75- pub fn insert < ' a > ( & mut self , key : impl Into < Key < ' a > > , value : Val ) {
66+ pub fn insert < ' a > ( & mut self , key : impl Into < InsertKey < ' a > > , value : Val ) {
7667 let key = key. into ( ) ;
7768 let value = EBox :: new ( value) ;
7869 unsafe {
7970 match key {
80- Key :: Index ( i) => {
71+ InsertKey :: NextIndex => {
72+ phper_zend_hash_next_index_insert (
73+ & mut self . inner ,
74+ EBox :: into_raw ( value) . cast ( ) ,
75+ ) ;
76+ }
77+ InsertKey :: Index ( i) => {
8178 phper_zend_hash_index_update ( & mut self . inner , i, EBox :: into_raw ( value) . cast ( ) ) ;
8279 }
83- Key :: Str ( s) => {
80+ InsertKey :: Str ( s) => {
8481 phper_zend_hash_str_update (
8582 & mut self . inner ,
8683 s. as_ptr ( ) . cast ( ) ,
@@ -118,16 +115,22 @@ impl Array {
118115 unsafe {
119116 match key {
120117 Key :: Index ( i) => phper_zend_hash_index_exists ( & self . inner , i) ,
121- Key :: Str ( s) => phper_zend_hash_str_exists (
122- & self . inner ,
123- s. as_ref ( ) . as_ptr ( ) . cast ( ) ,
124- s. as_ref ( ) . len ( ) ,
125- ) ,
118+ Key :: Str ( s) => phper_zend_hash_str_exists ( & self . inner , s. as_ptr ( ) . cast ( ) , s. len ( ) ) ,
126119 }
127120 }
128121 }
129122
130- pub fn clone ( & self ) -> EBox < Self > {
123+ pub fn remove < ' a > ( & mut self , key : impl Into < Key < ' a > > ) -> bool {
124+ let key = key. into ( ) ;
125+ unsafe {
126+ ( match key {
127+ Key :: Index ( i) => zend_hash_index_del ( & mut self . inner , i) ,
128+ Key :: Str ( s) => zend_hash_str_del ( & mut self . inner , s. as_ptr ( ) . cast ( ) , s. len ( ) ) ,
129+ } ) == ZEND_RESULT_CODE_SUCCESS
130+ }
131+ }
132+
133+ pub fn clone_arr ( & self ) -> EBox < Self > {
131134 let mut other = Self :: new ( ) ;
132135 unsafe {
133136 zend_hash_copy ( other. as_mut_ptr ( ) , self . as_ptr ( ) as * mut _ , None ) ;
@@ -146,11 +149,9 @@ impl Array {
146149impl EAllocatable for Array {
147150 fn free ( ptr : * mut Self ) {
148151 unsafe {
152+ ( * ptr) . inner . gc . refcount -= 1 ;
149153 if ( * ptr) . inner . gc . refcount == 0 {
150- zend_hash_destroy ( ptr. cast ( ) ) ;
151- _efree ( ptr. cast ( ) ) ;
152- } else {
153- ( * ptr) . inner . gc . refcount -= 1 ;
154+ zend_array_destroy ( ptr. cast ( ) ) ;
154155 }
155156 }
156157 }
@@ -162,6 +163,7 @@ impl Drop for Array {
162163 }
163164}
164165
166+ /// Iter created by [Array::iter].
165167pub struct Iter < ' a > {
166168 index : isize ,
167169 array : & ' a Array ,
@@ -182,9 +184,9 @@ impl<'a> Iterator for Iter<'a> {
182184 let key = if ( * bucket) . key . is_null ( ) {
183185 Key :: Index ( ( * bucket) . h )
184186 } else {
185- let s = ZendString :: from_ptr ( ( * bucket) . key ) ;
186- let s = s. to_string ( ) . unwrap ( ) ;
187- Key :: Str ( Cow :: Owned ( s ) )
187+ let s = ZendString :: from_ptr ( ( * bucket) . key ) . unwrap ( ) ;
188+ let s = s. as_str ( ) . unwrap ( ) ;
189+ Key :: Str ( s )
188190 } ;
189191
190192 let val = & mut ( * bucket) . val ;
0 commit comments