Oh Map on non-copy option in const context mentions using:
const fn map_wrapper<T>(option: Option<T>) -> Option<Wrapper<T>> {
match option {
// call unwrap() instead of destructuring
Some(_) => Some(Wrapper(option.unwrap())),
// call forget() instead of dropping
option @ None => {
mem::forget(option);
None
}
}
}
So we can avoid using unsafe:
const fn option_manually_drop<T>(value: Option<T>) -> Option<std::mem::ManuallyDrop<T>> {
if value.is_some() {
Some(std::mem::ManuallyDrop::new(value.unwrap()))
} else {
std::mem::forget(value);
None
}
}
const fn f<T>(slot: &mut MaybeUninit<T>, value: Option<T>) {
if let Some(value) = option_manually_drop(value) {
let value = std::mem::ManuallyDrop::into_inner(value);
// use value somehow
slot.write(value);
}
}