Skip to content

Commit 8f65dbf

Browse files
committed
auto merge of #8385 : cmr/rust/big-rollup, r=alexcrichton
This is a fairly large rollup, but I've tested everything locally, and none of it should be platform-specific. r=alexcrichton (bdfdbdd) r=brson (d803c18) r=alexcrichton (a5041d0) r=bstrie (317412a) r=alexcrichton (135c85e) r=thestinger (8805baa) r=pcwalton (0661178) r=cmr (9397fe0) r=cmr (caa4135) r=cmr (6a21d93) r=cmr (4dc3379) r=cmr (0aa5154) r=cmr (18be261) r=thestinger (f10be03)
2 parents a0080f4 + 878e74e commit 8f65dbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+762
-554
lines changed

doc/tutorial-ffi.md

+42
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,48 @@ unsafe fn kaboom(ptr: *int) -> int { *ptr }
228228

229229
This function can only be called from an `unsafe` block or another `unsafe` function.
230230

231+
# Accessing foreign globals
232+
233+
Foreign APIs often export a global variable which could do something like track
234+
global state. In order to access these variables, you declare them in `extern`
235+
blocks with the `static` keyword:
236+
237+
~~~{.xfail-test}
238+
use std::libc;
239+
240+
#[link_args = "-lreadline"]
241+
extern {
242+
static rl_readline_version: libc::c_int;
243+
}
244+
245+
fn main() {
246+
println(fmt!("You have readline version %d installed.",
247+
rl_readline_version as int));
248+
}
249+
~~~
250+
251+
Alternatively, you may need to alter global state provided by a foreign
252+
interface. To do this, statics can be declared with `mut` so rust can mutate
253+
them.
254+
255+
~~~{.xfail-test}
256+
use std::libc;
257+
use std::ptr;
258+
259+
#[link_args = "-lreadline"]
260+
extern {
261+
static mut rl_prompt: *libc::c_char;
262+
}
263+
264+
fn main() {
265+
do "[my-awesome-shell] $".as_c_str |buf| {
266+
unsafe { rl_prompt = buf; }
267+
// get a line, process it
268+
unsafe { rl_prompt = ptr::null(); }
269+
}
270+
}
271+
~~~
272+
231273
# Foreign calling conventions
232274

233275
Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when

doc/tutorial.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2288,8 +2288,8 @@ pub mod farm {
22882288
}
22892289
22902290
impl Farm {
2291-
priv fn feed_chickens(&self) { ... }
2292-
priv fn feed_cows(&self) { ... }
2291+
fn feed_chickens(&self) { ... }
2292+
fn feed_cows(&self) { ... }
22932293
pub fn add_chicken(&self, c: Chicken) { ... }
22942294
}
22952295

src/compiletest/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
412412
}
413413
}
414414

415-
for i in range(0u, found_flags.len()) {
416-
if !found_flags[i] {
415+
for (i, &flag) in found_flags.iter().enumerate() {
416+
if !flag {
417417
let ee = &expected_errors[i];
418418
fatal_ProcRes(fmt!("expected %s on line %u not found: %s",
419419
ee.kind, ee.line, ee.msg), ProcRes);

src/etc/gedit/share/gtksourceview-3.0/language-specs/rust.lang

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<keyword>for</keyword>
5151
<keyword>if</keyword>
5252
<keyword>impl</keyword>
53+
<keyword>in</keyword>
5354
<keyword>let</keyword>
5455
<keyword>log</keyword>
5556
<keyword>loop</keyword>

src/libextra/arc.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -847,22 +847,16 @@ mod tests {
847847
}
848848
assert_eq!(*state, 42);
849849
*state = 31337;
850-
// FIXME: #7372: hits type inference bug with iterators
851850
// send to other readers
852-
for i in range(0u, reader_convos.len()) {
853-
match reader_convos[i] {
854-
(ref rc, _) => rc.send(()),
855-
}
851+
for &(ref rc, _) in reader_convos.iter() {
852+
rc.send(())
856853
}
857854
}
858855
let read_mode = arc.downgrade(write_mode);
859856
do (&read_mode).read |state| {
860-
// FIXME: #7372: hits type inference bug with iterators
861857
// complete handshake with other readers
862-
for i in range(0u, reader_convos.len()) {
863-
match reader_convos[i] {
864-
(_, ref rp) => rp.recv(),
865-
}
858+
for &(_, ref rp) in reader_convos.iter() {
859+
rp.recv()
866860
}
867861
wc1.send(()); // tell writer to try again
868862
assert_eq!(*state, 31337);

src/libextra/bitv.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,24 @@ impl BigBitv {
145145
let len = b.storage.len();
146146
assert_eq!(self.storage.len(), len);
147147
let mut changed = false;
148-
for i in range(0, len) {
148+
for (i, (a, b)) in self.storage.mut_iter()
149+
.zip(b.storage.iter())
150+
.enumerate() {
149151
let mask = big_mask(nbits, i);
150-
let w0 = self.storage[i] & mask;
151-
let w1 = b.storage[i] & mask;
152+
let w0 = *a & mask;
153+
let w1 = *b & mask;
152154
let w = op(w0, w1) & mask;
153155
if w0 != w {
154156
changed = true;
155-
self.storage[i] = w;
157+
*a = w;
156158
}
157159
}
158160
changed
159161
}
160162

161163
#[inline]
162164
pub fn each_storage(&mut self, op: &fn(v: &mut uint) -> bool) -> bool {
163-
range(0u, self.storage.len()).advance(|i| op(&mut self.storage[i]))
165+
self.storage.mut_iter().advance(|elt| op(elt))
164166
}
165167

166168
#[inline]
@@ -205,10 +207,9 @@ impl BigBitv {
205207

206208
#[inline]
207209
pub fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
208-
let len = b.storage.len();
209-
for i in range(0, len) {
210+
for (i, elt) in b.storage.iter().enumerate() {
210211
let mask = big_mask(nbits, i);
211-
if mask & self.storage[i] != mask & b.storage[i] {
212+
if mask & self.storage[i] != mask & *elt {
212213
return false;
213214
}
214215
}

src/libextra/fileinput.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,27 @@ struct FileInput_ {
129129
`Some(path)` is the file represented by `path`, `None` is
130130
`stdin`. Consumed as the files are read.
131131
*/
132-
priv files: ~[Option<Path>],
132+
files: ~[Option<Path>],
133133
/**
134134
The current file: `Some(r)` for an open file, `None` before
135135
starting and after reading everything.
136136
*/
137-
priv current_reader: Option<@io::Reader>,
138-
priv state: FileInputState,
137+
current_reader: Option<@io::Reader>,
138+
state: FileInputState,
139139

140140
/**
141141
Used to keep track of whether we need to insert the newline at the
142142
end of a file that is missing it, which is needed to separate the
143143
last and first lines.
144144
*/
145-
priv previous_was_newline: bool
145+
previous_was_newline: bool
146146
}
147147

148148
// XXX: remove this when Reader has &mut self. Should be removable via
149149
// "self.fi." -> "self." and renaming FileInput_. Documentation above
150150
// will likely have to be updated to use `let mut in = ...`.
151151
pub struct FileInput {
152-
priv fi: @mut FileInput_
152+
fi: @mut FileInput_
153153
}
154154

155155
impl FileInput {
@@ -198,7 +198,7 @@ impl FileInput {
198198
FileInput::from_vec(pathed)
199199
}
200200

201-
priv fn current_file_eof(&self) -> bool {
201+
fn current_file_eof(&self) -> bool {
202202
match self.fi.current_reader {
203203
None => false,
204204
Some(r) => r.eof()
@@ -240,7 +240,7 @@ impl FileInput {
240240
Returns `true` if it had to move to the next file and did
241241
so successfully.
242242
*/
243-
priv fn next_file_if_eof(&self) -> bool {
243+
fn next_file_if_eof(&self) -> bool {
244244
match self.fi.current_reader {
245245
None => self.next_file(),
246246
Some(r) => {

src/libextra/flate.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ static LZ_NONE : c_int = 0x0; // Huffman-coding only.
4343
static LZ_FAST : c_int = 0x1; // LZ with only one probe
4444
static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
4545
static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
46+
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
47+
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
4648

47-
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
49+
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
4850
do bytes.as_imm_buf |b, len| {
4951
unsafe {
5052
let mut outsz : size_t = 0;
5153
let res =
5254
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
5355
len as size_t,
5456
&mut outsz,
55-
LZ_NORM);
57+
flags);
5658
assert!(res as int != 0);
5759
let out = vec::raw::from_buf_raw(res as *u8,
5860
outsz as uint);
@@ -62,15 +64,23 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
6264
}
6365
}
6466

65-
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
67+
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
68+
deflate_bytes_internal(bytes, LZ_NORM)
69+
}
70+
71+
pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
72+
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
73+
}
74+
75+
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
6676
do bytes.as_imm_buf |b, len| {
6777
unsafe {
6878
let mut outsz : size_t = 0;
6979
let res =
7080
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
7181
len as size_t,
7282
&mut outsz,
73-
0);
83+
flags);
7484
assert!(res as int != 0);
7585
let out = vec::raw::from_buf_raw(res as *u8,
7686
outsz as uint);
@@ -80,6 +90,14 @@ pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
8090
}
8191
}
8292

93+
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
94+
inflate_bytes_internal(bytes, 0)
95+
}
96+
97+
pub fn inflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
98+
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
99+
}
100+
83101
#[cfg(test)]
84102
mod tests {
85103
use super::*;
@@ -109,4 +127,12 @@ mod tests {
109127
assert_eq!(input, out);
110128
}
111129
}
130+
131+
#[test]
132+
fn test_zlib_flate() {
133+
let bytes = ~[1, 2, 3, 4, 5];
134+
let deflated = deflate_bytes(bytes);
135+
let inflated = inflate_bytes(deflated);
136+
assert_eq!(inflated, bytes);
137+
}
112138
}

src/libextra/future.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<A> Drop for Future<A> {
4646
fn drop(&self) {}
4747
}
4848

49-
priv enum FutureState<A> {
49+
enum FutureState<A> {
5050
Pending(~fn() -> A),
5151
Evaluating,
5252
Forced(A)

0 commit comments

Comments
 (0)