about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2015-06-20 12:00:29 -0400
committerSteve Klabnik <steve@steveklabnik.com>2015-06-20 14:18:28 -0400
commit24c1e109cc36d198c010722c80bb2b06efacabe7 (patch)
tree7a9a40b0d1b5a503ef054f7f0b7bb7ea34709702 /src
parent4b42cbd5eb3e947875aa427dbda52121ef186586 (diff)
downloadrust-24c1e109cc36d198c010722c80bb2b06efacabe7.tar.gz
rust-24c1e109cc36d198c010722c80bb2b06efacabe7.zip
TRPL: FFI: address panics
Fixes #26443
Diffstat (limited to 'src')
-rw-r--r--src/doc/trpl/ffi.md34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/doc/trpl/ffi.md b/src/doc/trpl/ffi.md
index 54e850c051e..917d8dbe196 100644
--- a/src/doc/trpl/ffi.md
+++ b/src/doc/trpl/ffi.md
@@ -530,3 +530,37 @@ The `extern` makes this function adhere to the C calling convention, as
 discussed above in "[Foreign Calling
 Conventions](ffi.html#foreign-calling-conventions)". The `no_mangle`
 attribute turns off Rust's name mangling, so that it is easier to link to.
+
+# FFI and panics
+
+It’s important to be mindful of `panic!`s when working with FFI. This code,
+when called from C, will `abort`:
+
+```rust
+#[no_mangle]
+pub extern fn oh_no() -> ! {
+    panic!("Oops!");
+}
+# fn main() {}
+```
+
+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:
+
+```rust
+use std::thread;
+
+#[no_mangle]
+pub extern fn oh_no() -> i32 {
+    let h = thread::spawn(|| {
+        panic!("Oops!");
+    });
+
+    match h.join() {
+        Ok(_) => 1,
+        Err(_) => 0,
+    }
+}
+# fn main() {}
+```
+