Skip to content

Commit 9454fd4

Browse files
committed
trpl: make FFI examples compile using comments and shims
1 parent 3715672 commit 9454fd4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/doc/trpl/ffi.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ remaining self-contained... but in your own projects you should use Cargo.)
2222
The following is a minimal example of calling a foreign function which will
2323
compile if snappy is installed:
2424

25-
```no_run
25+
```rust
2626
# #![feature(libc)]
2727
extern crate libc;
2828
use libc::size_t;
2929

30+
# /*
3031
#[link(name = "snappy")]
3132
extern {
3233
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
3334
}
35+
# */
36+
# unsafe fn snappy_max_compressed_length(_: size_t) -> size_t { 0 }
3437

3538
fn main() {
3639
let x = unsafe { snappy_max_compressed_length(100) };
@@ -56,12 +59,14 @@ keeping the binding correct at runtime.
5659

5760
The `extern` block can be extended to cover the entire snappy API:
5861

59-
```no_run
62+
```rust
6063
# #![feature(libc)]
6164
extern crate libc;
6265
use libc::{c_int, size_t};
6366

67+
# /*
6468
#[link(name = "snappy")]
69+
# */
6570
extern {
6671
fn snappy_compress(input: *const u8,
6772
input_length: size_t,
@@ -203,16 +208,21 @@ A basic example is:
203208

204209
Rust code:
205210

206-
```no_run
211+
```rust
207212
extern fn callback(a: i32) {
208213
println!("I'm called from C with value {0}", a);
209214
}
210215

216+
# /*
211217
#[link(name = "extlib")]
212218
extern {
213219
fn register_callback(cb: extern fn(i32)) -> i32;
214220
fn trigger_callback();
215221
}
222+
# */
223+
# static mut CALLBACK: Option<extern fn(i32)> = None;
224+
# unsafe fn register_callback(cb: extern fn(i32)) -> i32 { CALLBACK = Some(cb); 0 }
225+
# unsafe fn trigger_callback() { CALLBACK.unwrap()(7); }
216226

217227
fn main() {
218228
unsafe {
@@ -256,7 +266,7 @@ referenced Rust object.
256266
257267
Rust code:
258268
259-
```no_run
269+
```rust
260270
#[repr(C)]
261271
struct RustObject {
262272
a: i32,
@@ -271,12 +281,17 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
271281
}
272282
}
273283
284+
# /*
274285
#[link(name = "extlib")]
275286
extern {
276287
fn register_callback(target: *mut RustObject,
277288
cb: extern fn(*mut RustObject, i32)) -> i32;
278289
fn trigger_callback();
279290
}
291+
# */
292+
# static mut CALLBACK: Option<(*mut RustObject, extern fn(*mut RustObject, i32))> = None;
293+
# unsafe fn register_callback(target: *mut RustObject, cb: extern fn(*mut RustObject, i32)) -> i32 { CALLBACK = Some((target, cb)); 0 }
294+
# unsafe fn trigger_callback() { let (target, cb) = CALLBACK.unwrap(); cb(target, 7); }
280295
281296
fn main() {
282297
// Create the object that will be referenced in the callback
@@ -390,6 +405,7 @@ this:
390405
391406
```rust
392407
unsafe fn kaboom(ptr: *const i32) -> i32 { *ptr }
408+
# fn main() {}
393409
```
394410

395411
This function can only be called from an `unsafe` block or another `unsafe` function.
@@ -400,7 +416,7 @@ Foreign APIs often export a global variable which could do something like track
400416
global state. In order to access these variables, you declare them in `extern`
401417
blocks with the `static` keyword:
402418

403-
```no_run
419+
```rust
404420
# #![feature(libc)]
405421
extern crate libc;
406422

@@ -419,7 +435,7 @@ Alternatively, you may need to alter global state provided by a foreign
419435
interface. To do this, statics can be declared with `mut` so we can mutate
420436
them.
421437

422-
```no_run
438+
```rust
423439
# #![feature(libc)]
424440
extern crate libc;
425441

0 commit comments

Comments
 (0)