Skip to content

Instantly share code, notes, and snippets.

@retep998
Created October 16, 2016 05:42
Show Gist options
  • Save retep998/b9689de1e810ea40ec61ad0e6fb57a02 to your computer and use it in GitHub Desktop.
Save retep998/b9689de1e810ea40ec61ad0e6fb57a02 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
struct Bar {
~Bar() {
cout << "C++ destructor is firing" << endl;
}
};
extern "C" {
void foo();
void bar() {
cout << "C++ creating object with destructor" << endl;
Bar bar;
foo();
}
}
use std::panic::catch_unwind;
extern "C" {
fn bar();
}
#[no_mangle]
pub extern "C" fn foo() {
panic!("Rust is panicking");
}
fn main() {
println!("Rust main function");
println!("Caught Rust panic: {:?}", catch_unwind(|| unsafe { bar() } ));
}
> cl /c bar.cpp /EHs /Zi; rustc -g foo.rs -Clink-arg="bar.obj"; ./foo.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
bar.cpp
Rust main function
C++ creating object with destructor
thread 'main' panicked at 'Rust is panicking', foo.rs:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
C++ destructor is firing
Caught Rust panic: Err(Any)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment