use std::mem::MaybeUninit; use crate::{abi_backing::HeapValueBacking, FfiValue}; pub trait HeapPlacable { unsafe extern "C" fn free(value: FfiValue, taken: bool); } impl HeapPlacable for T { unsafe extern "C" fn free(value: FfiValue, taken: bool) { if !taken { drop(Box::from_raw(value.inner as *mut HeapValueBacking)) } else { let allocation = Box::from_raw(value.inner as *mut T); // Since we "took" the value, kindly inform the compiler that it can't // treat the value like it exists let allocation_uninit: Box>> = unsafe { core::mem::transmute(allocation) }; // Since the compiler has no idea whether the value exists or not, it won't try and // drop it. Success! drop(allocation_uninit); } } }