@@ -16,11 +16,14 @@ compile if snappy is installed:
16
16
extern crate libc;
17
17
use libc::size_t;
18
18
19
+ #[cfg(nope)]
19
20
#[link(name = "snappy")]
20
21
extern {
21
22
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
22
23
}
23
24
25
+ # #[cfg(not(nope))] unsafe fn snappy_max_compressed_length(_: size_t) -> size_t { 0 }
26
+
24
27
fn main() {
25
28
let x = unsafe { snappy_max_compressed_length(100) };
26
29
println!("max compressed length of a 100 byte buffer: {}", x);
@@ -50,6 +53,7 @@ The `extern` block can be extended to cover the entire snappy API:
50
53
extern crate libc;
51
54
use libc::{c_int, size_t};
52
55
56
+ # #[cfg(nope)]
53
57
#[link(name = "snappy")]
54
58
extern {
55
59
fn snappy_compress(input: *const u8,
@@ -67,6 +71,7 @@ extern {
67
71
fn snappy_validate_compressed_buffer(compressed: *const u8,
68
72
compressed_length: size_t) -> c_int;
69
73
}
74
+
70
75
# fn main() {}
71
76
```
72
77
@@ -85,8 +90,11 @@ the allocated memory. The length is less than or equal to the capacity.
85
90
# #![feature(libc)]
86
91
# extern crate libc;
87
92
# use libc :: {c_int, size_t};
93
+ #
88
94
# unsafe fn snappy_validate_compressed_buffer (_ : * const u8 , _ : size_t ) -> c_int { 0 }
95
+ #
89
96
# fn main () {}
97
+ #
90
98
pub fn validate_compressed_buffer (src : & [u8 ]) -> bool {
91
99
unsafe {
92
100
snappy_validate_compressed_buffer (src . as_ptr (), src . len () as size_t ) == 0
@@ -110,10 +118,13 @@ the true length after compression for setting the length.
110
118
# #![feature(libc)]
111
119
# extern crate libc;
112
120
# use libc :: {size_t, c_int};
121
+ #
113
122
# unsafe fn snappy_compress (a : * const u8 , b : size_t , c : * mut u8 ,
114
123
# d : * mut size_t ) -> c_int { 0 }
115
124
# unsafe fn snappy_max_compressed_length (a : size_t ) -> size_t { a }
125
+ #
116
126
# fn main () {}
127
+ #
117
128
pub fn compress (src : & [u8 ]) -> Vec <u8 > {
118
129
unsafe {
119
130
let srclen = src . len () as size_t ;
@@ -137,6 +148,7 @@ format and `snappy_uncompressed_length` will retrieve the exact buffer size requ
137
148
# #![feature(libc)]
138
149
# extern crate libc;
139
150
# use libc :: {size_t, c_int};
151
+ #
140
152
# unsafe fn snappy_uncompress (compressed : * const u8 ,
141
153
# compressed_length : size_t ,
142
154
# uncompressed : * mut u8 ,
@@ -145,6 +157,7 @@ format and `snappy_uncompressed_length` will retrieve the exact buffer size requ
145
157
# compressed_length : size_t ,
146
158
# result : * mut size_t ) -> c_int { 0 }
147
159
# fn main () {}
160
+ #
148
161
pub fn uncompress (src : & [u8 ]) -> Option <Vec <u8 >> {
149
162
unsafe {
150
163
let srclen = src . len () as size_t ;
@@ -197,11 +210,16 @@ extern fn callback(a: i32) {
197
210
println!("I'm called from C with value {0}", a);
198
211
}
199
212
213
+ # #[cfg(nope)]
200
214
#[link(name = "extlib")]
201
215
extern {
202
216
fn register_callback(cb: extern fn(i32)) -> i32;
203
217
fn trigger_callback();
204
218
}
219
+ #
220
+ # #[cfg(not(nope))] static mut CALLBACK: Option<extern fn(i32)> = None;
221
+ # #[cfg(not(nope))] unsafe fn register_callback(cb: extern fn(i32)) -> i32 { CALLBACK = Some(cb); 1 }
222
+ # #[cfg(not(nope))] unsafe fn trigger_callback() { CALLBACK.unwrap()(7); }
205
223
206
224
fn main() {
207
225
unsafe {
@@ -260,12 +278,17 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
260
278
}
261
279
}
262
280
281
+ # #[cfg(nope)]
263
282
#[link(name = "extlib")]
264
283
extern {
265
284
fn register_callback(target: *mut RustObject,
266
285
cb: extern fn(*mut RustObject, i32)) -> i32;
267
286
fn trigger_callback();
268
287
}
288
+ #
289
+ # #[cfg(not(nope))] static mut CALLBACK: Option<(*mut RustObject, extern fn(*mut RustObject, i32))> = None;
290
+ # #[cfg(not(nope))] unsafe fn register_callback(target: *mut RustObject, cb: extern fn(*mut RustObject, i32)) -> i32 { CALLBACK = Some((target, cb)); 1 }
291
+ # #[cfg(not(nope))] unsafe fn trigger_callback() { let (target, cb) = CALLBACK.unwrap(); cb(target, 7); }
269
292
270
293
fn main() {
271
294
// Create the object that will be referenced in the callback
@@ -379,6 +402,8 @@ this:
379
402
380
403
```rust
381
404
unsafe fn kaboom(ptr: *const i32) -> i32 { *ptr }
405
+
406
+ # fn main() {}
382
407
```
383
408
384
409
This function can only be called from an ` unsafe ` block or another ` unsafe ` function.
@@ -451,6 +476,7 @@ extern crate libc;
451
476
extern " stdcall" {
452
477
fn SetEnvironmentVariableA (n : * const u8 , v : * const u8 ) -> libc :: c_int ;
453
478
}
479
+
454
480
# fn main () { }
455
481
```
456
482
@@ -525,6 +551,7 @@ fairly easy, but requires a few things:
525
551
pub extern fn hello_rust () -> * const u8 {
526
552
" Hello, world!\ 0" . as_ptr ()
527
553
}
554
+
528
555
# fn main () {}
529
556
```
530
557
@@ -554,6 +581,7 @@ pub extern fn oh_no() -> i32 {
554
581
Err (_ ) => 0 ,
555
582
}
556
583
}
584
+
557
585
# fn main () {}
558
586
```
559
587
@@ -578,6 +606,7 @@ extern "C" {
578
606
pub fn foo(arg: *mut libc::c_void);
579
607
pub fn bar(arg: *mut libc::c_void);
580
608
}
609
+
581
610
# fn main() {}
582
611
```
583
612
@@ -603,6 +632,7 @@ extern "C" {
603
632
pub fn foo(arg: *mut Foo);
604
633
pub fn bar(arg: *mut Bar);
605
634
}
635
+
606
636
# fn main() {}
607
637
```
608
638
0 commit comments