diff --git a/crates/iddqd/src/bi_hash_map/imp.rs b/crates/iddqd/src/bi_hash_map/imp.rs index abbea1b..69976b6 100644 --- a/crates/iddqd/src/bi_hash_map/imp.rs +++ b/crates/iddqd/src/bi_hash_map/imp.rs @@ -1927,19 +1927,24 @@ impl BiHashMap { ([MapHash::new(hash1), MapHash::new(hash2)], dormant_item) }; - // SAFETY: The original items is no longer used after the first - // block above. - let items = unsafe { dormant_items.awaken() }; - // SAFETY: The original item is no longer used after the second - // block above. - let item = unsafe { dormant_item.awaken() }; - let hash2 = hashes[1].hash(); + let retain = { + // SAFETY: The original item is no longer used after the second + // block above. dormant_items, from which item is derived, is + // currently dormant. + let item = unsafe { dormant_item.awaken() }; + + let ref_mut = RefMut::new(hash_state.clone(), hashes, item); + f(ref_mut) + }; - let ref_mut = RefMut::new(hash_state.clone(), hashes, item); - if f(ref_mut) { + if retain { true } else { + // SAFETY: The original items is no longer used after the first + // block above, and item + dormant_item have been dropped after + // being used above. + let items = unsafe { dormant_items.awaken() }; items.remove(index); let k2_entry = self .tables diff --git a/crates/iddqd/src/id_hash_map/imp.rs b/crates/iddqd/src/id_hash_map/imp.rs index 9e5c404..2efdcdf 100644 --- a/crates/iddqd/src/id_hash_map/imp.rs +++ b/crates/iddqd/src/id_hash_map/imp.rs @@ -1341,17 +1341,22 @@ impl IdHashMap { (MapHash::new(hash), dormant_item) }; - // SAFETY: The original items is no longer used after the first - // block above. - let items = unsafe { dormant_items.awaken() }; - // SAFETY: The original item is no longer used after the second - // block above. - let item = unsafe { dormant_item.awaken() }; - - let ref_mut = RefMut::new(hash_state.clone(), hash, item); - if f(ref_mut) { + let retain = { + // SAFETY: The original item is no longer used after the second + // block above. dormant_items, from which item is derived, is + // currently dormant. + let item = unsafe { dormant_item.awaken() }; + + let ref_mut = RefMut::new(hash_state.clone(), hash, item); + f(ref_mut) + }; + + if retain { true } else { + // SAFETY: The original items is no longer used after the first + // block above, and item + dormant item have been used above. + let items = unsafe { dormant_items.awaken() }; items.remove(index); false } diff --git a/crates/iddqd/src/id_ord_map/imp.rs b/crates/iddqd/src/id_ord_map/imp.rs index 54cabdc..dd40048 100644 --- a/crates/iddqd/src/id_ord_map/imp.rs +++ b/crates/iddqd/src/id_ord_map/imp.rs @@ -1329,17 +1329,23 @@ impl IdOrdMap { (MapHash::new(hash), dormant_item) }; - // SAFETY: The original items is no longer used after the first - // block above. - let items = unsafe { dormant_items.awaken() }; - // SAFETY: The original item is no longer used after the second - // block above. - let item = unsafe { dormant_item.awaken() }; - - let ref_mut = RefMut::new(hash_state.clone(), hash, item); - if f(ref_mut) { + let retain = { + // SAFETY: The original item is no longer used after the second + // block above. dormant_items, from which item is derived, is + // currently dormant. + let item = unsafe { dormant_item.awaken() }; + + let ref_mut = RefMut::new(hash_state.clone(), hash, item); + f(ref_mut) + }; + + if retain { true } else { + // SAFETY: The original items is no longer used after the first + // block above, and item + dormant_item have been dropped after + // being used above. + let items = unsafe { dormant_items.awaken() }; items.remove(index); false } diff --git a/crates/iddqd/src/tri_hash_map/imp.rs b/crates/iddqd/src/tri_hash_map/imp.rs index a849b3b..1e0c25c 100644 --- a/crates/iddqd/src/tri_hash_map/imp.rs +++ b/crates/iddqd/src/tri_hash_map/imp.rs @@ -2586,20 +2586,25 @@ impl TriHashMap { ) }; - // SAFETY: The original items is no longer used after the first - // block above. - let items = unsafe { dormant_items.awaken() }; - // SAFETY: The original item is no longer used after the second - // block above. - let item = unsafe { dormant_item.awaken() }; - let hash2 = hashes[1].hash(); let hash3 = hashes[2].hash(); + let retain = { + // SAFETY: The original item is no longer used after the second + // block above. dormant_items, from which item is derived, is + // currently dormant. + let item = unsafe { dormant_item.awaken() }; + + let ref_mut = RefMut::new(hash_state.clone(), hashes, item); + f(ref_mut) + }; - let ref_mut = RefMut::new(hash_state.clone(), hashes, item); - if f(ref_mut) { + if retain { true } else { + // SAFETY: The original items is no longer used after the first + // block above, and item + dormant_item have been dropped after + // being used above. + let items = unsafe { dormant_items.awaken() }; items.remove(index); let k2_entry = self .tables