Skip to main content

ConcurrentHashMap

Struct ConcurrentHashMap 

Source
pub struct ConcurrentHashMap<K, V, S = RandomState> { /* private fields */ }
Expand description

Concurrent hash map with segment-based locking for scalability. This provides better scalability than a single mutex-protected HashMap.

Implementations§

Source§

impl<K: Hash + Eq, V> ConcurrentHashMap<K, V>

Source

pub fn new() -> Self

Create a new concurrent hash map with default hasher.

Source

pub fn with_segments(num_segments: usize) -> Self

Create with a specific number of segments (must be power of 2).

Source§

impl<K: Hash + Eq, V, S: BuildHasher> ConcurrentHashMap<K, V, S>

Source

pub fn insert(&self, key: K, value: V) -> Result<Option<V>, SegmentPoisoned>

Insert a key-value pair.

Returns the previous value if the key existed, or None if it was a new key.

§Errors

Returns SegmentPoisoned if the segment lock was poisoned by a panicked writer.

Source

pub fn get_or_insert_with<F>( &self, key: K, default: F, ) -> Result<V, SegmentPoisoned>
where F: FnOnce() -> V, V: Clone,

Get a value by key, or insert it if it is not present.

Executes atomically under the segment’s write lock, ensuring that the default closure runs exactly once on a cache miss and no concurrent insert can overwrite it.

§Errors

Returns SegmentPoisoned if the segment lock was poisoned by a panicked writer.

Source

pub fn get(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>
where V: Clone,

Get a value by key.

Returns the cloned value if found, or None if not found.

§Errors

Returns SegmentPoisoned if the segment lock was poisoned by a panicked writer.

Source

pub fn remove(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>

Remove a key-value pair.

Returns the removed value if the key existed, or None if it didn’t exist.

§Errors

Returns SegmentPoisoned if the segment lock was poisoned by a panicked writer.

Source

pub fn contains_key(&self, key: &K) -> Result<bool, SegmentPoisoned>

Check if a key exists.

Returns true if the key exists, false otherwise.

§Errors

Returns SegmentPoisoned if the segment lock was poisoned by a panicked writer.

Trait Implementations§

Source§

impl<K, V, S> Debug for ConcurrentHashMap<K, V, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash + Eq, V> Default for ConcurrentHashMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: Freeze, S: Freeze,

§

impl<K, V, S> RefUnwindSafe for ConcurrentHashMap<K, V, S>

§

impl<K, V, S> Send for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: Send, S: Send,

§

impl<K, V, S> Sync for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: Sync, S: Sync,

§

impl<K, V, S> Unpin for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: Unpin, S: Unpin,

§

impl<K, V, S> UnsafeUnpin for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: UnsafeUnpin, S: UnsafeUnpin,

§

impl<K, V, S> UnwindSafe for ConcurrentHashMap<K, V, S>
where Vec<RwLock<HashMap<K, V, S>>>: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.