Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Created January 24, 2021 10:07
Show Gist options
  • Save justanotherdot/3004afbe41116575b7fbe5dc72f19a38 to your computer and use it in GitHub Desktop.
Save justanotherdot/3004afbe41116575b7fbe5dc72f19a38 to your computer and use it in GitHub Desktop.
use libc;
#[derive(Debug)]
#[repr(C)]
struct Resource(i32);
impl Drop for Resource {
fn drop(&mut self) {
println!("goodbye from {}", self.0);
}
}
fn foo() -> Result<(), ()> {
return Err(());
}
//#[no_mangle]
extern "C" fn cleanup() {
println!("doing cleanup stuff");
}
fn main() {
let x = Resource(0);
println!("about to terminate the process");
unsafe { libc::atexit(cleanup); };
foo().unwrap_or_else(|_| {
// performs no cleanup.
std::process::exit(1);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment