@@ -87,15 +87,65 @@ impl Mapping {
87
87
}
88
88
89
89
/// Removes and returns the value corresponding to the key from the map.
90
+ ///
91
+ /// This is equivalent to [`.swap_remove(index)`][Self::swap_remove],
92
+ /// replacing this entry’s position with the last element. If you need to
93
+ /// preserve the relative order of the keys in the map, use
94
+ /// [`.shift_remove(key)`][Self::shift_remove] instead.
90
95
#[ inline]
91
96
pub fn remove < I : Index > ( & mut self , index : I ) -> Option < Value > {
92
- index . remove_from ( self )
97
+ self . swap_remove ( index )
93
98
}
94
99
95
100
/// Remove and return the key-value pair.
101
+ ///
102
+ /// This is equivalent to [`.swap_remove_entry(index)`][Self::swap_remove_entry],
103
+ /// replacing this entry’s position with the last element. If you need to
104
+ /// preserve the relative order of the keys in the map, use
105
+ /// [`.shift_remove_entry(key)`][Self::shift_remove_entry] instead.
96
106
#[ inline]
97
107
pub fn remove_entry < I : Index > ( & mut self , index : I ) -> Option < ( Value , Value ) > {
98
- index. remove_entry_from ( self )
108
+ self . swap_remove_entry ( index)
109
+ }
110
+
111
+ /// Removes and returns the value corresponding to the key from the map.
112
+ ///
113
+ /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the
114
+ /// last element of the map and popping it off. This perturbs the position
115
+ /// of what used to be the last element!
116
+ #[ inline]
117
+ pub fn swap_remove < I : Index > ( & mut self , index : I ) -> Option < Value > {
118
+ index. swap_remove_from ( self )
119
+ }
120
+
121
+ /// Remove and return the key-value pair.
122
+ ///
123
+ /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the
124
+ /// last element of the map and popping it off. This perturbs the position
125
+ /// of what used to be the last element!
126
+ #[ inline]
127
+ pub fn swap_remove_entry < I : Index > ( & mut self , index : I ) -> Option < ( Value , Value ) > {
128
+ index. swap_remove_entry_from ( self )
129
+ }
130
+
131
+ /// Removes and returns the value corresponding to the key from the map.
132
+ ///
133
+ /// Like [`Vec::remove`], the entry is removed by shifting all of the
134
+ /// elements that follow it, preserving their relative order. This perturbs
135
+ /// the index of all of those elements!
136
+ #[ inline]
137
+ pub fn shift_remove < I : Index > ( & mut self , index : I ) -> Option < Value > {
138
+ index. shift_remove_from ( self )
139
+ }
140
+
141
+ /// Remove and return the key-value pair.
142
+ ///
143
+ /// Like [`Vec::remove`], the entry is removed by shifting all of the
144
+ /// elements that follow it, preserving their relative order. This perturbs
145
+ /// the index of all of those elements!
146
+ #[ inline]
147
+ pub fn shift_remove_entry < I : Index > ( & mut self , index : I ) -> Option < ( Value , Value ) > {
148
+ index. shift_remove_entry_from ( self )
99
149
}
100
150
101
151
/// Scan through each key-value pair in the map and keep those where the
@@ -203,10 +253,16 @@ pub trait Index: private::Sealed {
203
253
fn index_into_mut < ' a > ( & self , v : & ' a mut Mapping ) -> Option < & ' a mut Value > ;
204
254
205
255
#[ doc( hidden) ]
206
- fn remove_from ( & self , v : & mut Mapping ) -> Option < Value > ;
256
+ fn swap_remove_from ( & self , v : & mut Mapping ) -> Option < Value > ;
257
+
258
+ #[ doc( hidden) ]
259
+ fn swap_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > ;
260
+
261
+ #[ doc( hidden) ]
262
+ fn shift_remove_from ( & self , v : & mut Mapping ) -> Option < Value > ;
207
263
208
264
#[ doc( hidden) ]
209
- fn remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > ;
265
+ fn shift_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > ;
210
266
}
211
267
212
268
struct HashLikeValue < ' a > ( & ' a str ) ;
@@ -239,11 +295,17 @@ impl Index for Value {
239
295
fn index_into_mut < ' a > ( & self , v : & ' a mut Mapping ) -> Option < & ' a mut Value > {
240
296
v. map . get_mut ( self )
241
297
}
242
- fn remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
243
- v. map . remove ( self )
298
+ fn swap_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
299
+ v. map . swap_remove ( self )
244
300
}
245
- fn remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
246
- v. map . remove_entry ( self )
301
+ fn swap_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
302
+ v. map . swap_remove_entry ( self )
303
+ }
304
+ fn shift_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
305
+ v. map . shift_remove ( self )
306
+ }
307
+ fn shift_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
308
+ v. map . shift_remove_entry ( self )
247
309
}
248
310
}
249
311
@@ -257,11 +319,17 @@ impl Index for str {
257
319
fn index_into_mut < ' a > ( & self , v : & ' a mut Mapping ) -> Option < & ' a mut Value > {
258
320
v. map . get_mut ( & HashLikeValue ( self ) )
259
321
}
260
- fn remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
261
- v. map . remove ( & HashLikeValue ( self ) )
322
+ fn swap_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
323
+ v. map . swap_remove ( & HashLikeValue ( self ) )
324
+ }
325
+ fn swap_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
326
+ v. map . swap_remove_entry ( & HashLikeValue ( self ) )
327
+ }
328
+ fn shift_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
329
+ v. map . shift_remove ( & HashLikeValue ( self ) )
262
330
}
263
- fn remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
264
- v. map . remove_entry ( & HashLikeValue ( self ) )
331
+ fn shift_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
332
+ v. map . shift_remove_entry ( & HashLikeValue ( self ) )
265
333
}
266
334
}
267
335
@@ -275,11 +343,17 @@ impl Index for String {
275
343
fn index_into_mut < ' a > ( & self , v : & ' a mut Mapping ) -> Option < & ' a mut Value > {
276
344
self . as_str ( ) . index_into_mut ( v)
277
345
}
278
- fn remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
279
- self . as_str ( ) . remove_from ( v)
346
+ fn swap_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
347
+ self . as_str ( ) . swap_remove_from ( v)
280
348
}
281
- fn remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
282
- self . as_str ( ) . remove_entry_from ( v)
349
+ fn swap_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
350
+ self . as_str ( ) . swap_remove_entry_from ( v)
351
+ }
352
+ fn shift_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
353
+ self . as_str ( ) . shift_remove_from ( v)
354
+ }
355
+ fn shift_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
356
+ self . as_str ( ) . shift_remove_entry_from ( v)
283
357
}
284
358
}
285
359
@@ -296,11 +370,17 @@ where
296
370
fn index_into_mut < ' a > ( & self , v : & ' a mut Mapping ) -> Option < & ' a mut Value > {
297
371
( * * self ) . index_into_mut ( v)
298
372
}
299
- fn remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
300
- ( * * self ) . remove_from ( v)
373
+ fn swap_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
374
+ ( * * self ) . swap_remove_from ( v)
375
+ }
376
+ fn swap_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
377
+ ( * * self ) . swap_remove_entry_from ( v)
378
+ }
379
+ fn shift_remove_from ( & self , v : & mut Mapping ) -> Option < Value > {
380
+ ( * * self ) . shift_remove_from ( v)
301
381
}
302
- fn remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
303
- ( * * self ) . remove_entry_from ( v)
382
+ fn shift_remove_entry_from ( & self , v : & mut Mapping ) -> Option < ( Value , Value ) > {
383
+ ( * * self ) . shift_remove_entry_from ( v)
304
384
}
305
385
}
306
386
0 commit comments