// this code is not allowed
self.data = self.data;
// case where an owned value is required, because data is not clone (or copy)
self.data = match self.data {
Data::One(v) => v.transform_into_two(),
Data::Two(v) => v.transform_into_one(),
};
Solution?
fn update_value<T>(value: &mut T, update: impl FnOnce(T) -> T) {
unsafe {
let v = std::ptr::read(value);
let v = update(v);
std::ptr::write(value, v);
}
}
update_value(&mut self.data ,|data| match data {
Data::One(v) => v.transform_into_two(),
Data::Two(v) => v.transform_into_one(),
});
Questions
Is this sound?
Is there a standard library function to solve the problem?