Skip to content

Commit

Permalink
Fix overflowing shift right with Default settings ncb to zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpbriggs committed Jan 15, 2020
1 parent d6f1025 commit 977023b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fn ncb(shard_amount: usize) -> usize {
/// DashMap tries to be very simple to use and to be a direct replacement for `RwLock<HashMap<K, V>>`.
/// To accomplish these all methods take `&self` instead modifying methods taking `&mut self`.
/// This allows you to put a DashMap in an `Arc<T>` and share it between threads while being able to modify it.
#[derive(Default)]
pub struct DashMap<K, V, S = FxBuildHasher>
where
K: Eq + Hash,
Expand All @@ -56,6 +55,15 @@ where
shards: Box<[RwLock<HashMap<K, V, S>>]>,
}

impl<K, V> Default for DashMap<K, V>
where
K: Eq + Hash,
{
fn default() -> Self {
Self::new()
}
}

impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, FxBuildHasher> {
/// Creates a new DashMap with a capacity of 0.
///
Expand Down Expand Up @@ -704,3 +712,20 @@ impl<K: Eq + Hash, V> FromIterator<(K, V)> for DashMap<K, V, FxBuildHasher> {
map
}
}

#[cfg(test)]
mod tests {
use crate::DashMap;
#[test]
fn test_basic() {
let dm = DashMap::new();
dm.insert(0, 0);
assert_eq!(dm.get(&0).unwrap().value(), &0);
}
#[test]
fn test_default() {
let dm: DashMap<u32, u32> = DashMap::default();
dm.insert(0, 0);
assert_eq!(dm.get(&0).unwrap().value(), &0);
}
}

0 comments on commit 977023b

Please sign in to comment.