txmap 1.0.0

A concurrent transactional hash map with fine-grained locking and internal mutability
Documentation
use crate::{indexer::Indexer, ops::op_trait::OpTrait, result::MISSING_MUTEX_GUARD_ERROR};
use hashbrown::HashMap;
use intmap::IntMap;
use parking_lot::MutexGuard;
use std::hash::Hash;

pub(crate) struct ModifyOp<K, V, P = ()> {
    guards_bitmask: u128,
    key_index: u8,
    key: K,
    #[allow(clippy::type_complexity)]
    mutate: Box<dyn Fn(&K, &mut V, &P)>,
}

impl<K, V, P> ModifyOp<K, V, P>
where
    K: Hash,
{
    pub fn new_with_params<M>(indexer: &Indexer, key: K, mutate: M) -> Self
    where
        M: Fn(&K, &mut V, &P) + 'static,
    {
        let key_index = indexer.index(&key);
        Self {
            guards_bitmask: 1 << key_index,
            key_index,
            key,
            mutate: Box::new(mutate),
        }
    }
}

impl<K, V> ModifyOp<K, V, ()>
where
    K: Hash,
{
    pub fn new<M>(indexer: &Indexer, key: K, mutate: M) -> Self
    where
        M: Fn(&K, &mut V) + 'static,
    {
        Self::new_with_params(indexer, key, move |k, v, _| mutate(k, v))
    }
}

impl<K, V, P> OpTrait<K, V, P> for ModifyOp<K, V, P>
where
    K: Hash + Eq,
{
    fn guards_bitmask(&self) -> u128 {
        self.guards_bitmask
    }
    fn apply(&self, mutex_guards: &mut IntMap<u8, MutexGuard<'_, HashMap<K, V>>>, params: &P) {
        let mutex_guard = mutex_guards
            .get_mut(self.key_index)
            .expect(MISSING_MUTEX_GUARD_ERROR);
        if let Some(key_mut_value) = mutex_guard.get_key_value_mut(&self.key) {
            (self.mutate)(key_mut_value.0, key_mut_value.1, params)
        }
    }
}