mirror of https://github.com/xacrimon/dashmap
Merge 6e79b24539
into 626b98dab3
This commit is contained in:
commit
5ab2fa8f50
|
@ -10,7 +10,7 @@ use hashbrown::hash_map;
|
|||
use std::collections::hash_map::RandomState;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Iterator over a DashMap yielding key value pairs.
|
||||
/// Iterator over a `DashMap` yielding key value pairs.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -101,7 +101,7 @@ type GuardIterMut<'a, K, V, S> = (
|
|||
hash_map::IterMut<'a, K, SharedValue<V>>,
|
||||
);
|
||||
|
||||
/// Iterator over a DashMap yielding immutable references.
|
||||
/// Iterator over a `DashMap` yielding immutable references.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -184,7 +184,7 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
|
|||
}
|
||||
}
|
||||
|
||||
/// Iterator over a DashMap yielding mutable references.
|
||||
/// Iterator over a `DashMap` yielding mutable references.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
|
50
src/lib.rs
50
src/lib.rs
|
@ -74,14 +74,14 @@ fn ncb(shard_amount: usize) -> usize {
|
|||
shard_amount.trailing_zeros() as usize
|
||||
}
|
||||
|
||||
/// DashMap is an implementation of a concurrent associative array/hashmap in Rust.
|
||||
/// `DashMap` is an implementation of a concurrent associative array/hashmap in Rust.
|
||||
///
|
||||
/// DashMap tries to implement an easy to use API similar to `std::collections::HashMap`
|
||||
/// `DashMap` tries to implement an easy to use API similar to `std::collections::HashMap`
|
||||
/// with some slight changes to handle concurrency.
|
||||
///
|
||||
/// DashMap tries to be very simple to use and to be a direct replacement for `RwLock<HashMap<K, V, S>>`.
|
||||
/// `DashMap` tries to be very simple to use and to be a direct replacement for `RwLock<HashMap<K, V, S>>`.
|
||||
/// To accomplish this, all methods take `&self` instead of 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.
|
||||
/// This allows you to put a `DashMap` in an `Arc<T>` and share it between threads while being able to modify it.
|
||||
///
|
||||
/// Documentation mentioning locking behaviour acts in the reference frame of the calling thread.
|
||||
/// This means that it is safe to ignore it across multiple threads.
|
||||
|
@ -120,7 +120,7 @@ where
|
|||
}
|
||||
|
||||
impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, RandomState> {
|
||||
/// Creates a new DashMap with a capacity of 0.
|
||||
/// Creates a new `DashMap` with a capacity of 0.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -134,7 +134,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, RandomState> {
|
|||
DashMap::with_hasher(RandomState::default())
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified starting capacity.
|
||||
/// Creates a new `DashMap` with a specified starting capacity.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -149,10 +149,10 @@ impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, RandomState> {
|
|||
DashMap::with_capacity_and_hasher(capacity, RandomState::default())
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified shard amount
|
||||
/// Creates a new `DashMap` with a specified shard amount
|
||||
///
|
||||
/// shard_amount should greater than 0 and be a power of two.
|
||||
/// If a shard_amount which is not a power of two is provided, the function will panic.
|
||||
/// `shard_amount` should greater than 0 and be a power of two.
|
||||
/// If a `shard_amount` which is not a power of two is provided, the function will panic.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -167,10 +167,10 @@ impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, RandomState> {
|
|||
Self::with_capacity_and_hasher_and_shard_amount(0, RandomState::default(), shard_amount)
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified capacity and shard amount.
|
||||
/// Creates a new `DashMap` with a specified capacity and shard amount.
|
||||
///
|
||||
/// shard_amount should greater than 0 and be a power of two.
|
||||
/// If a shard_amount which is not a power of two is provided, the function will panic.
|
||||
/// `shard_amount` should greater than 0 and be a power of two.
|
||||
/// If a `shard_amount` which is not a power of two is provided, the function will panic.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -196,7 +196,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
ReadOnlyView::new(self)
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a capacity of 0 and the provided hasher.
|
||||
/// Creates a new `DashMap` with a capacity of 0 and the provided hasher.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -212,7 +212,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
Self::with_capacity_and_hasher(0, hasher)
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified starting capacity and hasher.
|
||||
/// Creates a new `DashMap` with a specified starting capacity and hasher.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -229,10 +229,10 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
Self::with_capacity_and_hasher_and_shard_amount(capacity, hasher, default_shard_amount())
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified hasher and shard amount
|
||||
/// Creates a new `DashMap` with a specified hasher and shard amount
|
||||
///
|
||||
/// shard_amount should be greater than 0 and a power of two.
|
||||
/// If a shard_amount which is not a power of two is provided, the function will panic.
|
||||
/// `shard_amount` should be greater than 0 and a power of two.
|
||||
/// If a `shard_amount` which is not a power of two is provided, the function will panic.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -249,10 +249,10 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
Self::with_capacity_and_hasher_and_shard_amount(0, hasher, shard_amount)
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified starting capacity, hasher and shard_amount.
|
||||
/// Creates a new `DashMap` with a specified starting capacity, hasher and `shard_amount`.
|
||||
///
|
||||
/// shard_amount should greater than 0 and be a power of two.
|
||||
/// If a shard_amount which is not a power of two is provided, the function will panic.
|
||||
/// `shard_amount` should greater than 0 and be a power of two.
|
||||
/// If a `shard_amount` which is not a power of two is provided, the function will panic.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -293,7 +293,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
}
|
||||
|
||||
/// Hash a given item to produce a usize.
|
||||
/// Uses the provided or default HashBuilder.
|
||||
/// Uses the provided or default `HashBuilder`.
|
||||
pub fn hash_usize<T: Hash>(&self, item: &T) -> usize {
|
||||
let mut hasher = self.hasher.build_hasher();
|
||||
|
||||
|
@ -517,7 +517,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
self._remove_if_mut(key, f)
|
||||
}
|
||||
|
||||
/// Creates an iterator over a DashMap yielding immutable references.
|
||||
/// Creates an iterator over a `DashMap` yielding immutable references.
|
||||
///
|
||||
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
|
||||
///
|
||||
|
@ -534,7 +534,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
self._iter()
|
||||
}
|
||||
|
||||
/// Iterator over a DashMap yielding mutable references.
|
||||
/// Iterator over a `DashMap` yielding mutable references.
|
||||
///
|
||||
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
|
||||
///
|
||||
|
@ -596,7 +596,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
}
|
||||
|
||||
/// Get an immutable reference to an entry in the map, if the shard is not locked.
|
||||
/// If the shard is locked, the function will return [TryResult::Locked].
|
||||
/// If the shard is locked, the function will return [`TryResult::Locked`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -623,7 +623,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
|
|||
}
|
||||
|
||||
/// Get a mutable reference to an entry in the map, if the shard is not locked.
|
||||
/// If the shard is locked, the function will return [TryResult::Locked].
|
||||
/// If the shard is locked, the function will return [`TryResult::Locked`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
|
|
@ -94,7 +94,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher> Entry<'a, K, V, S> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sets the value of the entry, and returns an OccupiedEntry.
|
||||
/// Sets the value of the entry, and returns an `OccupiedEntry`.
|
||||
///
|
||||
/// If you are not interested in the occupied entry,
|
||||
/// consider [`insert`] as it doesn't need to clone the key.
|
||||
|
@ -147,7 +147,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher> VacantEntry<'a, K, V, S> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sets the value of the entry with the VacantEntry’s key, and returns an OccupiedEntry.
|
||||
/// Sets the value of the entry with the VacantEntry’s key, and returns an `OccupiedEntry`.
|
||||
pub fn insert_entry(mut self, value: V) -> OccupiedEntry<'a, K, V, S>
|
||||
where
|
||||
K: Clone,
|
||||
|
|
|
@ -140,7 +140,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher> RefMut<'a, K, V, S> {
|
|||
where
|
||||
F: FnOnce(&mut V) -> Option<&mut T>,
|
||||
{
|
||||
let v = match f(unsafe { &mut *(self.v as *mut _) }) {
|
||||
let v = match f(unsafe { &mut *self.v.cast() }) {
|
||||
Some(v) => v,
|
||||
None => return Err(self),
|
||||
};
|
||||
|
@ -297,7 +297,7 @@ impl<'a, K: Eq + Hash, V, T, S: BuildHasher> MappedRefMut<'a, K, V, T, S> {
|
|||
where
|
||||
F: FnOnce(&mut T) -> Option<&mut T2>,
|
||||
{
|
||||
let v = match f(unsafe { &mut *(self.v as *mut _) }) {
|
||||
let v = match f(unsafe { &mut *self.v.cast() }) {
|
||||
Some(v) => v,
|
||||
None => return Err(self),
|
||||
};
|
||||
|
|
14
src/set.rs
14
src/set.rs
|
@ -12,7 +12,7 @@ use core::hash::{BuildHasher, Hash};
|
|||
use core::iter::FromIterator;
|
||||
use std::collections::hash_map::RandomState;
|
||||
|
||||
/// DashSet is a thin wrapper around [`DashMap`] using `()` as the value type. It uses
|
||||
/// `DashSet` is a thin wrapper around [`DashMap`] using `()` as the value type. It uses
|
||||
/// methods and types which are more convenient to work with on a set.
|
||||
///
|
||||
/// [`DashMap`]: struct.DashMap.html
|
||||
|
@ -49,7 +49,7 @@ where
|
|||
}
|
||||
|
||||
impl<'a, K: 'a + Eq + Hash> DashSet<K, RandomState> {
|
||||
/// Creates a new DashSet with a capacity of 0.
|
||||
/// Creates a new `DashSet` with a capacity of 0.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -63,7 +63,7 @@ impl<'a, K: 'a + Eq + Hash> DashSet<K, RandomState> {
|
|||
Self::with_hasher(RandomState::default())
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified starting capacity.
|
||||
/// Creates a new `DashMap` with a specified starting capacity.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -80,7 +80,7 @@ impl<'a, K: 'a + Eq + Hash> DashSet<K, RandomState> {
|
|||
}
|
||||
|
||||
impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
|
||||
/// Creates a new DashMap with a capacity of 0 and the provided hasher.
|
||||
/// Creates a new `DashMap` with a capacity of 0 and the provided hasher.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -96,7 +96,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
|
|||
Self::with_capacity_and_hasher(0, hasher)
|
||||
}
|
||||
|
||||
/// Creates a new DashMap with a specified starting capacity and hasher.
|
||||
/// Creates a new `DashMap` with a specified starting capacity and hasher.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -116,7 +116,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
|
|||
}
|
||||
|
||||
/// Hash a given item to produce a usize.
|
||||
/// Uses the provided or default HashBuilder.
|
||||
/// Uses the provided or default `HashBuilder`.
|
||||
pub fn hash_usize<T: Hash>(&self, item: &T) -> usize {
|
||||
self.inner.hash_usize(item)
|
||||
}
|
||||
|
@ -252,7 +252,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
|
|||
self.inner.remove_if(key, |k, _| f(k)).map(|(k, _)| k)
|
||||
}
|
||||
|
||||
/// Creates an iterator over a DashMap yielding immutable references.
|
||||
/// Creates an iterator over a `DashMap` yielding immutable references.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/// Represents the result of a non-blocking read from a [DashMap](crate::DashMap).
|
||||
/// Represents the result of a non-blocking read from a [`DashMap`](crate::DashMap).
|
||||
#[derive(Debug)]
|
||||
pub enum TryResult<R> {
|
||||
/// The value was present in the map, and the lock for the shard was successfully obtained.
|
||||
|
|
Loading…
Reference in New Issue