I do not have time to implement it right now (I say as I struggle to prevent myself from going and implementing it), but a #[derive(AfterDrop)]
could be set up for this. Given that a safe abstraction can be built without language support, getting it into the stdlib is going to be an uphill battle if you want to fight for it; the Rust stdlib prefers to stick to fundamental abstractions.
#[derive(AfterDrop)]
struct Guard {
#[after_drop]
resource: ManuallyDrop<Resource>,
trivial: Trivial,
}
// derived
impl Drop for Guard {
fn drop(&mut self) {
let mut resource;
unsafe {
resource = mem::uninitialized();
ptr::copy_nonoverlapping(&*self.resource, &mut resource, 1);
}
Self::after_drop(resource);
}
}
You could probably use frunk
's (Labelled
)Generic
to make AfterDrop<HList![..]>
a trait, but it seems unnecessary. You could make the trait AfterDrop<(..,)>
if you really want it to be a trait; the derive macro can construct the flat tuple (wheras HList
is a cons-list tuple).