@@ -7,6 +7,7 @@ use crate::{
77 utils:: ensure_end_with_zero,
88 TypeError ,
99} ;
10+ use indexmap:: map:: IndexMap ;
1011use std:: {
1112 collections:: HashMap , mem:: zeroed, os:: raw:: c_char, slice:: from_raw_parts, str, str:: Utf8Error ,
1213 sync:: atomic:: Ordering ,
@@ -227,6 +228,10 @@ impl Val {
227228 }
228229}
229230
231+ /// The trait for setting the value of [Val], mainly as the return value of
232+ /// [crate::functions::Function] and [crate::functions::Method], and initializer of [Val].
233+ ///
234+ /// TODO Fix the possibility of leak memory.
230235pub trait SetVal {
231236 fn set_val ( self , val : & mut Val ) ;
232237}
@@ -304,6 +309,8 @@ impl<T: SetVal> SetVal for Vec<T> {
304309 }
305310}
306311
312+ /// Setting the val to an array, Because of the feature of [std::collections::HashMap], the item
313+ /// order of array is not guarantee.
307314impl < K : AsRef < str > , V : SetVal > SetVal for HashMap < K , V > {
308315 fn set_val ( self , val : & mut Val ) {
309316 unsafe {
@@ -321,6 +328,24 @@ impl<K: AsRef<str>, V: SetVal> SetVal for HashMap<K, V> {
321328 }
322329}
323330
331+ /// Setting the val to an array, which preserves item order.
332+ impl < K : AsRef < str > , V : SetVal > SetVal for IndexMap < K , V > {
333+ fn set_val ( self , val : & mut Val ) {
334+ unsafe {
335+ phper_array_init ( val. as_mut_ptr ( ) ) ;
336+ for ( k, v) in self {
337+ let k = k. as_ref ( ) ;
338+ zend_hash_str_update (
339+ ( * val. as_mut_ptr ( ) ) . value . arr ,
340+ k. as_ptr ( ) . cast ( ) ,
341+ k. len ( ) ,
342+ Val :: new ( v) . as_mut_ptr ( ) ,
343+ ) ;
344+ }
345+ }
346+ }
347+ }
348+
324349impl SetVal for Array {
325350 fn set_val ( mut self , val : & mut Val ) {
326351 unsafe {
@@ -330,12 +355,6 @@ impl SetVal for Array {
330355 }
331356}
332357
333- extern "C" fn array_copy_ctor ( zv : * mut zval ) {
334- let val = unsafe { Val :: from_mut ( zv) } ;
335- let other = unsafe { Val :: from_mut ( zv) } ;
336- val. deep_copy ( other) . expect ( "deep copy failed" ) ;
337- }
338-
339358impl < T : SetVal > SetVal for Option < T > {
340359 fn set_val ( self , val : & mut Val ) {
341360 match self {
0 commit comments