@@ -2738,11 +2738,7 @@ where
2738
2738
where
2739
2739
V : Visitor < ' de > ,
2740
2740
{
2741
- visitor. visit_map ( FlatInternallyTaggedAccess {
2742
- iter : self . 0 . iter_mut ( ) ,
2743
- pending : None ,
2744
- _marker : PhantomData ,
2745
- } )
2741
+ self . deserialize_map ( visitor)
2746
2742
}
2747
2743
2748
2744
fn deserialize_enum < V > (
@@ -2755,16 +2751,7 @@ where
2755
2751
V : Visitor < ' de > ,
2756
2752
{
2757
2753
for item in self . 0 . iter_mut ( ) {
2758
- // items in the vector are nulled out when used. So we can only use
2759
- // an item if it's still filled in and if the field is one we care
2760
- // about.
2761
- let use_item = match * item {
2762
- None => false ,
2763
- Some ( ( ref c, _) ) => c. as_str ( ) . map_or ( false , |x| variants. contains ( & x) ) ,
2764
- } ;
2765
-
2766
- if use_item {
2767
- let ( key, value) = item. take ( ) . unwrap ( ) ;
2754
+ if let Some ( ( key, value) ) = use_item ( item, variants) {
2768
2755
return visitor. visit_enum ( EnumDeserializer :: new ( key, Some ( value) ) ) ;
2769
2756
}
2770
2757
}
@@ -2779,7 +2766,11 @@ where
2779
2766
where
2780
2767
V : Visitor < ' de > ,
2781
2768
{
2782
- visitor. visit_map ( FlatMapAccess :: new ( self . 0 . iter ( ) ) )
2769
+ visitor. visit_map ( FlatMapAccess {
2770
+ iter : self . 0 . iter ( ) ,
2771
+ pending_content : None ,
2772
+ _marker : PhantomData ,
2773
+ } )
2783
2774
}
2784
2775
2785
2776
fn deserialize_struct < V > (
@@ -2791,7 +2782,12 @@ where
2791
2782
where
2792
2783
V : Visitor < ' de > ,
2793
2784
{
2794
- visitor. visit_map ( FlatStructAccess :: new ( self . 0 . iter_mut ( ) , fields) )
2785
+ visitor. visit_map ( FlatStructAccess {
2786
+ iter : self . 0 . iter_mut ( ) ,
2787
+ pending_content : None ,
2788
+ fields : fields,
2789
+ _marker : PhantomData ,
2790
+ } )
2795
2791
}
2796
2792
2797
2793
fn deserialize_newtype_struct < V > ( self , _name : & str , visitor : V ) -> Result < V :: Value , Self :: Error >
@@ -2845,25 +2841,12 @@ where
2845
2841
}
2846
2842
2847
2843
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2848
- pub struct FlatMapAccess < ' a , ' de : ' a , E > {
2844
+ struct FlatMapAccess < ' a , ' de : ' a , E > {
2849
2845
iter : slice:: Iter < ' a , Option < ( Content < ' de > , Content < ' de > ) > > ,
2850
2846
pending_content : Option < & ' a Content < ' de > > ,
2851
2847
_marker : PhantomData < E > ,
2852
2848
}
2853
2849
2854
- #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2855
- impl < ' a , ' de , E > FlatMapAccess < ' a , ' de , E > {
2856
- fn new (
2857
- iter : slice:: Iter < ' a , Option < ( Content < ' de > , Content < ' de > ) > > ,
2858
- ) -> FlatMapAccess < ' a , ' de , E > {
2859
- FlatMapAccess {
2860
- iter : iter,
2861
- pending_content : None ,
2862
- _marker : PhantomData ,
2863
- }
2864
- }
2865
- }
2866
-
2867
2850
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2868
2851
impl < ' a , ' de , E > MapAccess < ' de > for FlatMapAccess < ' a , ' de , E >
2869
2852
where
@@ -2878,6 +2861,10 @@ where
2878
2861
for item in & mut self . iter {
2879
2862
// Items in the vector are nulled out when used by a struct.
2880
2863
if let Some ( ( ref key, ref content) ) = * item {
2864
+ // Do not take(), instead borrow this entry. The internally tagged
2865
+ // enum does its own buffering so we can't tell whether this entry
2866
+ // is going to be consumed. Borrowing here leaves the entry
2867
+ // available for later flattened fields.
2881
2868
self . pending_content = Some ( content) ;
2882
2869
return seed. deserialize ( ContentRefDeserializer :: new ( key) ) . map ( Some ) ;
2883
2870
}
@@ -2897,28 +2884,13 @@ where
2897
2884
}
2898
2885
2899
2886
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2900
- pub struct FlatStructAccess < ' a , ' de : ' a , E > {
2887
+ struct FlatStructAccess < ' a , ' de : ' a , E > {
2901
2888
iter : slice:: IterMut < ' a , Option < ( Content < ' de > , Content < ' de > ) > > ,
2902
2889
pending_content : Option < Content < ' de > > ,
2903
2890
fields : & ' static [ & ' static str ] ,
2904
2891
_marker : PhantomData < E > ,
2905
2892
}
2906
2893
2907
- #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2908
- impl < ' a , ' de , E > FlatStructAccess < ' a , ' de , E > {
2909
- fn new (
2910
- iter : slice:: IterMut < ' a , Option < ( Content < ' de > , Content < ' de > ) > > ,
2911
- fields : & ' static [ & ' static str ] ,
2912
- ) -> FlatStructAccess < ' a , ' de , E > {
2913
- FlatStructAccess {
2914
- iter : iter,
2915
- pending_content : None ,
2916
- fields : fields,
2917
- _marker : PhantomData ,
2918
- }
2919
- }
2920
- }
2921
-
2922
2894
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2923
2895
impl < ' a , ' de , E > MapAccess < ' de > for FlatStructAccess < ' a , ' de , E >
2924
2896
where
@@ -2931,16 +2903,7 @@ where
2931
2903
T : DeserializeSeed < ' de > ,
2932
2904
{
2933
2905
while let Some ( item) = self . iter . next ( ) {
2934
- // items in the vector are nulled out when used. So we can only use
2935
- // an item if it's still filled in and if the field is one we care
2936
- // about. In case we do not know which fields we want, we take them all.
2937
- let use_item = match * item {
2938
- None => false ,
2939
- Some ( ( ref c, _) ) => c. as_str ( ) . map_or ( false , |key| self . fields . contains ( & key) ) ,
2940
- } ;
2941
-
2942
- if use_item {
2943
- let ( key, content) = item. take ( ) . unwrap ( ) ;
2906
+ if let Some ( ( key, content) ) = use_item ( item, self . fields ) {
2944
2907
self . pending_content = Some ( content) ;
2945
2908
return seed. deserialize ( ContentDeserializer :: new ( key) ) . map ( Some ) ;
2946
2909
}
@@ -2959,44 +2922,25 @@ where
2959
2922
}
2960
2923
}
2961
2924
2925
+ /// Checks if first element of the specified pair matches one of the key from
2926
+ /// `keys` parameter and if this is true, takes it from the option and returns.
2927
+ /// Otherwise, or if `item` already empty, returns `None`.
2962
2928
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2963
- pub struct FlatInternallyTaggedAccess < ' a , ' de : ' a , E > {
2964
- iter : slice:: IterMut < ' a , Option < ( Content < ' de > , Content < ' de > ) > > ,
2965
- pending : Option < & ' a Content < ' de > > ,
2966
- _marker : PhantomData < E > ,
2967
- }
2968
-
2969
- #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2970
- impl < ' a , ' de , E > MapAccess < ' de > for FlatInternallyTaggedAccess < ' a , ' de , E >
2971
- where
2972
- E : Error ,
2973
- {
2974
- type Error = E ;
2975
-
2976
- fn next_key_seed < T > ( & mut self , seed : T ) -> Result < Option < T :: Value > , Self :: Error >
2977
- where
2978
- T : DeserializeSeed < ' de > ,
2979
- {
2980
- for item in & mut self . iter {
2981
- if let Some ( ( ref key, ref content) ) = * item {
2982
- // Do not take(), instead borrow this entry. The internally tagged
2983
- // enum does its own buffering so we can't tell whether this entry
2984
- // is going to be consumed. Borrowing here leaves the entry
2985
- // available for later flattened fields.
2986
- self . pending = Some ( content) ;
2987
- return seed. deserialize ( ContentRefDeserializer :: new ( key) ) . map ( Some ) ;
2988
- }
2989
- }
2990
- Ok ( None )
2991
- }
2929
+ fn use_item < ' de > (
2930
+ item : & mut Option < ( Content < ' de > , Content < ' de > ) > ,
2931
+ keys : & [ & str ] ,
2932
+ ) -> Option < ( Content < ' de > , Content < ' de > ) > {
2933
+ // items in the vector are nulled out when used. So we can only use
2934
+ // an item if it's still filled in and if the field is one we care
2935
+ // about.
2936
+ let use_item = match * item {
2937
+ None => false ,
2938
+ Some ( ( ref c, _) ) => c. as_str ( ) . map_or ( false , |key| keys. contains ( & key) ) ,
2939
+ } ;
2992
2940
2993
- fn next_value_seed < T > ( & mut self , seed : T ) -> Result < T :: Value , Self :: Error >
2994
- where
2995
- T : DeserializeSeed < ' de > ,
2996
- {
2997
- match self . pending . take ( ) {
2998
- Some ( value) => seed. deserialize ( ContentRefDeserializer :: new ( value) ) ,
2999
- None => panic ! ( "value is missing" ) ,
3000
- }
2941
+ if use_item {
2942
+ item. take ( )
2943
+ } else {
2944
+ None
3001
2945
}
3002
2946
}
0 commit comments