I have this enum:
enum VecP<'a> {
Imut(&'a Vec<usize>),
Mut(&'a mut Vec<usize>)
}
I want to pass it to a function and later to be able to use it again (or use it in a loop)
The function I want to pass it to accepts a VecP
and I can't change it to accept a pointer.
If I were to do this:
let vp = VecP::Imut(&v);
func(vp); // ownership is given here and I can't use it anymore
The solution I've come up with is to do:
match vp {
VecP::Mut(v) => {
func(VecP::Mut(v)); // I don't loose ownership to v cuz it's a pointer
vp = VecP::Mut(v); // because of the reassignment I can continue to use `vp`
}
VecP::Imut(v) => {
func(VecP::Imut(v));
vp = VecP::Imut(v);
}
}
However this is somewhat tedious, (especailly if the func call is more complicated or there are more options to the enum) I was wondering if there is a better way, maybe something like a clone method?
Best I could think of is to write a macro to generate this code for me but that seems like overkill