use once_cell::sync::Lazy; use std::cmp::Eq; use std::collections::HashMap; use std::hash::Hash; use std::ops::Deref; use std::sync::Arc; use std::sync::{Mutex, MutexGuard}; //improve this later, replace job cacher with it //possibly add different backends (memory, disk) pub struct SharedStorage(Arc>>); impl SharedStorage { pub fn new() -> Lazy> { Lazy::new(|| SharedStorage(Arc::new(Mutex::new(HashMap::new())))) } pub fn lock(&self) -> MutexGuard<'_, HashMap> { self.0.lock().unwrap() } pub fn get<'get, 'slf: 'get>(&'slf self, key: &'get Key) -> Option> { if !self.lock().contains_key(key) { return None; } let lock = self.lock(); Some(RefHolder(lock, key)) } } pub struct RefHolder<'h, Key, Val>(pub MutexGuard<'h, HashMap>, pub &'h Key); impl<'h, Key: Eq + Hash, Val> Deref for RefHolder<'h, Key, Val> { type Target = Val; fn deref(&self) -> &Self::Target { self.0.get(self.1).unwrap() } }