@@ -17,7 +17,7 @@ use std::{
17
17
borrow:: Borrow ,
18
18
convert:: TryInto ,
19
19
marker:: PhantomData ,
20
- mem:: forget,
20
+ mem:: { forget, ManuallyDrop } ,
21
21
ops:: { Deref , DerefMut } ,
22
22
} ;
23
23
@@ -40,6 +40,17 @@ pub enum InsertKey<'a> {
40
40
ZStr ( & ' a ZStr ) ,
41
41
}
42
42
43
+ impl < ' a > From < Key < ' a > > for InsertKey < ' a > {
44
+ fn from ( k : Key < ' a > ) -> Self {
45
+ match k {
46
+ Key :: Index ( i) => InsertKey :: Index ( i) ,
47
+ Key :: Str ( s) => InsertKey :: Str ( s) ,
48
+ Key :: Bytes ( b) => InsertKey :: Bytes ( b) ,
49
+ Key :: ZStr ( s) => InsertKey :: ZStr ( s) ,
50
+ }
51
+ }
52
+ }
53
+
43
54
#[ repr( transparent) ]
44
55
pub struct ZArr {
45
56
inner : zend_array ,
@@ -115,16 +126,16 @@ impl ZArr {
115
126
}
116
127
117
128
// Get item by key.
118
- pub fn get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & ZVal > {
129
+ pub fn get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & ' a ZVal > {
119
130
self . inner_get ( key) . map ( |v| & * v)
120
131
}
121
132
122
133
// Get item by key.
123
- pub fn get_mut < ' a > ( & mut self , key : impl Into < Key < ' a > > ) -> Option < & mut ZVal > {
134
+ pub fn get_mut < ' a > ( & mut self , key : impl Into < Key < ' a > > ) -> Option < & ' a mut ZVal > {
124
135
self . inner_get ( key)
125
136
}
126
137
127
- fn inner_get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & mut ZVal > {
138
+ fn inner_get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & ' a mut ZVal > {
128
139
let key = key. into ( ) ;
129
140
unsafe {
130
141
let value = match key {
@@ -199,17 +210,19 @@ impl ZArr {
199
210
}
200
211
}
201
212
202
- pub fn clear ( & mut self ) { }
203
-
204
213
pub fn iter ( & self ) -> Iter < ' _ > {
205
214
Iter {
206
215
index : 0 ,
207
216
array : self ,
208
217
}
209
218
}
210
219
211
- pub fn entry < ' a > ( & mut self , _key : impl Into < Key < ' a > > ) -> Entry < ' a > {
212
- todo ! ( )
220
+ pub fn entry < ' a > ( & ' a mut self , key : impl Into < Key < ' a > > ) -> Entry < ' a > {
221
+ let key = key. into ( ) ;
222
+ match self . get_mut ( key. clone ( ) ) {
223
+ Some ( val) => Entry :: Occupied ( val) ,
224
+ None => Entry :: Vacant { arr : self , key } ,
225
+ }
213
226
}
214
227
}
215
228
@@ -229,7 +242,12 @@ impl ToRefOwned for ZArr {
229
242
type Owned = ZArray ;
230
243
231
244
fn to_ref_owned ( & mut self ) -> Self :: Owned {
232
- todo ! ( )
245
+ let mut val = ManuallyDrop :: new ( ZVal :: default ( ) ) ;
246
+ unsafe {
247
+ phper_zval_arr ( val. as_mut_ptr ( ) , self . as_mut_ptr ( ) ) ;
248
+ phper_z_addref_p ( val. as_mut_ptr ( ) ) ;
249
+ ZArray :: from_raw ( val. as_mut_z_arr ( ) . unwrap ( ) . as_mut_ptr ( ) )
250
+ }
233
251
}
234
252
}
235
253
@@ -358,14 +376,20 @@ impl<'a> Iterator for Iter<'a> {
358
376
}
359
377
}
360
378
361
- // TODO Implement it.
362
379
pub enum Entry < ' a > {
363
- Occupied ( PhantomData < & ' a ( ) > ) ,
364
- Vacant ( PhantomData < & ' a ( ) > ) ,
380
+ Occupied ( & ' a mut ZVal ) ,
381
+ Vacant { arr : & ' a mut ZArr , key : Key < ' a > } ,
365
382
}
366
383
367
384
impl < ' a > Entry < ' a > {
368
- pub fn or_insert ( & mut self , _val : ZVal ) -> & ' a mut ZVal {
369
- todo ! ( )
385
+ pub fn or_insert ( self , val : ZVal ) -> & ' a mut ZVal {
386
+ match self {
387
+ Entry :: Occupied ( val) => val,
388
+ Entry :: Vacant { arr, key } => {
389
+ let insert_key: InsertKey < ' _ > = key. clone ( ) . into ( ) ;
390
+ arr. insert ( insert_key, val) ;
391
+ arr. get_mut ( key) . unwrap ( )
392
+ }
393
+ }
370
394
}
371
395
}
0 commit comments