Created
January 24, 2021 10:07
-
-
Save justanotherdot/3004afbe41116575b7fbe5dc72f19a38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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