Add insert_same extension to HashMap
This commit is contained in:
parent
e5a602e364
commit
60d0cbe532
1 changed files with 14 additions and 0 deletions
|
@ -29,6 +29,8 @@
|
||||||
//! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
|
//! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
|
||||||
//! depending on the value of cfg!(parallel_queries).
|
//! depending on the value of cfg!(parallel_queries).
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::hash::{Hash, BuildHasher};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fmt::Formatter;
|
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_val<T: ?Sized + Send>(_t: &T) {}
|
||||||
pub fn assert_send_sync_val<T: ?Sized + Sync + 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> {
|
impl<T: Copy + Debug> Debug for LockCell<T> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
f.debug_struct("LockCell")
|
f.debug_struct("LockCell")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue