Why is there no std::mem::update?
Akin to Cell::<T>::update(), my hypothetical std::mem::update would have the signature fn update<T>(dest: &mut T, f: impl FnOnce(T) -> T). I’ve come up with an implementation as follows:
pub fn update<T>(dest: &mut T, f: impl FnOnce(T) -> T) {
let dest = dest as *mut T;
unsafe { dest.write(f(dest.read())); }
}
Is this function safe? If so, why doesn’t the standard library offer an in-place update function already? Seems like it would be highly useful for struct members to avoid Option-based constructs like self.member = Some(f(self.member.take().unwrap())).