@@ -31,7 +31,7 @@ compile if snappy is installed:
31
31
use libc::size_t;
32
32
33
33
#[link(name = "snappy")]
34
- extern {
34
+ unsafe extern "C" {
35
35
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
36
36
}
37
37
@@ -64,7 +64,7 @@ The `extern` block can be extended to cover the entire snappy API:
64
64
use libc::{c_int, size_t};
65
65
66
66
#[link(name = "snappy")]
67
- extern {
67
+ unsafe extern {
68
68
fn snappy_compress(input: *const u8,
69
69
input_length: size_t,
70
70
compressed: *mut u8,
@@ -251,7 +251,7 @@ First, we assume you have a lib crate named as `rust_from_c`.
251
251
` lib.rs ` should have Rust code as following:
252
252
253
253
``` rust
254
- #[no_mangle]
254
+ #[unsafe ( no_mangle) ]
255
255
pub extern " C" fn hello_from_rust () {
256
256
println! (" Hello from Rust!" );
257
257
}
@@ -331,7 +331,7 @@ extern fn callback(a: i32) {
331
331
}
332
332
333
333
#[link(name = "extlib")]
334
- extern {
334
+ unsafe extern {
335
335
fn register_callback(cb: extern fn(i32)) -> i32;
336
336
fn trigger_callback();
337
337
}
@@ -383,7 +383,7 @@ struct RustObject {
383
383
// Other members...
384
384
}
385
385
386
- extern "C" fn callback(target: *mut RustObject, a: i32) {
386
+ unsafe extern "C" fn callback(target: *mut RustObject, a: i32) {
387
387
println!("I'm called from C with value {0}", a);
388
388
unsafe {
389
389
// 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) {
392
392
}
393
393
394
394
#[link(name = "extlib")]
395
- extern {
395
+ unsafe extern {
396
396
fn register_callback(target: *mut RustObject,
397
- cb: extern fn(*mut RustObject, i32)) -> i32;
397
+ cb: unsafe extern fn(*mut RustObject, i32)) -> i32;
398
398
fn trigger_callback();
399
399
}
400
400
@@ -523,7 +523,7 @@ blocks with the `static` keyword:
523
523
<!-- ignore: requires libc crate -->
524
524
``` rust,ignore
525
525
#[link(name = "readline")]
526
- extern {
526
+ unsafe extern {
527
527
static rl_readline_version: libc::c_int;
528
528
}
529
529
@@ -543,7 +543,7 @@ use std::ffi::CString;
543
543
use std::ptr;
544
544
545
545
#[link(name = "readline")]
546
- extern {
546
+ unsafe extern {
547
547
static mut rl_prompt: *const libc::c_char;
548
548
}
549
549
@@ -573,7 +573,7 @@ conventions. Rust provides a way to tell the compiler which convention to use:
573
573
#[cfg(all(target_os = "win32", target_arch = "x86"))]
574
574
#[link(name = "kernel32")]
575
575
#[allow(non_snake_case)]
576
- extern "stdcall" {
576
+ unsafe extern "stdcall" {
577
577
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
578
578
}
579
579
# fn main() { }
@@ -635,7 +635,7 @@ In C, functions can be 'variadic', meaning they accept a variable number of argu
635
635
be achieved in Rust by specifying ` ... ` within the argument list of a foreign function declaration:
636
636
637
637
``` no_run
638
- extern {
638
+ unsafe extern {
639
639
fn foo(x: i32, ...);
640
640
}
641
641
@@ -685,7 +685,7 @@ we have function pointers flying across the FFI boundary in both directions.
685
685
use libc::c_int;
686
686
687
687
# #[cfg(hidden)]
688
- extern "C" {
688
+ unsafe extern "C" {
689
689
/// Registers the callback.
690
690
fn register(cb: Option<extern "C" fn(Option<extern "C" fn(c_int) -> c_int>, c_int) -> c_int>);
691
691
}
@@ -750,8 +750,8 @@ mechanisms (notably C++'s `try`/`catch`).
750
750
751
751
<!-- ignore: using unstable feature -->
752
752
```rust,ignore
753
- #[no_mangle]
754
- extern "C-unwind" fn example() {
753
+ #[unsafe( no_mangle) ]
754
+ unsafe extern "C-unwind" fn example() {
755
755
panic!("Uh oh");
756
756
}
757
757
```
@@ -780,13 +780,13 @@ If the C++ frames have objects, their destructors will be called.
780
780
<!-- ignore: using unstable feature -->
781
781
``` rust,ignore
782
782
#[link(...)]
783
- extern "C-unwind" {
783
+ unsafe extern "C-unwind" {
784
784
// A C++ function that may throw an exception
785
785
fn may_throw();
786
786
}
787
787
788
- #[no_mangle]
789
- extern "C-unwind" fn rust_passthrough() {
788
+ #[unsafe( no_mangle) ]
789
+ unsafe extern "C-unwind" fn rust_passthrough() {
790
790
let b = Box::new(5);
791
791
unsafe { may_throw(); }
792
792
println!("{:?}", &b);
@@ -816,7 +816,7 @@ will be printed.
816
816
### ` panic ` can be stopped at an ABI boundary
817
817
818
818
``` rust
819
- #[no_mangle]
819
+ #[unsafe ( no_mangle) ]
820
820
extern " C" fn assert_nonzero (input : u32 ) {
821
821
assert! (input != 0 )
822
822
}
@@ -833,7 +833,7 @@ process if it panics, you must use [`catch_unwind`]:
833
833
``` rust
834
834
use std :: panic :: catch_unwind;
835
835
836
- #[no_mangle]
836
+ #[unsafe ( no_mangle) ]
837
837
pub extern " C" fn oh_no () -> i32 {
838
838
let result = catch_unwind (|| {
839
839
panic! (" Oops!" );
@@ -867,7 +867,7 @@ We can represent this in Rust with the `c_void` type:
867
867
868
868
<!-- ignore: requires libc crate -->
869
869
```rust,ignore
870
- extern "C" {
870
+ unsafe extern "C" {
871
871
pub fn foo(arg: *mut libc::c_void);
872
872
pub fn bar(arg: *mut libc::c_void);
873
873
}
@@ -902,7 +902,7 @@ pub struct Bar {
902
902
core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
903
903
}
904
904
905
- extern "C" {
905
+ unsafe extern "C" {
906
906
pub fn foo(arg: *mut Foo);
907
907
pub fn bar(arg: *mut Bar);
908
908
}
0 commit comments