Skip to content

Commit 25c5422

Browse files
committed
auto merge of #13401 : cmr/rust/docs, r=brson
Adds docs where previously there were no docs. Also adds windows support to libterm.
2 parents bbd034c + 3da99c5 commit 25c5422

File tree

11 files changed

+615
-304
lines changed

11 files changed

+615
-304
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ DEPS_arena := std collections
7373
DEPS_graphviz := std
7474
DEPS_glob := std
7575
DEPS_serialize := std collections log
76-
DEPS_term := std collections
76+
DEPS_term := std collections log
7777
DEPS_semver := std
7878
DEPS_uuid := std serialize rand
7979
DEPS_sync := std

src/libarena/lib.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
//! once, once the arena itself is destroyed. They do not support deallocation
1515
//! of individual objects while the arena itself is still alive. The benefit
1616
//! of an arena is very fast allocation; just a pointer bump.
17+
//!
18+
//! This crate has two arenas implemented: TypedArena, which is a simpler
19+
//! arena but can only hold objects of a single type, and Arena, which is a
20+
//! more complex, slower Arena which can hold objects of any type.
1721
1822
#![crate_id = "arena#0.11.0-pre"]
1923
#![crate_type = "rlib"]
@@ -56,41 +60,42 @@ impl Chunk {
5660
}
5761
}
5862

59-
// Arenas are used to quickly allocate objects that share a
60-
// lifetime. The arena uses ~[u8] vectors as a backing store to
61-
// allocate objects from. For each allocated object, the arena stores
62-
// a pointer to the type descriptor followed by the
63-
// object. (Potentially with alignment padding after each of them.)
64-
// When the arena is destroyed, it iterates through all of its chunks,
65-
// and uses the tydesc information to trace through the objects,
66-
// calling the destructors on them.
67-
// One subtle point that needs to be addressed is how to handle
68-
// failures while running the user provided initializer function. It
69-
// is important to not run the destructor on uninitialized objects, but
70-
// how to detect them is somewhat subtle. Since alloc() can be invoked
71-
// recursively, it is not sufficient to simply exclude the most recent
72-
// object. To solve this without requiring extra space, we use the low
73-
// order bit of the tydesc pointer to encode whether the object it
74-
// describes has been fully initialized.
75-
76-
// As an optimization, objects with destructors are stored in
77-
// different chunks than objects without destructors. This reduces
78-
// overhead when initializing plain-old-data and means we don't need
79-
// to waste time running the destructors of POD.
63+
/// A slower reflection-based arena that can allocate objects of any type.
64+
///
65+
/// This arena uses Vec<u8> as a backing store to allocate objects from. For
66+
/// each allocated object, the arena stores a pointer to the type descriptor
67+
/// followed by the object. (Potentially with alignment padding after each
68+
/// element.) When the arena is destroyed, it iterates through all of its
69+
/// chunks, and uses the tydesc information to trace through the objects,
70+
/// calling the destructors on them. One subtle point that needs to be
71+
/// addressed is how to handle failures while running the user provided
72+
/// initializer function. It is important to not run the destructor on
73+
/// uninitialized objects, but how to detect them is somewhat subtle. Since
74+
/// alloc() can be invoked recursively, it is not sufficient to simply exclude
75+
/// the most recent object. To solve this without requiring extra space, we
76+
/// use the low order bit of the tydesc pointer to encode whether the object
77+
/// it describes has been fully initialized.
78+
///
79+
/// As an optimization, objects with destructors are stored in
80+
/// different chunks than objects without destructors. This reduces
81+
/// overhead when initializing plain-old-data and means we don't need
82+
/// to waste time running the destructors of POD.
8083
pub struct Arena {
8184
// The head is separated out from the list as a unbenchmarked
82-
// microoptimization, to avoid needing to case on the list to
83-
// access the head.
85+
// microoptimization, to avoid needing to case on the list to access the
86+
// head.
8487
head: Chunk,
8588
copy_head: Chunk,
8689
chunks: RefCell<Vec<Chunk>>,
8790
}
8891

8992
impl Arena {
93+
/// Allocate a new Arena with 32 bytes preallocated.
9094
pub fn new() -> Arena {
9195
Arena::new_with_size(32u)
9296
}
9397

98+
/// Allocate a new Arena with `initial_size` bytes preallocated.
9499
pub fn new_with_size(initial_size: uint) -> Arena {
95100
Arena {
96101
head: chunk(initial_size, false),
@@ -265,7 +270,8 @@ impl Arena {
265270
}
266271
}
267272

268-
// The external interface
273+
/// Allocate a new item in the arena, using `op` to initialize the value
274+
/// and returning a reference to it.
269275
#[inline]
270276
pub fn alloc<'a, T>(&'a self, op: || -> T) -> &'a T {
271277
unsafe {
@@ -313,7 +319,7 @@ fn test_arena_destructors_fail() {
313319
});
314320
}
315321

316-
/// An arena that can hold objects of only one type.
322+
/// A faster arena that can hold objects of only one type.
317323
///
318324
/// Safety note: Modifying objects in the arena that have already had their
319325
/// `drop` destructors run can cause leaks, because the destructor will not
@@ -405,13 +411,13 @@ impl<T> TypedArenaChunk<T> {
405411
}
406412

407413
impl<T> TypedArena<T> {
408-
/// Creates a new arena with preallocated space for 8 objects.
414+
/// Creates a new TypedArena with preallocated space for 8 objects.
409415
#[inline]
410416
pub fn new() -> TypedArena<T> {
411417
TypedArena::with_capacity(8)
412418
}
413419

414-
/// Creates a new arena with preallocated space for the given number of
420+
/// Creates a new TypedArena with preallocated space for the given number of
415421
/// objects.
416422
#[inline]
417423
pub fn with_capacity(capacity: uint) -> TypedArena<T> {
@@ -423,7 +429,7 @@ impl<T> TypedArena<T> {
423429
}
424430
}
425431

426-
/// Allocates an object into this arena.
432+
/// Allocates an object in the TypedArena, returning a reference to it.
427433
#[inline]
428434
pub fn alloc<'a>(&'a self, object: T) -> &'a T {
429435
unsafe {

src/libflate/lib.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
/*!
1212
13-
Simple compression
13+
Simple [DEFLATE][def]-based compression. This is a wrapper around the
14+
[`miniz`][mz] library, which is a one-file pure-C implementation of zlib.
15+
16+
[def]: https://en.wikipedia.org/wiki/DEFLATE
17+
[mz]: https://code.google.com/p/miniz/
1418
1519
*/
1620

@@ -31,23 +35,21 @@ extern crate libc;
3135
use std::c_vec::CVec;
3236
use libc::{c_void, size_t, c_int};
3337

34-
35-
pub mod rustrt {
36-
use libc::{c_void, size_t, c_int};
37-
#[link(name = "miniz", kind = "static")]
38-
extern {
39-
pub fn tdefl_compress_mem_to_heap(psrc_buf: *c_void,
40-
src_buf_len: size_t,
41-
pout_len: *mut size_t,
42-
flags: c_int)
43-
-> *mut c_void;
44-
45-
pub fn tinfl_decompress_mem_to_heap(psrc_buf: *c_void,
46-
src_buf_len: size_t,
47-
pout_len: *mut size_t,
48-
flags: c_int)
49-
-> *mut c_void;
50-
}
38+
#[link(name = "miniz", kind = "static")]
39+
extern {
40+
/// Raw miniz compression function.
41+
fn tdefl_compress_mem_to_heap(psrc_buf: *c_void,
42+
src_buf_len: size_t,
43+
pout_len: *mut size_t,
44+
flags: c_int)
45+
-> *mut c_void;
46+
47+
/// Raw miniz decompression function.
48+
fn tinfl_decompress_mem_to_heap(psrc_buf: *c_void,
49+
src_buf_len: size_t,
50+
pout_len: *mut size_t,
51+
flags: c_int)
52+
-> *mut c_void;
5153
}
5254

5355
static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
@@ -57,7 +59,7 @@ static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler
5759
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
5860
unsafe {
5961
let mut outsz : size_t = 0;
60-
let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
62+
let res = tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
6163
bytes.len() as size_t,
6264
&mut outsz,
6365
flags);
@@ -69,18 +71,20 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
6971
}
7072
}
7173

74+
/// Compress a buffer, without writing any sort of header on the output.
7275
pub fn deflate_bytes(bytes: &[u8]) -> Option<CVec<u8>> {
7376
deflate_bytes_internal(bytes, LZ_NORM)
7477
}
7578

79+
/// Compress a buffer, using a header that zlib can understand.
7680
pub fn deflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {
7781
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
7882
}
7983

8084
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
8185
unsafe {
8286
let mut outsz : size_t = 0;
83-
let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
87+
let res = tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
8488
bytes.len() as size_t,
8589
&mut outsz,
8690
flags);
@@ -92,10 +96,12 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
9296
}
9397
}
9498

99+
/// Decompress a buffer, without parsing any sort of header on the input.
95100
pub fn inflate_bytes(bytes: &[u8]) -> Option<CVec<u8>> {
96101
inflate_bytes_internal(bytes, 0)
97102
}
98103

104+
/// Decompress a buffer that starts with a zlib header.
99105
pub fn inflate_bytes_zlib(bytes: &[u8]) -> Option<CVec<u8>> {
100106
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
101107
}

src/libnum/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Simple numerics.
12+
//!
13+
//! This crate contains arbitrary-sized integer, rational, and complex types.
14+
//!
15+
//! ## Example
16+
//!
17+
//! This example uses the BigRational type and [Newton's method][newt] to
18+
//! approximate a square root to arbitrary precision:
19+
//!
20+
//! ```
21+
//! extern crate num;
22+
//!
23+
//! use num::bigint::BigInt;
24+
//! use num::rational::{Ratio, BigRational};
25+
//!
26+
//! fn approx_sqrt(number: u64, iterations: uint) -> BigRational {
27+
//! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
28+
//! let mut approx = start.clone();
29+
//!
30+
//! for _ in range(0, iterations) {
31+
//! approx = (approx + (start / approx)) /
32+
//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
33+
//! }
34+
//!
35+
//! approx
36+
//! }
37+
//!
38+
//! fn main() {
39+
//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
40+
//! }
41+
//! ```
42+
//!
43+
//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
44+
1145
#![feature(macro_rules)]
1246

1347
#![crate_id = "num#0.11.0-pre"]

src/libsyntax/diagnostic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub struct EmitterWriter {
259259
}
260260

261261
enum Destination {
262-
Terminal(term::Terminal<io::stdio::StdWriter>),
262+
Terminal(Box<term::Terminal<Box<Writer:Send>>:Send>),
263263
Raw(Box<Writer:Send>),
264264
}
265265

@@ -274,9 +274,9 @@ impl EmitterWriter {
274274
};
275275

276276
if use_color {
277-
let dst = match term::Terminal::new(stderr.unwrap()) {
278-
Ok(t) => Terminal(t),
279-
Err(..) => Raw(box io::stderr()),
277+
let dst = match term::stderr() {
278+
Some(t) => Terminal(t),
279+
None => Raw(box stderr),
280280
};
281281
EmitterWriter { dst: dst }
282282
} else {

0 commit comments

Comments
 (0)