Hi,
I'm trying to share a HashMap between two threads, one of which shall update the nested array if the key is already present and initialize a new one if there's no value associated with it.
Ho wever, because i'm trying to obtain a reference to the value and try inserting within same code block, I get in trouble for trying to use mutable reference twice.
I'm trying to find a language-idiomatic way to solve the problem, although so far without big success. Code follows.
Thank you!
fn put_metric<'a>(metrics: Arc<Mutex<HashMap<&'a str, RingBuffer<Metric>>>>,
name: &'a str,
metric: Metric) {
let mut unwrapped = metrics.lock().unwrap();
match unwrapped.get_mut(name) {
None => {
let mut rb: RingBuffer<Metric> = RingBuffer { size: 5,
vec: VecDeque::new() };
rb.publish(metric);
unwrapped.insert(name, rb); // Here compiler complains that value was used twice
},
Some(rb) => {
rb.publish(metric);
}
};
}