about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCobrand <cobrandw@gmail.com>2016-12-07 18:43:07 +0100
committerGitHub <noreply@github.com>2016-12-07 18:43:07 +0100
commit614b74c24bb80e17230e58a74ef5a4725972f84a (patch)
treec3474085fb857ac901f1f34f7e0bd9d1f69883f4
parent7846610470392abc3ab1470853bbe7b408fe4254 (diff)
downloadrust-614b74c24bb80e17230e58a74ef5a4725972f84a.tar.gz
rust-614b74c24bb80e17230e58a74ef5a4725972f84a.zip
Update book/ffi to use catch_unwind
r? @GuillaumeGomez

The doc mentioned to spawn a new thread instead of using catch_unwind, which has been the recommended way to catch panics for foreign function interfaces for a few releases now.
-rw-r--r--src/doc/book/ffi.md23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/doc/book/ffi.md b/src/doc/book/ffi.md
index 7510cd0b3b5..b53af694428 100644
--- a/src/doc/book/ffi.md
+++ b/src/doc/book/ffi.md
@@ -662,26 +662,31 @@ attribute turns off Rust's name mangling, so that it is easier to link to.
 
 It’s important to be mindful of `panic!`s when working with FFI. A `panic!`
 across an FFI boundary is undefined behavior. If you’re writing code that may
-panic, you should run it in another thread, so that the panic doesn’t bubble up
-to C:
+panic, you should run it in a closure with [`catch_unwind()`]:
 
 ```rust
-use std::thread;
+use std::panic::catch_unwind;
 
 #[no_mangle]
 pub extern fn oh_no() -> i32 {
-    let h = thread::spawn(|| {
+    let result = catch_unwind(|| {
         panic!("Oops!");
     });
-
-    match h.join() {
-        Ok(_) => 1,
-        Err(_) => 0,
+    match result {
+        Ok(_) => 0,
+        Err(_) => 1,
     }
 }
-# fn main() {}
+
+fn main() {}
 ```
 
+Please note that [`catch_unwind()`] will only catch unwinding panics, not
+those who abort the process. See the documentation of [`catch_unwind()`]
+for more information.
+
+[`catch_unwind()`]: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
+
 # Representing opaque structs
 
 Sometimes, a C library wants to provide a pointer to something, but not let you