1010
1111//! Apis relate to [crate::sys::zend_array].
1212
13- use crate :: { strings:: ZStr , sys:: * , values:: ZVal } ;
13+ use crate :: { alloc :: ToRefOwned , strings:: ZStr , sys:: * , values:: ZVal } ;
1414use derive_more:: From ;
15- use phper_alloc:: ToRefOwned ;
1615use std:: {
1716 borrow:: Borrow ,
1817 convert:: TryInto ,
@@ -87,42 +86,46 @@ impl ZArr {
8786 }
8887
8988 /// Add or update item by key.
90- pub fn insert < ' a > ( & mut self , key : impl Into < InsertKey < ' a > > , value : ZVal ) {
89+ pub fn insert < ' a > ( & mut self , key : impl Into < InsertKey < ' a > > , mut value : ZVal ) {
9190 let key = key. into ( ) ;
91+ let val = value. as_mut_ptr ( ) ;
92+
9293 unsafe {
9394 match key {
9495 InsertKey :: NextIndex => {
95- phper_zend_hash_next_index_insert ( self . as_mut_ptr ( ) , value . into_raw ( ) ) ;
96+ phper_zend_hash_next_index_insert ( self . as_mut_ptr ( ) , val ) ;
9697 }
9798 InsertKey :: Index ( i) => {
98- phper_zend_hash_index_update ( self . as_mut_ptr ( ) , i, value . into_raw ( ) ) ;
99+ phper_zend_hash_index_update ( self . as_mut_ptr ( ) , i, val ) ;
99100 }
100101 InsertKey :: Str ( s) => {
101- phper_zend_hash_str_update (
102+ phper_zend_symtable_str_update (
102103 self . as_mut_ptr ( ) ,
103104 s. as_ptr ( ) . cast ( ) ,
104105 s. len ( ) . try_into ( ) . unwrap ( ) ,
105- value . into_raw ( ) ,
106+ val ,
106107 ) ;
107108 }
108109 InsertKey :: Bytes ( b) => {
109- phper_zend_hash_str_update (
110+ phper_zend_symtable_str_update (
110111 self . as_mut_ptr ( ) ,
111112 b. as_ptr ( ) . cast ( ) ,
112113 b. len ( ) . try_into ( ) . unwrap ( ) ,
113- value . into_raw ( ) ,
114+ val ,
114115 ) ;
115116 }
116117 InsertKey :: ZStr ( s) => {
117- phper_zend_hash_str_update (
118+ phper_zend_symtable_str_update (
118119 self . as_mut_ptr ( ) ,
119120 s. as_c_str_ptr ( ) . cast ( ) ,
120121 s. len ( ) . try_into ( ) . unwrap ( ) ,
121- value . into_raw ( ) ,
122+ val ,
122123 ) ;
123124 }
124125 }
125126 }
127+
128+ forget ( value) ;
126129 }
127130
128131 // Get item by key.
@@ -137,21 +140,22 @@ impl ZArr {
137140
138141 fn inner_get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & ' a mut ZVal > {
139142 let key = key. into ( ) ;
143+ let ptr = self . as_ptr ( ) as * mut _ ;
140144 unsafe {
141145 let value = match key {
142- Key :: Index ( i) => zend_hash_index_find ( self . as_ptr ( ) , i) ,
143- Key :: Str ( s) => zend_hash_str_find (
144- self . as_ptr ( ) ,
146+ Key :: Index ( i) => phper_zend_hash_index_find ( ptr , i) ,
147+ Key :: Str ( s) => phper_zend_symtable_str_find (
148+ ptr ,
145149 s. as_ptr ( ) . cast ( ) ,
146150 s. len ( ) . try_into ( ) . unwrap ( ) ,
147151 ) ,
148- Key :: Bytes ( b) => zend_hash_str_find (
149- self . as_ptr ( ) ,
152+ Key :: Bytes ( b) => phper_zend_symtable_str_find (
153+ ptr ,
150154 b. as_ptr ( ) . cast ( ) ,
151155 b. len ( ) . try_into ( ) . unwrap ( ) ,
152156 ) ,
153157 Key :: ZStr ( s) => {
154- zend_hash_str_find ( self . as_ptr ( ) , s. as_c_str_ptr ( ) , s. len ( ) . try_into ( ) . unwrap ( ) )
158+ phper_zend_symtable_str_find ( ptr , s. as_c_str_ptr ( ) , s. len ( ) . try_into ( ) . unwrap ( ) )
155159 }
156160 } ;
157161 if value. is_null ( ) {
@@ -164,21 +168,22 @@ impl ZArr {
164168
165169 pub fn exists < ' a > ( & self , key : impl Into < Key < ' a > > ) -> bool {
166170 let key = key. into ( ) ;
171+ let ptr = self . as_ptr ( ) as * mut _ ;
167172 unsafe {
168173 match key {
169- Key :: Index ( i) => phper_zend_hash_index_exists ( & self . inner , i) ,
170- Key :: Str ( s) => phper_zend_hash_str_exists (
171- & self . inner ,
174+ Key :: Index ( i) => phper_zend_hash_index_exists ( ptr , i) ,
175+ Key :: Str ( s) => phper_zend_symtable_str_exists (
176+ ptr ,
172177 s. as_ptr ( ) . cast ( ) ,
173178 s. len ( ) . try_into ( ) . unwrap ( ) ,
174179 ) ,
175- Key :: Bytes ( b) => phper_zend_hash_str_exists (
176- & self . inner ,
180+ Key :: Bytes ( b) => phper_zend_symtable_str_exists (
181+ ptr ,
177182 b. as_ptr ( ) . cast ( ) ,
178183 b. len ( ) . try_into ( ) . unwrap ( ) ,
179184 ) ,
180- Key :: ZStr ( s) => phper_zend_hash_str_exists (
181- & self . inner ,
185+ Key :: ZStr ( s) => phper_zend_symtable_str_exists (
186+ ptr ,
182187 s. to_bytes ( ) . as_ptr ( ) . cast ( ) ,
183188 s. len ( ) . try_into ( ) . unwrap ( ) ,
184189 ) ,
@@ -189,24 +194,24 @@ impl ZArr {
189194 pub fn remove < ' a > ( & mut self , key : impl Into < Key < ' a > > ) -> bool {
190195 let key = key. into ( ) ;
191196 unsafe {
192- ( match key {
193- Key :: Index ( i) => zend_hash_index_del ( & mut self . inner , i) ,
194- Key :: Str ( s) => zend_hash_str_del (
197+ match key {
198+ Key :: Index ( i) => phper_zend_hash_index_del ( & mut self . inner , i) ,
199+ Key :: Str ( s) => phper_zend_symtable_str_del (
195200 & mut self . inner ,
196201 s. as_ptr ( ) . cast ( ) ,
197202 s. len ( ) . try_into ( ) . unwrap ( ) ,
198203 ) ,
199- Key :: Bytes ( b) => zend_hash_str_del (
204+ Key :: Bytes ( b) => phper_zend_symtable_str_del (
200205 & mut self . inner ,
201206 b. as_ptr ( ) . cast ( ) ,
202207 b. len ( ) . try_into ( ) . unwrap ( ) ,
203208 ) ,
204- Key :: ZStr ( s) => zend_hash_str_del (
209+ Key :: ZStr ( s) => phper_zend_symtable_str_del (
205210 & mut self . inner ,
206211 s. as_c_str_ptr ( ) . cast ( ) ,
207212 s. len ( ) . try_into ( ) . unwrap ( ) ,
208213 ) ,
209- } ) == ZEND_RESULT_CODE_SUCCESS
214+ }
210215 }
211216 }
212217
@@ -280,10 +285,8 @@ impl ZArray {
280285 }
281286
282287 #[ inline]
283- pub fn into_raw ( mut self ) -> * mut zend_array {
284- let ptr = self . as_mut_ptr ( ) ;
285- forget ( self ) ;
286- ptr
288+ pub fn into_raw ( self ) -> * mut zend_array {
289+ ManuallyDrop :: new ( self ) . as_mut_ptr ( )
287290 }
288291}
289292
0 commit comments