5
5
This guide will use the [ snappy] ( https://github.com/google/snappy )
6
6
compression/decompression library as an introduction to writing bindings for
7
7
foreign code. Rust is currently unable to call directly into a C++ library, but
8
- snappy includes a C interface (documented in
9
- [ ` snappy-c.h ` ] ( https://github.com/google/snappy/blob/master/snappy-c.h ) ).
8
+ snappy includes a C interface (documented in [ ` snappy-c.h ` ] [ snappy-header ] ).
9
+
10
+ [ snappy-header ] : https://github.com/google/snappy/blob/master/snappy-c.h
11
+
12
+ Often when writing these bindings, types and functions from the C standard
13
+ library will be necessary. These can be found in the
14
+ [ libc crate on crates.io] [ libc ] , which can be accessed in a Cargo project
15
+ by [ adding it as a dependency] [ cargo-add ] . (Note that if you click the examples
16
+ here to load them in the [ playground] ( https://play.rust-lang.org ) , which doesn't
17
+ support Cargo, you'll see extra lines of code to keep them compiling while
18
+ remaining self-contained... but in your own projects you should use Cargo.)
19
+
20
+ [ cargo-add ] : http://doc.crates.io/guide.html#adding-a-dependency
10
21
11
22
The following is a minimal example of calling a foreign function which will
12
23
compile if snappy is installed:
@@ -23,7 +34,7 @@ extern {
23
34
}
24
35
25
36
# #[cfg(not(nope))] unsafe fn snappy_max_compressed_length(_: size_t) -> size_t { 0 }
26
-
37
+ #
27
38
fn main() {
28
39
let x = unsafe { snappy_max_compressed_length(100) };
29
40
println!("max compressed length of a 100 byte buffer: {}", x);
0 commit comments