I have been trying to find a way to get more than one (mutable-) reference from a Hashmap at different indices and return it as a result of a function/macro.
For this i have been experimenting with Rc<RefCell>> combinations but whenever i allways run into some problem.
Minimum example of what i am trying to do:
fn get_data(map: &HashMap<usize, Rc<RefCell<ArrayD<isize>>>>, index: usize) -> Option<ArrayViewMut<isize, Ix1>> {
match map.get(&index) {
Some(data) => {
Some(data.borrow_mut().slice_mut(s![..]))
},
_ => None
}
}
fn main() {
let mut example: HashMap<usize, Rc<RefCell<ArrayD<isize>>>> = HashMap::new();
example.insert(0, Rc::new(RefCell::new(Array::from_elem(IxDyn(&[3]), 0))));
example.insert(1, Rc::new(RefCell::new(Array::from_elem(IxDyn(&[3]), 1))));
example.insert(2, Rc::new(RefCell::new(Array::from_elem(IxDyn(&[3]), 2))));
let mut sum = get_data(&example, 0).unwrap();
let value1 = get_data(&example, 1).unwrap();
let value2 = get_data(&example, 2).unwrap();
let temp = &value1 + &value2;
sum.assign(&temp);
}
I have already read through this topic, but there he needs to modify his reference only once but in my case i need the same reference at multiple locations.
Based on this topic one might think that changing to Vec instead of Hashmap could work based on the example. But in the final version the Hashmap indices are different so that would not work.
I have also looked at the nightly feature that is described in this topic, but here you can only get references as a group and not individually. This is a problem for me as i dont allways know which indices to get at compile time.
My question therefore is:
Is there a way for me to make sure that the arrayView(-Mut) can be returned without the compiler complaining that it outlives "res"?
If the above is not possible:
Is there a better way to get multiple individual references from a Hashmap simultaneously?
EDIT: fixed duplicate indices in given example resulting in runtime panic