Skip to content

Commit 50c8cbb

Browse files
committed
doc: Fix tutorial-ffi xfail-tests and update SHA1() to use size_t
1 parent 0b6487c commit 50c8cbb

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

doc/tutorial-ffi.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ argument, which it then converts to a hexadecimal string and prints to
1212
standard output. If you have the OpenSSL libraries installed, it
1313
should compile and run without any extra effort.
1414

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

46-
~~~~ {.xfail-test}
46+
~~~~
47+
# use libc::size_t;
4748
extern mod crypto {
48-
fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; }
49+
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
50+
}
4951
~~~~
5052

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

65-
~~~~ {.xfail-test}
67+
~~~~
68+
# use libc::size_t;
6669
#[link_name = "crypto"]
6770
extern mod something {
68-
fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8;
71+
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
6972
}
7073
~~~~
7174

@@ -94,9 +97,10 @@ calling conventions.
9497

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

97-
~~~~ {.xfail-test}
100+
~~~~
101+
# use libc::size_t;
98102
# extern mod crypto {
99-
fn SHA1(src: *u8, sz: libc::c_uint, out: *u8) -> *u8;
103+
fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
100104
# }
101105
~~~~
102106

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

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

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

125129
~~~~
126130
# pub mod crypto {
127-
# pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
131+
# use libc::size_t;
132+
# pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
128133
# }
134+
# use libc::size_t;
129135
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
130136
fn sha1(data: ~str) -> ~str {
131137
unsafe {
132138
let bytes = str::to_bytes(data);
133139
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
134-
vec::len(bytes), ptr::null());
140+
vec::len(bytes) as size_t, ptr::null());
135141
return as_hex(vec::from_buf(hash, 20));
136142
}
137143
}
@@ -169,14 +175,16 @@ Let's look at our `sha1` function again.
169175

170176
~~~~
171177
# pub mod crypto {
172-
# pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
178+
# use libc::size_t;
179+
# pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
173180
# }
181+
# use libc::size_t;
174182
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
175183
# fn x(data: ~str) -> ~str {
176184
# unsafe {
177185
let bytes = str::to_bytes(data);
178186
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
179-
vec::len(bytes), ptr::null());
187+
vec::len(bytes) as size_t, ptr::null());
180188
return as_hex(vec::from_buf(hash, 20));
181189
# }
182190
# }

0 commit comments

Comments
 (0)