@@ -31,7 +31,7 @@ compile if snappy is installed:
3131use libc::size_t;
3232
3333#[link(name = "snappy")]
34- extern {
34+ unsafe extern "C" {
3535 fn snappy_max_compressed_length(source_length: size_t) -> size_t;
3636}
3737
@@ -64,7 +64,7 @@ The `extern` block can be extended to cover the entire snappy API:
6464use libc::{c_int, size_t};
6565
6666#[link(name = "snappy")]
67- extern {
67+ unsafe extern {
6868 fn snappy_compress(input: *const u8,
6969 input_length: size_t,
7070 compressed: *mut u8,
@@ -251,7 +251,7 @@ First, we assume you have a lib crate named as `rust_from_c`.
251251` lib.rs ` should have Rust code as following:
252252
253253``` rust
254- #[no_mangle]
254+ #[unsafe ( no_mangle) ]
255255pub extern " C" fn hello_from_rust () {
256256 println! (" Hello from Rust!" );
257257}
@@ -331,7 +331,7 @@ extern fn callback(a: i32) {
331331}
332332
333333#[link(name = "extlib")]
334- extern {
334+ unsafe extern {
335335 fn register_callback(cb: extern fn(i32)) -> i32;
336336 fn trigger_callback();
337337}
@@ -383,7 +383,7 @@ struct RustObject {
383383 // Other members...
384384}
385385
386- extern "C" fn callback(target: *mut RustObject, a: i32) {
386+ unsafe extern "C" fn callback(target: *mut RustObject, a: i32) {
387387 println!("I'm called from C with value {0}", a);
388388 unsafe {
389389 // Update the value in RustObject with the value received from the callback:
@@ -392,9 +392,9 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
392392}
393393
394394#[link(name = "extlib")]
395- extern {
395+ unsafe extern {
396396 fn register_callback(target: *mut RustObject,
397- cb: extern fn(*mut RustObject, i32)) -> i32;
397+ cb: unsafe extern fn(*mut RustObject, i32)) -> i32;
398398 fn trigger_callback();
399399}
400400
@@ -523,7 +523,7 @@ blocks with the `static` keyword:
523523<!-- ignore: requires libc crate -->
524524``` rust,ignore
525525#[link(name = "readline")]
526- extern {
526+ unsafe extern {
527527 static rl_readline_version: libc::c_int;
528528}
529529
@@ -543,7 +543,7 @@ use std::ffi::CString;
543543use std::ptr;
544544
545545#[link(name = "readline")]
546- extern {
546+ unsafe extern {
547547 static mut rl_prompt: *const libc::c_char;
548548}
549549
@@ -573,7 +573,7 @@ conventions. Rust provides a way to tell the compiler which convention to use:
573573#[cfg(all(target_os = "win32", target_arch = "x86"))]
574574#[link(name = "kernel32")]
575575#[allow(non_snake_case)]
576- extern "stdcall" {
576+ unsafe extern "stdcall" {
577577 fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
578578}
579579# fn main() { }
@@ -635,7 +635,7 @@ In C, functions can be 'variadic', meaning they accept a variable number of argu
635635be achieved in Rust by specifying ` ... ` within the argument list of a foreign function declaration:
636636
637637``` no_run
638- extern {
638+ unsafe extern {
639639 fn foo(x: i32, ...);
640640}
641641
@@ -685,7 +685,7 @@ we have function pointers flying across the FFI boundary in both directions.
685685use libc::c_int;
686686
687687# #[cfg(hidden)]
688- extern "C" {
688+ unsafe extern "C" {
689689 /// Registers the callback.
690690 fn register(cb: Option<extern "C" fn(Option<extern "C" fn(c_int) -> c_int>, c_int) -> c_int>);
691691}
@@ -750,8 +750,8 @@ mechanisms (notably C++'s `try`/`catch`).
750750
751751<!-- ignore: using unstable feature -->
752752```rust,ignore
753- #[no_mangle]
754- extern "C-unwind" fn example() {
753+ #[unsafe( no_mangle) ]
754+ unsafe extern "C-unwind" fn example() {
755755 panic!("Uh oh");
756756}
757757```
@@ -780,13 +780,13 @@ If the C++ frames have objects, their destructors will be called.
780780<!-- ignore: using unstable feature -->
781781``` rust,ignore
782782#[link(...)]
783- extern "C-unwind" {
783+ unsafe extern "C-unwind" {
784784 // A C++ function that may throw an exception
785785 fn may_throw();
786786}
787787
788- #[no_mangle]
789- extern "C-unwind" fn rust_passthrough() {
788+ #[unsafe( no_mangle) ]
789+ unsafe extern "C-unwind" fn rust_passthrough() {
790790 let b = Box::new(5);
791791 unsafe { may_throw(); }
792792 println!("{:?}", &b);
@@ -816,7 +816,7 @@ will be printed.
816816### ` panic ` can be stopped at an ABI boundary
817817
818818``` rust
819- #[no_mangle]
819+ #[unsafe ( no_mangle) ]
820820extern " C" fn assert_nonzero (input : u32 ) {
821821 assert! (input != 0 )
822822}
@@ -833,7 +833,7 @@ process if it panics, you must use [`catch_unwind`]:
833833``` rust
834834use std :: panic :: catch_unwind;
835835
836- #[no_mangle]
836+ #[unsafe ( no_mangle) ]
837837pub extern " C" fn oh_no () -> i32 {
838838 let result = catch_unwind (|| {
839839 panic! (" Oops!" );
@@ -867,7 +867,7 @@ We can represent this in Rust with the `c_void` type:
867867
868868<!-- ignore: requires libc crate -->
869869```rust,ignore
870- extern "C" {
870+ unsafe extern "C" {
871871 pub fn foo(arg: *mut libc::c_void);
872872 pub fn bar(arg: *mut libc::c_void);
873873}
@@ -902,7 +902,7 @@ pub struct Bar {
902902 core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
903903}
904904
905- extern "C" {
905+ unsafe extern "C" {
906906 pub fn foo(arg: *mut Foo);
907907 pub fn bar(arg: *mut Bar);
908908}
0 commit comments