@@ -11,7 +11,7 @@ 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
- ~~~~ no_run
14
+ ``` no_run
15
15
extern crate libc;
16
16
use libc::size_t;
17
17
@@ -24,7 +24,7 @@ fn main() {
24
24
let x = unsafe { snappy_max_compressed_length(100) };
25
25
println!("max compressed length of a 100 byte buffer: {}", x);
26
26
}
27
- ~~~~
27
+ ```
28
28
29
29
The ` extern ` block is a list of function signatures in a foreign library, in
30
30
this case with the platform's C ABI. The ` #[link(...)] ` attribute is used to
@@ -44,7 +44,7 @@ keeping the binding correct at runtime.
44
44
45
45
The ` extern ` block can be extended to cover the entire snappy API:
46
46
47
- ~~~~ no_run
47
+ ``` no_run
48
48
extern crate libc;
49
49
use libc::{c_int, size_t};
50
50
@@ -66,7 +66,7 @@ extern {
66
66
compressed_length: size_t) -> c_int;
67
67
}
68
68
# fn main() {}
69
- ~~~~
69
+ ```
70
70
71
71
# Creating a safe interface
72
72
@@ -79,7 +79,7 @@ vectors as pointers to memory. Rust's vectors are guaranteed to be a contiguous
79
79
length is number of elements currently contained, and the capacity is the total size in elements of
80
80
the allocated memory. The length is less than or equal to the capacity.
81
81
82
- ~~~~
82
+ ```
83
83
# extern crate libc;
84
84
# use libc::{c_int, size_t};
85
85
# unsafe fn snappy_validate_compressed_buffer(_: *const u8, _: size_t) -> c_int { 0 }
@@ -89,7 +89,7 @@ pub fn validate_compressed_buffer(src: &[u8]) -> bool {
89
89
snappy_validate_compressed_buffer(src.as_ptr(), src.len() as size_t) == 0
90
90
}
91
91
}
92
- ~~~~
92
+ ```
93
93
94
94
The ` validate_compressed_buffer ` wrapper above makes use of an ` unsafe ` block, but it makes the
95
95
guarantee that calling it is safe for all inputs by leaving off ` unsafe ` from the function
@@ -103,7 +103,7 @@ required capacity to hold the compressed output. The vector can then be passed t
103
103
` snappy_compress ` function as an output parameter. An output parameter is also passed to retrieve
104
104
the true length after compression for setting the length.
105
105
106
- ~~~~
106
+ ```
107
107
# extern crate libc;
108
108
# use libc::{size_t, c_int};
109
109
# unsafe fn snappy_compress(a: *const u8, b: size_t, c: *mut u8,
@@ -124,12 +124,12 @@ pub fn compress(src: &[u8]) -> Vec<u8> {
124
124
dst
125
125
}
126
126
}
127
- ~~~~
127
+ ```
128
128
129
129
Decompression is similar, because snappy stores the uncompressed size as part of the compression
130
130
format and ` snappy_uncompressed_length ` will retrieve the exact buffer size required.
131
131
132
- ~~~~
132
+ ```
133
133
# extern crate libc;
134
134
# use libc::{size_t, c_int};
135
135
# unsafe fn snappy_uncompress(compressed: *const u8,
@@ -159,7 +159,7 @@ pub fn uncompress(src: &[u8]) -> Option<Vec<u8>> {
159
159
}
160
160
}
161
161
}
162
- ~~~~
162
+ ```
163
163
164
164
For reference, the examples used here are also available as an [ library on
165
165
GitHub] ( https://github.com/thestinger/rust-snappy ) .
@@ -185,7 +185,7 @@ A basic example is:
185
185
186
186
Rust code:
187
187
188
- ~~~~ no_run
188
+ ``` no_run
189
189
extern fn callback(a: i32) {
190
190
println!("I'm called from C with value {0}", a);
191
191
}
@@ -202,11 +202,11 @@ fn main() {
202
202
trigger_callback(); // Triggers the callback
203
203
}
204
204
}
205
- ~~~~
205
+ ```
206
206
207
207
C code:
208
208
209
- ~~~~ c
209
+ ``` c
210
210
typedef void (* rust_callback)(int32_t);
211
211
rust_callback cb;
212
212
@@ -218,7 +218,7 @@ int32_t register_callback(rust_callback callback) {
218
218
void trigger_callback() {
219
219
cb(7); // Will call callback(7) in Rust
220
220
}
221
- ~~~~
221
+ ```
222
222
223
223
In this example Rust's `main()` will call `trigger_callback()` in C,
224
224
which would, in turn, call back to `callback()` in Rust.
@@ -238,7 +238,7 @@ referenced Rust object.
238
238
239
239
Rust code:
240
240
241
- ~~~~ no_run
241
+ ``` no_run
242
242
#[repr(C)]
243
243
struct RustObject {
244
244
a: i32,
@@ -269,11 +269,11 @@ fn main() {
269
269
trigger_callback();
270
270
}
271
271
}
272
- ~~~~
272
+ ```
273
273
274
274
C code:
275
275
276
- ~~~~ c
276
+ ``` c
277
277
typedef void (* rust_callback)(void* , int32_t);
278
278
void* cb_target;
279
279
rust_callback cb;
@@ -287,7 +287,7 @@ int32_t register_callback(void* callback_target, rust_callback callback) {
287
287
void trigger_callback() {
288
288
cb(cb_target, 7); // Will call callback(&rustObject, 7) in Rust
289
289
}
290
- ~~~~
290
+ ```
291
291
292
292
## Asynchronous callbacks
293
293
@@ -366,13 +366,13 @@ the `link_args` attribute. This attribute is applied to `extern` blocks and
366
366
specifies raw flags which need to get passed to the linker when producing an
367
367
artifact. An example usage would be:
368
368
369
- ~~~ no_run
369
+ ``` no_run
370
370
#![feature(link_args)]
371
371
372
372
#[link_args = "-foo -bar -baz"]
373
373
extern {}
374
374
# fn main() {}
375
- ~~~
375
+ ```
376
376
377
377
Note that this feature is currently hidden behind the ` feature(link_args) ` gate
378
378
because this is not a sanctioned way of performing linking. Right now rustc
@@ -393,9 +393,9 @@ the compiler that the unsafety does not leak out of the block.
393
393
Unsafe functions, on the other hand, advertise it to the world. An unsafe function is written like
394
394
this:
395
395
396
- ~~~~
396
+ ```
397
397
unsafe fn kaboom(ptr: *const int) -> int { *ptr }
398
- ~~~~
398
+ ```
399
399
400
400
This function can only be called from an ` unsafe ` block or another ` unsafe ` function.
401
401
@@ -405,7 +405,7 @@ Foreign APIs often export a global variable which could do something like track
405
405
global state. In order to access these variables, you declare them in ` extern `
406
406
blocks with the ` static ` keyword:
407
407
408
- ~~~ no_run
408
+ ``` no_run
409
409
extern crate libc;
410
410
411
411
#[link(name = "readline")]
@@ -417,13 +417,13 @@ fn main() {
417
417
println!("You have readline version {} installed.",
418
418
rl_readline_version as int);
419
419
}
420
- ~~~
420
+ ```
421
421
422
422
Alternatively, you may need to alter global state provided by a foreign
423
423
interface. To do this, statics can be declared with ` mut ` so rust can mutate
424
424
them.
425
425
426
- ~~~ no_run
426
+ ``` no_run
427
427
extern crate libc;
428
428
429
429
use std::ffi::CString;
@@ -440,15 +440,15 @@ fn main() {
440
440
// get a line, process it
441
441
unsafe { rl_prompt = ptr::null(); }
442
442
}
443
- ~~~
443
+ ```
444
444
445
445
# Foreign calling conventions
446
446
447
447
Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when
448
448
calling foreign functions. Some foreign functions, most notably the Windows API, use other calling
449
449
conventions. Rust provides a way to tell the compiler which convention to use:
450
450
451
- ~~~~
451
+ ```
452
452
extern crate libc;
453
453
454
454
#[cfg(all(target_os = "win32", target_arch = "x86"))]
@@ -458,7 +458,7 @@ extern "stdcall" {
458
458
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
459
459
}
460
460
# fn main() { }
461
- ~~~~
461
+ ```
462
462
463
463
This applies to the entire ` extern ` block. The list of supported ABI constraints
464
464
are:
@@ -518,3 +518,21 @@ with one of the non-nullable types, it is represented as a single pointer,
518
518
and the non-data variant is represented as the null pointer. So
519
519
` Option<extern "C" fn(c_int) -> c_int> ` is how one represents a nullable
520
520
function pointer using the C ABI.
521
+
522
+ # Calling Rust code from C
523
+
524
+ You may wish to compile Rust code in a way so that it can be called from C. This is
525
+ fairly easy, but requires a few things:
526
+
527
+ ```
528
+ #[no_mangle]
529
+ pub extern fn hello_rust() -> *const u8 {
530
+ "Hello, world!\0".as_ptr()
531
+ }
532
+ ```
533
+
534
+ The ` extern ` makes this function adhere to the C calling convention, as
535
+ discussed above in "[ Foreign Calling
536
+ Conventions] ( guide-ffi.html#foreign-calling-conventions ) ". The ` no_mangle `
537
+ attribute turns off Rust's name mangling, so that it is easier to link to.
538
+
0 commit comments