@@ -876,6 +876,12 @@ impl<'a> OccupiedEntry<'a> {
876
876
877
877
/// Takes the value of the entry out of the map, and returns it.
878
878
///
879
+ /// If serde_json's "preserve_order" is enabled, `.remove()` is
880
+ /// equivalent to [`.swap_remove()`][Self::swap_remove], replacing this
881
+ /// entry's position with the last element. If you need to preserve the
882
+ /// relative order of the keys in the map, use
883
+ /// [`.shift_remove()`][Self::shift_remove] instead.
884
+ ///
879
885
/// # Examples
880
886
///
881
887
/// ```
@@ -896,10 +902,101 @@ impl<'a> OccupiedEntry<'a> {
896
902
#[ inline]
897
903
pub fn remove ( self ) -> Value {
898
904
#[ cfg( feature = "preserve_order" ) ]
899
- return self . occupied . swap_remove ( ) ;
905
+ return self . swap_remove ( ) ;
900
906
#[ cfg( not( feature = "preserve_order" ) ) ]
901
907
return self . occupied . remove ( ) ;
902
908
}
909
+
910
+ /// Takes the value of the entry out of the map, and returns it.
911
+ ///
912
+ /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the
913
+ /// last element of the map and popping it off. This perturbs the position
914
+ /// of what used to be the last element!
915
+ ///
916
+ /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove
917
+ #[ cfg( feature = "preserve_order" ) ]
918
+ #[ cfg_attr( docsrs, doc( cfg( feature = "preserve_order" ) ) ) ]
919
+ #[ inline]
920
+ pub fn swap_remove ( self ) -> Value {
921
+ self . occupied . swap_remove ( )
922
+ }
923
+
924
+ /// Takes the value of the entry out of the map, and returns it.
925
+ ///
926
+ /// Like [`Vec::remove`], the entry is removed by shifting all of the
927
+ /// elements that follow it, preserving their relative order. This perturbs
928
+ /// the index of all of those elements!
929
+ ///
930
+ /// [`Vec::remove`]: std::vec::Vec::remove
931
+ #[ cfg( feature = "preserve_order" ) ]
932
+ #[ cfg_attr( docsrs, doc( cfg( feature = "preserve_order" ) ) ) ]
933
+ #[ inline]
934
+ pub fn shift_remove ( self ) -> Value {
935
+ self . occupied . shift_remove ( )
936
+ }
937
+
938
+ /// Removes the entry from the map, returning the stored key and value.
939
+ ///
940
+ /// If serde_json's "preserve_order" is enabled, `.remove_entry()` is
941
+ /// equivalent to [`.swap_remove_entry()`][Self::swap_remove_entry],
942
+ /// replacing this entry's position with the last element. If you need to
943
+ /// preserve the relative order of the keys in the map, use
944
+ /// [`.shift_remove_entry()`][Self::shift_remove_entry] instead.
945
+ ///
946
+ /// # Examples
947
+ ///
948
+ /// ```
949
+ /// # use serde_json::json;
950
+ /// #
951
+ /// use serde_json::map::Entry;
952
+ ///
953
+ /// let mut map = serde_json::Map::new();
954
+ /// map.insert("serde".to_owned(), json!(12));
955
+ ///
956
+ /// match map.entry("serde") {
957
+ /// Entry::Occupied(occupied) => {
958
+ /// let (key, value) = occupied.remove_entry();
959
+ /// assert_eq!(key, "serde");
960
+ /// assert_eq!(value, 12);
961
+ /// }
962
+ /// Entry::Vacant(_) => unimplemented!(),
963
+ /// }
964
+ /// ```
965
+ #[ inline]
966
+ pub fn remove_entry ( self ) -> ( String , Value ) {
967
+ #[ cfg( feature = "preserve_order" ) ]
968
+ return self . swap_remove_entry ( ) ;
969
+ #[ cfg( not( feature = "preserve_order" ) ) ]
970
+ return self . occupied . remove_entry ( ) ;
971
+ }
972
+
973
+ /// Removes the entry from the map, returning the stored key and value.
974
+ ///
975
+ /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the
976
+ /// last element of the map and popping it off. This perturbs the position
977
+ /// of what used to be the last element!
978
+ ///
979
+ /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove
980
+ #[ cfg( feature = "preserve_order" ) ]
981
+ #[ cfg_attr( docsrs, doc( cfg( feature = "preserve_order" ) ) ) ]
982
+ #[ inline]
983
+ pub fn swap_remove_entry ( self ) -> ( String , Value ) {
984
+ self . occupied . swap_remove_entry ( )
985
+ }
986
+
987
+ /// Removes the entry from the map, returning the stored key and value.
988
+ ///
989
+ /// Like [`Vec::remove`], the entry is removed by shifting all of the
990
+ /// elements that follow it, preserving their relative order. This perturbs
991
+ /// the index of all of those elements!
992
+ ///
993
+ /// [`Vec::remove`]: std::vec::Vec::remove
994
+ #[ cfg( feature = "preserve_order" ) ]
995
+ #[ cfg_attr( docsrs, doc( cfg( feature = "preserve_order" ) ) ) ]
996
+ #[ inline]
997
+ pub fn shift_remove_entry ( self ) -> ( String , Value ) {
998
+ self . occupied . shift_remove_entry ( )
999
+ }
903
1000
}
904
1001
905
1002
//////////////////////////////////////////////////////////////////////////////
0 commit comments