@@ -11,16 +11,14 @@ snappy includes a C interface (documented in
11
11
The following is a minimal example of calling a foreign function which will
12
12
compile if snappy is installed:
13
13
14
- ~~~~
14
+ ~~~~ no_run
15
15
extern crate libc;
16
16
use libc::size_t;
17
17
18
18
#[link(name = "snappy")]
19
- # #[cfg(ignore_this)]
20
19
extern {
21
20
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
22
21
}
23
- # unsafe fn snappy_max_compressed_length(a: size_t) -> size_t { a }
24
22
25
23
fn main() {
26
24
let x = unsafe { snappy_max_compressed_length(100) };
@@ -46,7 +44,7 @@ keeping the binding correct at runtime.
46
44
47
45
The ` extern ` block can be extended to cover the entire snappy API:
48
46
49
- ~~~~ {.ignore}
47
+ ~~~~ no_run
50
48
extern crate libc;
51
49
use libc::{c_int, size_t};
52
50
@@ -67,6 +65,7 @@ extern {
67
65
fn snappy_validate_compressed_buffer(compressed: *u8,
68
66
compressed_length: size_t) -> c_int;
69
67
}
68
+ # fn main() {}
70
69
~~~~
71
70
72
71
# Creating a safe interface
@@ -209,19 +208,16 @@ A basic example is:
209
208
210
209
Rust code:
211
210
212
- ~~~~
211
+ ~~~~ no_run
213
212
extern fn callback(a:i32) {
214
213
println!("I'm called from C with value {0}", a);
215
214
}
216
215
217
216
#[link(name = "extlib")]
218
- # #[cfg(ignore)]
219
217
extern {
220
218
fn register_callback(cb: extern fn(i32)) -> i32;
221
219
fn trigger_callback();
222
220
}
223
- # unsafe fn register_callback(cb: extern fn(i32)) -> i32 { 0 }
224
- # unsafe fn trigger_callback() { }
225
221
226
222
fn main() {
227
223
unsafe {
@@ -265,7 +261,7 @@ referenced Rust object.
265
261
266
262
Rust code:
267
263
268
- ~~~~
264
+ ~~~~ no_run
269
265
270
266
struct RustObject {
271
267
a: i32,
@@ -281,15 +277,11 @@ extern fn callback(target: *mut RustObject, a:i32) {
281
277
}
282
278
283
279
#[link(name = "extlib")]
284
- # #[cfg(ignore)]
285
280
extern {
286
281
fn register_callback(target: *mut RustObject,
287
282
cb: extern fn(*mut RustObject, i32)) -> i32;
288
283
fn trigger_callback();
289
284
}
290
- # unsafe fn register_callback(a: *mut RustObject,
291
- # b: extern fn(*mut RustObject, i32)) -> i32 { 0 }
292
- # unsafe fn trigger_callback() {}
293
285
294
286
fn main() {
295
287
// Create the object that will be referenced in the callback
@@ -398,9 +390,12 @@ the `link_args` attribute. This attribute is applied to `extern` blocks and
398
390
specifies raw flags which need to get passed to the linker when producing an
399
391
artifact. An example usage would be:
400
392
401
- ~~~ {.ignore}
393
+ ~~~ no_run
394
+ #![feature(link_args)]
395
+
402
396
#[link_args = "-foo -bar -baz"]
403
397
extern {}
398
+ # fn main() {}
404
399
~~~
405
400
406
401
Note that this feature is currently hidden behind the ` feature(link_args) ` gate
@@ -434,15 +429,13 @@ Foreign APIs often export a global variable which could do something like track
434
429
global state. In order to access these variables, you declare them in ` extern `
435
430
blocks with the ` static ` keyword:
436
431
437
- ~~~
432
+ ~~~ no_run
438
433
extern crate libc;
439
434
440
435
#[link(name = "readline")]
441
- # #[cfg(ignore)]
442
436
extern {
443
437
static rl_readline_version: libc::c_int;
444
438
}
445
- # static rl_readline_version: libc::c_int = 0;
446
439
447
440
fn main() {
448
441
println!("You have readline version {} installed.",
@@ -454,16 +447,14 @@ Alternatively, you may need to alter global state provided by a foreign
454
447
interface. To do this, statics can be declared with ` mut ` so rust can mutate
455
448
them.
456
449
457
- ~~~
450
+ ~~~ no_run
458
451
extern crate libc;
459
452
use std::ptr;
460
453
461
454
#[link(name = "readline")]
462
- # #[cfg(ignore)]
463
455
extern {
464
456
static mut rl_prompt: *libc::c_char;
465
457
}
466
- # static mut rl_prompt: *libc::c_char = 0 as *libc::c_char;
467
458
468
459
fn main() {
469
460
"[my-awesome-shell] $".with_c_str(|buf| {
@@ -488,7 +479,6 @@ extern crate libc;
488
479
extern "stdcall" {
489
480
fn SetEnvironmentVariableA(n: *u8, v: *u8) -> libc::c_int;
490
481
}
491
-
492
482
# fn main() { }
493
483
~~~~
494
484
0 commit comments