Skip to content

Commit 5225abd

Browse files
authored
Merge pull request #560 from prutschman/truncate_bug
Fix #559: IndexMap::truncate leaves map in inconsistent state
2 parents 24fccaf + daa9196 commit 5225abd

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
9999

100100
### Fixed
101101

102+
- Fixed bug in `IndexMap::truncate` that left the map in an inconsistent state.
102103
- Fixed clippy lints.
103104
- Fixed `{arc,box,object}_pool!` emitting clippy lints.
104105
- Fixed the list of implemented data structures in the crate docs, by adding `Deque`,

src/index_map.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ where
12451245
///
12461246
/// If `len` is greater than the map's current length, this has no effect.
12471247
///
1248-
/// Computes in *O*(1) time (average).
1248+
/// Computes in *O*(n) time (average).
12491249
///
12501250
/// # Examples
12511251
///
@@ -1258,6 +1258,7 @@ where
12581258
/// map.insert(1, "c").unwrap();
12591259
/// map.truncate(2);
12601260
/// assert_eq!(map.len(), 2);
1261+
/// assert_eq!(map.get(&1), None);
12611262
///
12621263
/// let mut iter = map.iter();
12631264
/// assert_eq!(iter.next(), Some((&3, &"a")));
@@ -1266,6 +1267,15 @@ where
12661267
/// ```
12671268
pub fn truncate(&mut self, len: usize) {
12681269
self.core.entries.truncate(len);
1270+
1271+
if self.core.indices.len() > self.core.entries.len() {
1272+
for index in self.core.indices.iter_mut() {
1273+
match index {
1274+
Some(pos) if pos.index() >= len => *index = None,
1275+
_ => (),
1276+
}
1277+
}
1278+
}
12691279
}
12701280

12711281
/* Private API */

0 commit comments

Comments
 (0)