@@ -576,13 +576,13 @@ pub struct Iter<'a> {
576
576
}
577
577
578
578
#[ derive( Debug , PartialEq ) ]
579
- pub enum ArrayKey < ' a > {
579
+ pub enum ArrayKey {
580
580
Long ( i64 ) ,
581
- String ( & ' a str ) ,
581
+ String ( String ) ,
582
582
}
583
583
584
584
/// Represent the key of a PHP array, which can be either a long or a string.
585
- impl < ' a > ArrayKey < ' a > {
585
+ impl ArrayKey {
586
586
/// Check if the key is an integer.
587
587
///
588
588
/// # Returns
@@ -596,7 +596,7 @@ impl<'a> ArrayKey<'a> {
596
596
}
597
597
}
598
598
599
- impl < ' a > Display for ArrayKey < ' a > {
599
+ impl Display for ArrayKey {
600
600
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
601
601
match self {
602
602
ArrayKey :: Long ( key) => write ! ( f, "{}" , key) ,
@@ -605,17 +605,17 @@ impl<'a> Display for ArrayKey<'a> {
605
605
}
606
606
}
607
607
608
- impl < ' a > FromZval < ' _ > for ArrayKey < ' a > {
608
+ impl < ' a > FromZval < ' a > for ArrayKey {
609
609
const TYPE : DataType = DataType :: String ;
610
610
611
- fn from_zval ( zval : & Zval ) -> Option < Self > {
611
+ fn from_zval ( zval : & ' a Zval ) -> Option < Self > {
612
612
if let Some ( key) = zval. long ( ) {
613
613
return Some ( ArrayKey :: Long ( key) ) ;
614
614
}
615
- if let Some ( key) = zval. str ( ) {
615
+ if let Some ( key) = zval. string ( ) {
616
616
return Some ( ArrayKey :: String ( key) ) ;
617
617
}
618
- return None ;
618
+ None
619
619
}
620
620
}
621
621
@@ -635,7 +635,7 @@ impl<'a> Iter<'a> {
635
635
}
636
636
637
637
impl < ' a > IntoIterator for & ' a ZendHashTable {
638
- type Item = ( ArrayKey < ' a > , & ' a Zval ) ;
638
+ type Item = ( ArrayKey , & ' a Zval ) ;
639
639
type IntoIter = Iter < ' a > ;
640
640
641
641
/// Returns an iterator over the key(s) and value contained inside the
@@ -662,9 +662,28 @@ impl<'a> IntoIterator for &'a ZendHashTable {
662
662
}
663
663
664
664
impl < ' a > Iterator for Iter < ' a > {
665
- type Item = ( ArrayKey < ' a > , & ' a Zval ) ;
665
+ type Item = ( ArrayKey , & ' a Zval ) ;
666
666
667
667
fn next ( & mut self ) -> Option < Self :: Item > {
668
+ self . next_zval ( ) . map ( |( k, v) | ( ArrayKey :: from_zval ( & k) . expect ( "Invalid array key!" ) , v) )
669
+ }
670
+
671
+ fn count ( self ) -> usize
672
+ where
673
+ Self : Sized ,
674
+ {
675
+ self . ht . len ( )
676
+ }
677
+ }
678
+
679
+ impl < ' a > ExactSizeIterator for Iter < ' a > {
680
+ fn len ( & self ) -> usize {
681
+ self . ht . len ( )
682
+ }
683
+ }
684
+
685
+ impl < ' a > DoubleEndedIterator for Iter < ' a > {
686
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
668
687
let key_type = unsafe {
669
688
zend_hash_get_current_key_type_ex (
670
689
self . ht as * const ZendHashTable as * mut ZendHashTable ,
@@ -677,6 +696,7 @@ impl<'a> Iterator for Iter<'a> {
677
696
}
678
697
679
698
let key = Zval :: new ( ) ;
699
+
680
700
unsafe {
681
701
zend_hash_get_current_key_zval_ex (
682
702
self . ht as * const ZendHashTable as * mut ZendHashTable ,
@@ -697,32 +717,19 @@ impl<'a> Iterator for Iter<'a> {
697
717
} ;
698
718
699
719
unsafe {
700
- zend_hash_move_forward_ex (
720
+ zend_hash_move_backwards_ex (
701
721
self . ht as * const ZendHashTable as * mut ZendHashTable ,
702
722
& mut self . pos as * mut HashPosition ,
703
723
)
704
724
} ;
705
- self . current_num + = 1 ;
725
+ self . current_num - = 1 ;
706
726
707
727
Some ( ( key, value) )
708
728
}
709
-
710
- fn count ( self ) -> usize
711
- where
712
- Self : Sized ,
713
- {
714
- self . ht . len ( )
715
- }
716
- }
717
-
718
- impl < ' a > ExactSizeIterator for Iter < ' a > {
719
- fn len ( & self ) -> usize {
720
- self . ht . len ( )
721
- }
722
729
}
723
730
724
- impl < ' a > DoubleEndedIterator for Iter < ' a > {
725
- fn next_back ( & mut self ) -> Option < Self :: Item > {
731
+ impl < ' a , ' b > Iter < ' a > {
732
+ pub fn next_zval ( & ' b mut self ) -> Option < ( Zval , & ' a Zval ) > {
726
733
let key_type = unsafe {
727
734
zend_hash_get_current_key_type_ex (
728
735
self . ht as * const ZendHashTable as * mut ZendHashTable ,
@@ -734,7 +741,8 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
734
741
return None ;
735
742
}
736
743
737
- let key = Zval :: new ( ) ;
744
+ let mut key = Zval :: new ( ) ;
745
+
738
746
unsafe {
739
747
zend_hash_get_current_key_zval_ex (
740
748
self . ht as * const ZendHashTable as * mut ZendHashTable ,
@@ -749,20 +757,19 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
749
757
)
750
758
} ;
751
759
752
- let r = match ArrayKey :: from_zval ( & key) {
753
- Some ( key) => ( key, value) ,
754
- None => ( ArrayKey :: Long ( self . current_num ) , value) ,
755
- } ;
760
+ if !key. is_long ( ) && !key. is_string ( ) {
761
+ key. set_long ( self . current_num )
762
+ }
756
763
757
764
unsafe {
758
- zend_hash_move_backwards_ex (
765
+ zend_hash_move_forward_ex (
759
766
self . ht as * const ZendHashTable as * mut ZendHashTable ,
760
767
& mut self . pos as * mut HashPosition ,
761
768
)
762
769
} ;
763
- self . current_num - = 1 ;
770
+ self . current_num + = 1 ;
764
771
765
- Some ( r )
772
+ Some ( ( key , value ) )
766
773
}
767
774
}
768
775
0 commit comments