about summary refs log tree commit diff
path: root/src/librustc_data_structures
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2018-04-01 10:25:16 +0200
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2018-04-10 14:39:33 +0200
commit60d0cbe532eba39dba75d84b1eb98abf7cd12a48 (patch)
treea3d12f4873961aa189ecbe7bd8c20250d96df883 /src/librustc_data_structures
parente5a602e364d5083a4c475747ad08c81ef29897bf (diff)
downloadrust-60d0cbe532eba39dba75d84b1eb98abf7cd12a48.tar.gz
rust-60d0cbe532eba39dba75d84b1eb98abf7cd12a48.zip
Add insert_same extension to HashMap
Diffstat (limited to 'src/librustc_data_structures')
-rw-r--r--src/librustc_data_structures/sync.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/librustc_data_structures/sync.rs b/src/librustc_data_structures/sync.rs
index ad524916f0c..19039b9b0b0 100644
--- a/src/librustc_data_structures/sync.rs
+++ b/src/librustc_data_structures/sync.rs
@@ -29,6 +29,8 @@
 //! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
 //! depending on the value of cfg!(parallel_queries).
 
+use std::collections::HashMap;
+use std::hash::{Hash, BuildHasher};
 use std::cmp::Ordering;
 use std::fmt::Debug;
 use std::fmt::Formatter;
@@ -227,6 +229,18 @@ pub fn assert_sync<T: ?Sized + Sync>() {}
 pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
 pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
 
+pub trait HashMapExt<K, V> {
+    /// Same as HashMap::insert, but it may panic if there's already an
+    /// entry for `key` with a value not equal to `value`
+    fn insert_same(&mut self, key: K, value: V);
+}
+
+impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> {
+    fn insert_same(&mut self, key: K, value: V) {
+        self.entry(key).and_modify(|old| assert!(*old == value)).or_insert(value);
+    }
+}
+
 impl<T: Copy + Debug> Debug for LockCell<T> {
     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
         f.debug_struct("LockCell")