So I am dealing with some variables that are shared and must have mutable access from several places that won't conflict at runtime. These variables are mostly a wrapper around some vectors of generic elements.
To that effect, I have wrapped my structure with an Rc
and RefCell
. The type essentially looks like this:
type Shell<T> = Rc<RefCell<Vec<T>>>;
Because of how the program works, I have several of these collected in an array with a known size.
So the final thing is more like this:
type ArrShell<const N: usize, T>= [Shell<T>; N];
There is this function that only needs the last element of every item in the ArrShell
. I'd like it to take a form that is easy to debug later on, meaning that the author of that function only has to see that he has received an array of references to the underlying type T
.
How can I do that?? I have tried the following:
use std::rc::Rc;
use std::cell::RefCell;
type Shell<T> = Rc<RefCell<Vec<T>>>;
type ArrShell<const N: usize, T>= [Shell<T>; N];
fn ideal_case(_a: &[&Unclonable; 2]){
todo!()
}
fn also_acceptable<'a, T: AsRef<[&'a Unclonable]>>(_a: T){
todo!()
}
struct Unclonable{
a: f64
}
fn main() {
let a = vec![Unclonable{a: 5.0}, Unclonable{a: 5.0}, Unclonable{a: 5.0}];
let b = vec![Unclonable{a: 5.0}, Unclonable{a: 5.0}, Unclonable{a: 5.0}];
let arr_of_shells: ArrShell<2, _> = [Rc::new(RefCell::new(a)), Rc::new(RefCell::new(b))];
let ref_vec = arr_of_shells.map(|e| e.borrow().last().unwrap());
ideal_case(&ref_vec);
also_acceptable(&ref_vec);
}
but I am logically getting an error in the map
, as I am trying to return an internal reference to the closure.
let ref_vec = arr_of_shells.map(|e| e.borrow().last().unwrap());
| ----------^^^^^^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| temporary value created here
Is there any way to do what I want?
I feel there should be a way, but I haven't been able to make it work.