@@ -22,15 +22,18 @@ remaining self-contained... but in your own projects you should use Cargo.)
22
22
The following is a minimal example of calling a foreign function which will
23
23
compile if snappy is installed:
24
24
25
- ``` no_run
25
+ ``` rust
26
26
# #![feature(libc)]
27
27
extern crate libc;
28
28
use libc :: size_t;
29
29
30
+ # /*
30
31
#[link(name = "snappy")]
31
32
extern {
32
33
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
33
34
}
35
+ # */
36
+ # unsafe fn snappy_max_compressed_length (_ : size_t ) -> size_t { 0 }
34
37
35
38
fn main () {
36
39
let x = unsafe { snappy_max_compressed_length (100 ) };
@@ -56,12 +59,14 @@ keeping the binding correct at runtime.
56
59
57
60
The ` extern ` block can be extended to cover the entire snappy API:
58
61
59
- ``` no_run
62
+ ``` rust
60
63
# #![feature(libc)]
61
64
extern crate libc;
62
65
use libc :: {c_int, size_t};
63
66
67
+ # /*
64
68
#[link(name = "snappy")]
69
+ # */
65
70
extern {
66
71
fn snappy_compress (input : * const u8 ,
67
72
input_length : size_t ,
@@ -203,16 +208,21 @@ A basic example is:
203
208
204
209
Rust code:
205
210
206
- ``` no_run
211
+ ``` rust
207
212
extern fn callback (a : i32 ) {
208
213
println! (" I'm called from C with value {0}" , a );
209
214
}
210
215
216
+ # /*
211
217
#[link(name = "extlib")]
212
218
extern {
213
219
fn register_callback(cb: extern fn(i32)) -> i32;
214
220
fn trigger_callback();
215
221
}
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 ); }
216
226
217
227
fn main () {
218
228
unsafe {
@@ -256,7 +266,7 @@ referenced Rust object.
256
266
257
267
Rust code:
258
268
259
- ```no_run
269
+ ```rust
260
270
#[repr(C)]
261
271
struct RustObject {
262
272
a: i32,
@@ -271,12 +281,17 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
271
281
}
272
282
}
273
283
284
+ # /*
274
285
#[link(name = "extlib")]
275
286
extern {
276
287
fn register_callback(target: *mut RustObject,
277
288
cb: extern fn(*mut RustObject, i32)) -> i32;
278
289
fn trigger_callback();
279
290
}
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); }
280
295
281
296
fn main() {
282
297
// Create the object that will be referenced in the callback
@@ -390,6 +405,7 @@ this:
390
405
391
406
```rust
392
407
unsafe fn kaboom(ptr: *const i32) -> i32 { *ptr }
408
+ # fn main() {}
393
409
```
394
410
395
411
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
400
416
global state. In order to access these variables, you declare them in ` extern `
401
417
blocks with the ` static ` keyword:
402
418
403
- ``` no_run
419
+ ``` rust
404
420
# #![feature(libc)]
405
421
extern crate libc;
406
422
@@ -419,7 +435,7 @@ Alternatively, you may need to alter global state provided by a foreign
419
435
interface. To do this, statics can be declared with ` mut ` so we can mutate
420
436
them.
421
437
422
- ``` no_run
438
+ ``` rust
423
439
# #![feature(libc)]
424
440
extern crate libc;
425
441
0 commit comments