@@ -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
+ ~~~~ {.ignore}
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) };
@@ -80,11 +78,7 @@ vectors as pointers to memory. Rust's vectors are guaranteed to be a contiguous
80
78
length is number of elements currently contained, and the capacity is the total size in elements of
81
79
the allocated memory. The length is less than or equal to the capacity.
82
80
83
- ~~~~
84
- # extern crate libc;
85
- # use libc::{c_int, size_t};
86
- # unsafe fn snappy_validate_compressed_buffer(_: *u8, _: size_t) -> c_int { 0 }
87
- # fn main() {}
81
+ ~~~~ {.ignore}
88
82
pub fn validate_compressed_buffer(src: &[u8]) -> bool {
89
83
unsafe {
90
84
snappy_validate_compressed_buffer(src.as_ptr(), src.len() as size_t) == 0
@@ -104,20 +98,14 @@ required capacity to hold the compressed output. The vector can then be passed t
104
98
` snappy_compress ` function as an output parameter. An output parameter is also passed to retrieve
105
99
the true length after compression for setting the length.
106
100
107
- ~~~~
108
- # extern crate libc;
109
- # use libc::{size_t, c_int};
110
- # unsafe fn snappy_compress(a: *u8, b: size_t, c: *mut u8,
111
- # d: *mut size_t) -> c_int { 0 }
112
- # unsafe fn snappy_max_compressed_length(a: size_t) -> size_t { a }
113
- # fn main() {}
114
- pub fn compress(src: &[u8]) -> Vec<u8> {
101
+ ~~~~ {.ignore}
102
+ pub fn compress(src: &[u8]) -> ~[u8] {
115
103
unsafe {
116
104
let srclen = src.len() as size_t;
117
105
let psrc = src.as_ptr();
118
106
119
107
let mut dstlen = snappy_max_compressed_length(srclen);
120
- let mut dst = Vec ::with_capacity(dstlen as uint);
108
+ let mut dst = slice ::with_capacity(dstlen as uint);
121
109
let pdst = dst.as_mut_ptr();
122
110
123
111
snappy_compress(psrc, srclen, pdst, &mut dstlen);
@@ -130,26 +118,16 @@ pub fn compress(src: &[u8]) -> Vec<u8> {
130
118
Decompression is similar, because snappy stores the uncompressed size as part of the compression
131
119
format and ` snappy_uncompressed_length ` will retrieve the exact buffer size required.
132
120
133
- ~~~~
134
- # extern crate libc;
135
- # use libc::{size_t, c_int};
136
- # unsafe fn snappy_uncompress(compressed: *u8,
137
- # compressed_length: size_t,
138
- # uncompressed: *mut u8,
139
- # uncompressed_length: *mut size_t) -> c_int { 0 }
140
- # unsafe fn snappy_uncompressed_length(compressed: *u8,
141
- # compressed_length: size_t,
142
- # result: *mut size_t) -> c_int { 0 }
143
- # fn main() {}
144
- pub fn uncompress(src: &[u8]) -> Option<Vec<u8>> {
121
+ ~~~~ {.ignore}
122
+ pub fn uncompress(src: &[u8]) -> Option<~[u8]> {
145
123
unsafe {
146
124
let srclen = src.len() as size_t;
147
125
let psrc = src.as_ptr();
148
126
149
127
let mut dstlen: size_t = 0;
150
128
snappy_uncompressed_length(psrc, srclen, &mut dstlen);
151
129
152
- let mut dst = Vec ::with_capacity(dstlen as uint);
130
+ let mut dst = slice ::with_capacity(dstlen as uint);
153
131
let pdst = dst.as_mut_ptr();
154
132
155
133
if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {
@@ -209,19 +187,16 @@ A basic example is:
209
187
210
188
Rust code:
211
189
212
- ~~~~
190
+ ~~~~ {.ignore}
213
191
extern fn callback(a:i32) {
214
192
println!("I'm called from C with value {0}", a);
215
193
}
216
194
217
195
#[link(name = "extlib")]
218
- # #[cfg(ignore)]
219
196
extern {
220
- fn register_callback(cb: extern fn(i32)) -> i32;
197
+ fn register_callback(cb: extern "C" fn(i32)) -> i32;
221
198
fn trigger_callback();
222
199
}
223
- # unsafe fn register_callback(cb: extern fn(i32)) -> i32 { 0 }
224
- # unsafe fn trigger_callback() { }
225
200
226
201
fn main() {
227
202
unsafe {
@@ -265,39 +240,33 @@ referenced Rust object.
265
240
266
241
Rust code:
267
242
268
- ~~~~
243
+ ~~~~ {.ignore}
269
244
270
245
struct RustObject {
271
246
a: i32,
272
247
// other members
273
248
}
274
249
275
- extern fn callback(target: *mut RustObject, a:i32) {
250
+ extern fn callback(target: *RustObject, a:i32) {
276
251
println!("I'm called from C with value {0}", a);
277
- unsafe {
278
- // Update the value in RustObject with the value received from the callback
279
- (*target).a = a;
280
- }
252
+ (*target).a = a; // Update the value in RustObject with the value received from the callback
281
253
}
282
254
283
255
#[link(name = "extlib")]
284
- # #[cfg(ignore)]
285
256
extern {
286
- fn register_callback(target: *mut RustObject,
287
- cb: extern fn(*mut RustObject, i32)) -> i32;
257
+ fn register_callback(target: *RustObject, cb: extern "C" fn(*RustObject, i32)) -> i32;
288
258
fn trigger_callback();
289
259
}
290
- # unsafe fn register_callback(a: *mut RustObject,
291
- # b: extern fn(*mut RustObject, i32)) -> i32 { 0 }
292
- # unsafe fn trigger_callback() {}
293
260
294
261
fn main() {
295
262
// Create the object that will be referenced in the callback
296
- let mut rust_object = ~RustObject{ a: 5 };
263
+ let rust_object = ~RustObject{a: 5, ... };
297
264
298
265
unsafe {
299
- register_callback(&mut *rust_object, callback);
300
- trigger_callback();
266
+ // Gets a raw pointer to the object
267
+ let target_addr:*RustObject = ptr::to_unsafe_ptr(rust_object);
268
+ register_callback(target_addr, callback);
269
+ trigger_callback(); // Triggers the callback
301
270
}
302
271
}
303
272
~~~~
@@ -434,15 +403,13 @@ Foreign APIs often export a global variable which could do something like track
434
403
global state. In order to access these variables, you declare them in ` extern `
435
404
blocks with the ` static ` keyword:
436
405
437
- ~~~
406
+ ~~~ {.ignore}
438
407
extern crate libc;
439
408
440
409
#[link(name = "readline")]
441
- # #[cfg(ignore)]
442
410
extern {
443
411
static rl_readline_version: libc::c_int;
444
412
}
445
- # static rl_readline_version: libc::c_int = 0;
446
413
447
414
fn main() {
448
415
println!("You have readline version {} installed.",
@@ -454,23 +421,21 @@ Alternatively, you may need to alter global state provided by a foreign
454
421
interface. To do this, statics can be declared with ` mut ` so rust can mutate
455
422
them.
456
423
457
- ~~~
424
+ ~~~ {.ignore}
458
425
extern crate libc;
459
426
use std::ptr;
460
427
461
428
#[link(name = "readline")]
462
- # #[cfg(ignore)]
463
429
extern {
464
430
static mut rl_prompt: *libc::c_char;
465
431
}
466
- # static mut rl_prompt: *libc::c_char = 0 as *libc::c_char;
467
432
468
433
fn main() {
469
- "[my-awesome-shell] $".with_c_str( |buf| {
434
+ do "[my-awesome-shell] $".as_c_str |buf| {
470
435
unsafe { rl_prompt = buf; }
471
436
// get a line, process it
472
437
unsafe { rl_prompt = ptr::null(); }
473
- });
438
+ }
474
439
}
475
440
~~~
476
441
0 commit comments