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