Skip to content

Commit db26493

Browse files
authored
doc: ffi referenced rust-snappy can not compile
r? @steveklabnik The referenced code https://github.com/thestinger/rust-snappy can not work. Maybe it's the old rust version? I do not know. So I try to rewrite these test cases. If it is not what you originally meaning, just ignored it.
1 parent 2ab18ce commit db26493

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

src/doc/book/ffi.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,62 @@ pub fn uncompress(src: &[u8]) -> Option<Vec<u8>> {
183183
}
184184
```
185185

186-
For reference, the examples used here are also available as a [library on
187-
GitHub](https://github.com/thestinger/rust-snappy).
186+
Then, we can add some tests to show how to use them.
187+
188+
```rust
189+
# #![feature(libc)]
190+
# extern crate libc;
191+
# use libc::{c_int, size_t};
192+
# unsafe fn snappy_compress(input: *const u8,
193+
# input_length: size_t,
194+
# compressed: *mut u8,
195+
# compressed_length: *mut size_t)
196+
# -> c_int { 0 }
197+
# unsafe fn snappy_uncompress(compressed: *const u8,
198+
# compressed_length: size_t,
199+
# uncompressed: *mut u8,
200+
# uncompressed_length: *mut size_t)
201+
# -> c_int { 0 }
202+
# unsafe fn snappy_max_compressed_length(source_length: size_t) -> size_t { 0 }
203+
# unsafe fn snappy_uncompressed_length(compressed: *const u8,
204+
# compressed_length: size_t,
205+
# result: *mut size_t)
206+
# -> c_int { 0 }
207+
# unsafe fn snappy_validate_compressed_buffer(compressed: *const u8,
208+
# compressed_length: size_t)
209+
# -> c_int { 0 }
210+
# fn main() { }
211+
212+
#[cfg(test)]
213+
mod tests {
214+
use super::*;
215+
216+
#[test]
217+
fn valid() {
218+
let d = vec![0xde, 0xad, 0xd0, 0x0d];
219+
let c: &[u8] = &compress(&d);
220+
assert!(validate_compressed_buffer(c));
221+
assert!(uncompress(c) == Some(d));
222+
}
223+
224+
#[test]
225+
fn invalid() {
226+
let d = vec![0, 0, 0, 0];
227+
assert!(!validate_compressed_buffer(&d));
228+
assert!(uncompress(&d).is_none());
229+
}
230+
231+
#[test]
232+
fn empty() {
233+
let d = vec![];
234+
assert!(!validate_compressed_buffer(&d));
235+
assert!(uncompress(&d).is_none());
236+
let c = compress(&d);
237+
assert!(validate_compressed_buffer(&c));
238+
assert!(uncompress(&c) == Some(d));
239+
}
240+
}
241+
```
188242

189243
# Destructors
190244

0 commit comments

Comments
 (0)