about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2016-07-15 10:56:43 +0200
committerGitHub <noreply@github.com>2016-07-15 10:56:43 +0200
commitca67437ec926be029719600af8dfaa800fc2f2ce (patch)
tree207797ccb9a0948d85a6f9d147c1000582932ce3 /src
parent367f942ce483ce0f8b858d3e31d55dd3cbf070cb (diff)
parentdb2649363b2c2881870505365cd27f75567042aa (diff)
downloadrust-ca67437ec926be029719600af8dfaa800fc2f2ce.tar.gz
rust-ca67437ec926be029719600af8dfaa800fc2f2ce.zip
Rollup merge of #34799 - wuranbo:patch-3, r=steveklabnik
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.
Diffstat (limited to 'src')
-rw-r--r--src/doc/book/ffi.md58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/doc/book/ffi.md b/src/doc/book/ffi.md
index 3fbcbc2f471..3cbe5d60267 100644
--- a/src/doc/book/ffi.md
+++ b/src/doc/book/ffi.md
@@ -183,8 +183,62 @@ pub fn uncompress(src: &[u8]) -> Option<Vec<u8>> {
 }
 ```
 
-For reference, the examples used here are also available as a [library on
-GitHub](https://github.com/thestinger/rust-snappy).
+Then, we can add some tests to show how to use them.
+
+```rust
+# #![feature(libc)]
+# extern crate libc;
+# use libc::{c_int, size_t};
+# unsafe fn snappy_compress(input: *const u8,
+#                           input_length: size_t,
+#                           compressed: *mut u8,
+#                           compressed_length: *mut size_t)
+#                           -> c_int { 0 }
+# unsafe fn snappy_uncompress(compressed: *const u8,
+#                             compressed_length: size_t,
+#                             uncompressed: *mut u8,
+#                             uncompressed_length: *mut size_t)
+#                             -> c_int { 0 }
+# unsafe fn snappy_max_compressed_length(source_length: size_t) -> size_t { 0 }
+# unsafe fn snappy_uncompressed_length(compressed: *const u8,
+#                                      compressed_length: size_t,
+#                                      result: *mut size_t)
+#                                      -> c_int { 0 }
+# unsafe fn snappy_validate_compressed_buffer(compressed: *const u8,
+#                                             compressed_length: size_t)
+#                                             -> c_int { 0 }
+# fn main() { }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn valid() {
+        let d = vec![0xde, 0xad, 0xd0, 0x0d];
+        let c: &[u8] = &compress(&d);
+        assert!(validate_compressed_buffer(c));
+        assert!(uncompress(c) == Some(d));
+    }
+
+    #[test]
+    fn invalid() {
+        let d = vec![0, 0, 0, 0];
+        assert!(!validate_compressed_buffer(&d));
+        assert!(uncompress(&d).is_none());
+    }
+
+    #[test]
+    fn empty() {
+        let d = vec![];
+        assert!(!validate_compressed_buffer(&d));
+        assert!(uncompress(&d).is_none());
+        let c = compress(&d);
+        assert!(validate_compressed_buffer(&c));
+        assert!(uncompress(&c) == Some(d));
+    }
+}
+```
 
 # Destructors