I have an enumeration where multiple variants contain data I'd prefer not to clone. Occasionally, I want to switch between these variants, and not clone the data in them.
In essence, what I want to do is the following:
fn main() {
let mut a = Data::One(DoNotClone {
i: 1
});
move_data(&mut a);
}
struct DoNotClone {
i: usize
}
enum Data {
One(DoNotClone),
Two(DoNotClone)
}
fn move_data(data: &mut Data) {
if let Data::One(donotclone) = data {
// I cannot create Data::Two because I don't own donotclone here
*data = Data::Two(donotclone)
}
}
This kind of operation isn't fundamentally impossible given ownership rules, but perhaps the borrow checker is unable to handle such a scenario.
I'd like to avoid using unsafe, or cloning the data. Is this possible?