Skip to content

Commit

Permalink
doc: Fix tutorial-ffi xfail-tests and update SHA1() to use size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
cpeterso committed Jan 21, 2013
1 parent 0b6487c commit 50c8cbb
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions doc/tutorial-ffi.md
Expand Up @@ -12,12 +12,12 @@ argument, which it then converts to a hexadecimal string and prints to
standard output. If you have the OpenSSL libraries installed, it
should compile and run without any extra effort.

~~~~ {.xfail-test}
~~~~
extern mod std;
use libc::c_uint;
use libc::size_t;
extern mod crypto {
fn SHA1(src: *u8, sz: c_uint, out: *u8) -> *u8;
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
}
fn as_hex(data: ~[u8]) -> ~str {
Expand All @@ -29,7 +29,7 @@ fn as_hex(data: ~[u8]) -> ~str {
fn sha1(data: ~str) -> ~str unsafe {
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
vec::len(bytes) as c_uint, ptr::null());
vec::len(bytes) as size_t, ptr::null());
return as_hex(vec::from_buf(hash, 20));
}
Expand All @@ -43,9 +43,11 @@ fn main() {
Before we can call the `SHA1` function defined in the OpenSSL library, we have
to declare it. That is what this part of the program does:

~~~~ {.xfail-test}
~~~~
# use libc::size_t;
extern mod crypto {
fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; }
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
}
~~~~

An `extern` module declaration containing function signatures introduces the
Expand All @@ -62,10 +64,11 @@ searches for the shared library with that name, and links the library into the
program. If you want the module to have a different name from the actual
library, you can use the `"link_name"` attribute, like:

~~~~ {.xfail-test}
~~~~
# use libc::size_t;
#[link_name = "crypto"]
extern mod something {
fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8;
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
}
~~~~

Expand Down Expand Up @@ -94,9 +97,10 @@ calling conventions.

The foreign `SHA1` function takes three arguments, and returns a pointer.

~~~~ {.xfail-test}
~~~~
# use libc::size_t;
# extern mod crypto {
fn SHA1(src: *u8, sz: libc::c_uint, out: *u8) -> *u8;
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
# }
~~~~

Expand All @@ -108,8 +112,8 @@ probably even worse, your code will work on one platform, but break on
another.

In this case, we declare that `SHA1` takes two `unsigned char*`
arguments and one `unsigned long`. The Rust equivalents are `*u8`
unsafe pointers and an `uint` (which, like `unsigned long`, is a
arguments and one `size_t`. The Rust equivalents are `*u8`
unsafe pointers and an `libc::size_t` (which, like `unsigned long`, is a
machine-word-sized type).

The standard library provides various functions to create unsafe pointers,
Expand All @@ -124,14 +128,16 @@ The `sha1` function is the most obscure part of the program.

~~~~
# pub mod crypto {
# pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
# use libc::size_t;
# pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
# }
# use libc::size_t;
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
fn sha1(data: ~str) -> ~str {
unsafe {
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
vec::len(bytes), ptr::null());
vec::len(bytes) as size_t, ptr::null());
return as_hex(vec::from_buf(hash, 20));
}
}
Expand Down Expand Up @@ -169,14 +175,16 @@ Let's look at our `sha1` function again.

~~~~
# pub mod crypto {
# pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
# use libc::size_t;
# pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
# }
# use libc::size_t;
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
# fn x(data: ~str) -> ~str {
# unsafe {
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
vec::len(bytes), ptr::null());
vec::len(bytes) as size_t, ptr::null());
return as_hex(vec::from_buf(hash, 20));
# }
# }
Expand Down

0 comments on commit 50c8cbb

Please sign in to comment.