1 //! A test for calling `C-unwind` functions across foreign function boundaries.
3 //! This test triggers a panic in a Rust library that our foreign function invokes. This shows
4 //! that we can unwind through the C code in that library, and catch the underlying panic.
7 use std::panic::{catch_unwind, AssertUnwindSafe};
10 // Call `add_small_numbers`, passing arguments that will NOT trigger a panic.
12 let c = unsafe { add_small_numbers(a, b) };
15 // Call `add_small_numbers`, passing arguments that will trigger a panic, and catch it.
16 let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
18 let _c = unsafe { add_small_numbers(a, b) };
19 unreachable!("should have unwound instead of returned");
22 // Assert that we did indeed panic, then unwrap and downcast the panic into the sum.
23 assert!(caught_unwind.is_err());
24 let panic_obj = caught_unwind.unwrap_err();
25 let msg = panic_obj.downcast_ref::<String>().unwrap();
26 assert_eq!(msg, "11");
29 #[link(name = "add", kind = "static")]
31 /// An external function, defined in C.
33 /// Returns the sum of two numbers, or panics if the sum is greater than 10.
34 fn add_small_numbers(a: u32, b: u32) -> u32;