Hello Im a RUST begginer Im dealing with some Borrowing issues that I can't solve
I want to make a Concurrent Map, I saw some examples to do it where I need it and it works, but now I want to generalize the solution writing my own mod.
Here it is
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Mutex;
pub struct ConcurrentMap<K, V> {
elems: Mutex<HashMap<K, V>>,
}
impl<K: Hash + Eq, V> ConcurrentMap<K, V> {
pub fn new() -> ConcurrentMap<K, V> {
let map: HashMap<K, V> = HashMap::new();
ConcurrentMap {
elems: Mutex::new(map),
}
}
pub fn get(&self, k: &K) -> Option<&V> {
let map = self.elems.lock();
match map {
Ok(t) => {
return match t.get(k) {
None => None,
Some(v) => Some(v),
}
}
Err(e) => panic!("GET Concurrent Map Error: {}", e),
}
}
}
My problem is in Some(v) => Some(v)
line, I dont know how to copy the value.
If I make that V implement Copy trait then I can't use this Map to store String because String does not implement Copy trait.
Maybe the last matching is not necessary but is the last thing I tried.