Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit f8a99a4

Browse files
committed
Add swap_remove and shift_remove methods on Mapping
1 parent 8b26413 commit f8a99a4

File tree

2 files changed

+101
-21
lines changed

2 files changed

+101
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repository = "https://github.com/dtolnay/serde-yaml"
1212
rust-version = "1.64"
1313

1414
[dependencies]
15-
indexmap = "2"
15+
indexmap = "2.2.1"
1616
itoa = "1.0"
1717
ryu = "1.0"
1818
serde = "1.0.195"

src/mapping.rs

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,65 @@ impl Mapping {
8787
}
8888

8989
/// 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.
9095
#[inline]
9196
pub fn remove<I: Index>(&mut self, index: I) -> Option<Value> {
92-
index.remove_from(self)
97+
self.swap_remove(index)
9398
}
9499

95100
/// 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.
96106
#[inline]
97107
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)
99149
}
100150

101151
/// Scan through each key-value pair in the map and keep those where the
@@ -203,10 +253,16 @@ pub trait Index: private::Sealed {
203253
fn index_into_mut<'a>(&self, v: &'a mut Mapping) -> Option<&'a mut Value>;
204254

205255
#[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>;
207263

208264
#[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)>;
210266
}
211267

212268
struct HashLikeValue<'a>(&'a str);
@@ -239,11 +295,17 @@ impl Index for Value {
239295
fn index_into_mut<'a>(&self, v: &'a mut Mapping) -> Option<&'a mut Value> {
240296
v.map.get_mut(self)
241297
}
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)
244300
}
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)
247309
}
248310
}
249311

@@ -257,11 +319,17 @@ impl Index for str {
257319
fn index_into_mut<'a>(&self, v: &'a mut Mapping) -> Option<&'a mut Value> {
258320
v.map.get_mut(&HashLikeValue(self))
259321
}
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))
262330
}
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))
265333
}
266334
}
267335

@@ -275,11 +343,17 @@ impl Index for String {
275343
fn index_into_mut<'a>(&self, v: &'a mut Mapping) -> Option<&'a mut Value> {
276344
self.as_str().index_into_mut(v)
277345
}
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)
280348
}
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)
283357
}
284358
}
285359

@@ -296,11 +370,17 @@ where
296370
fn index_into_mut<'a>(&self, v: &'a mut Mapping) -> Option<&'a mut Value> {
297371
(**self).index_into_mut(v)
298372
}
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)
301381
}
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)
304384
}
305385
}
306386

0 commit comments

Comments
 (0)