Merge pull request #4563 from cpeterso/fix-doc-tests
Fix various doc tests
This commit is contained in:
commit
c9c8d6fdd1
10
doc/rust.md
10
doc/rust.md
@ -594,16 +594,16 @@ and may optionally begin with any number of `attributes` that apply to the conta
|
||||
Atributes on the anonymous crate module define important metadata that influences
|
||||
the behavior of the compiler.
|
||||
|
||||
~~~~~~~~{.xfail-test}
|
||||
~~~~~~~~
|
||||
// Linkage attributes
|
||||
#[ link(name = "projx"
|
||||
#[ link(name = "projx",
|
||||
vers = "2.5",
|
||||
uuid = "9cccc5d5-aceb-4af5-8285-811211826b82") ];
|
||||
|
||||
// Additional metadata attributes
|
||||
#[ desc = "Project X",
|
||||
license = "BSD" ];
|
||||
author = "Jane Doe" ];
|
||||
#[ desc = "Project X" ];
|
||||
#[ license = "BSD" ];
|
||||
#[ author = "Jane Doe" ];
|
||||
|
||||
// Specify the output type
|
||||
#[ crate_type = "lib" ];
|
||||
|
@ -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 {
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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
|
||||
@ -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;
|
||||
}
|
||||
~~~~
|
||||
|
||||
@ -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;
|
||||
# }
|
||||
~~~~
|
||||
|
||||
@ -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,
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
# }
|
||||
# }
|
||||
|
@ -220,7 +220,7 @@ let result = port.recv();
|
||||
The `Port` and `Chan` pair created by `stream` enables efficient communication
|
||||
between a single sender and a single receiver, but multiple senders cannot use
|
||||
a single `Chan`, and multiple receivers cannot use a single `Port`. What if our
|
||||
example needed to computer multiple results across a number of tasks? The
|
||||
example needed to compute multiple results across a number of tasks? The
|
||||
following program is ill-typed:
|
||||
|
||||
~~~ {.xfail-test}
|
||||
|
@ -2393,7 +2393,7 @@ override the name used to search for the crate.
|
||||
|
||||
Our example crate declared this set of `link` attributes:
|
||||
|
||||
~~~~ {.xfail-test}
|
||||
~~~~
|
||||
#[link(name = "farm", vers = "2.5", author = "mjh")];
|
||||
~~~~
|
||||
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
# Script for extracting compilable fragments from markdown
|
||||
# documentation. See prep.js for a description of the format
|
||||
# recognized by this tool. Expects a directory fragements/ to exist
|
||||
# recognized by this tool. Expects a directory fragments/ to exist
|
||||
# under the current directory, and writes the fragments in there as
|
||||
# individual .rs files.
|
||||
|
||||
import sys, re;
|
||||
import sys, re
|
||||
|
||||
if len(sys.argv) < 3:
|
||||
print("Please provide an input filename")
|
||||
@ -26,7 +26,7 @@ chapter_n = 0
|
||||
while cur < len(lines):
|
||||
line = lines[cur]
|
||||
cur += 1
|
||||
chap = re.match("# (.*)", line);
|
||||
chap = re.match("# (.*)", line)
|
||||
if chap:
|
||||
chapter = re.sub(r"\W", "_", chap.group(1)).lower()
|
||||
chapter_n = 1
|
||||
@ -51,14 +51,30 @@ while cur < len(lines):
|
||||
else:
|
||||
# Lines beginning with '# ' are turned into valid code
|
||||
line = re.sub("^# ", "", line)
|
||||
# Allow elipses in code snippets
|
||||
# Allow ellipses in code snippets
|
||||
line = re.sub("\.\.\.", "", line)
|
||||
block += line
|
||||
if not ignore:
|
||||
if not re.search(r"\bfn main\b", block):
|
||||
block = "fn main() {\n" + block + "\n}\n"
|
||||
if not re.search(r"\bextern mod std\b", block):
|
||||
block = "extern mod std;\n" + block;
|
||||
block = "extern mod std;\n" + block
|
||||
block = """#[ forbid(ctypes) ];
|
||||
#[ forbid(deprecated_mode) ];
|
||||
#[ forbid(deprecated_pattern) ];
|
||||
#[ forbid(implicit_copies) ];
|
||||
#[ forbid(non_implicitly_copyable_typarams) ];
|
||||
#[ forbid(path_statement) ];
|
||||
#[ forbid(type_limits) ];
|
||||
#[ forbid(unrecognized_lint) ];
|
||||
#[ forbid(unused_imports) ];
|
||||
#[ forbid(vecs_implicitly_copyable) ];
|
||||
#[ forbid(while_true) ];
|
||||
|
||||
#[ warn(deprecated_self) ];
|
||||
#[ warn(non_camel_case_types) ];
|
||||
#[ warn(structural_records) ];\n
|
||||
""" + block
|
||||
if xfail:
|
||||
block = "// xfail-test\n" + block
|
||||
filename = (dest + "/" + str(chapter)
|
||||
|
Loading…
x
Reference in New Issue
Block a user