Skip to main content

AtomicMap

Struct AtomicMap 

Source
pub struct AtomicMap<K, V>(/* private fields */);
Expand description

A lock-free concurrent map optimized for read-heavy access patterns.

Reads are a single atomic pointer load with no contention between readers. Writes clone the inner map, mutate the clone, and atomically swap it in.

Not safe for concurrent writers using load/store: the last store wins and earlier updates are silently lost. Use rcu when multiple writers may race, or restrict writes to a single task.

Wrap in Arc for shared ownership across threads.

Implementations§

Source§

impl<K, V> AtomicMap<K, V>

Source

pub fn new() -> Self

Creates a new empty atomic map.

Source

pub fn load(&self) -> Guard<Arc<AHashMap<K, V>>>

Returns a snapshot guard for direct access to the inner map.

The guard dereferences to AHashMap<K, V>. Use for operations that need a reference into the map (e.g., load().get(&key)).

Source

pub fn store(&self, map: AHashMap<K, V>)

Atomically replaces the inner map.

Source§

impl<K, V> AtomicMap<K, V>
where K: Eq + Hash + Clone, V: Clone,

Source

pub fn rcu<F>(&self, f: F)
where F: FnMut(&mut AHashMap<K, V>),

Atomically applies f to a clone of the inner map.

Retries if another writer swapped the map between the clone and the compare-and-swap, so f may run more than once.

Source

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

Returns true if the map contains the given key.

Source

pub fn get_cloned(&self, key: &K) -> Option<V>

Returns a clone of the value for the given key, if present.

Source

pub fn insert(&self, key: K, value: V)

Inserts a key-value pair (clone-and-swap).

Source

pub fn remove(&self, key: &K)

Removes a key (clone-and-swap).

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

Trait Implementations§

Source§

impl<K: Debug + Eq + Hash, V: Debug> Debug for AtomicMap<K, V>

Source§

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

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

impl<K, V> Default for AtomicMap<K, V>

Source§

fn default() -> Self

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

impl<K: Eq + Hash, V> From<AHashMap<K, V>> for AtomicMap<K, V>

Source§

fn from(map: AHashMap<K, V>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<K, V> !Freeze for AtomicMap<K, V>

§

impl<K, V> RefUnwindSafe for AtomicMap<K, V>

§

impl<K, V> Send for AtomicMap<K, V>
where K: Sync + Send, V: Sync + Send,

§

impl<K, V> Sync for AtomicMap<K, V>
where K: Sync + Send, V: Sync + Send,

§

impl<K, V> Unpin for AtomicMap<K, V>

§

impl<K, V> UnsafeUnpin for AtomicMap<K, V>

§

impl<K, V> UnwindSafe for AtomicMap<K, V>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> Ungil for T
where T: Send,