Skip to content

Big rollup #8385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
026c1ae
extra: Remove all .each methods in smallintmap
Aug 7, 2013
40bdbf0
std: Fix for-range loops that can use iterators
Aug 7, 2013
e7d4a9c
Bugfix .each_edge in middle/graph.rs
Aug 7, 2013
8523f6d
rustc: Fix for-range loops that can use iterators
Aug 7, 2013
8964fcc
Implement DoubleEndedIterator on Range
lilyball Aug 7, 2013
e99eff1
Forbid `priv` where it has no effect
alexcrichton Aug 7, 2013
a7f008b
Add missing getopts::groups::optflagmulti function
Seldaek Aug 5, 2013
a8f3f03
Turn OptGroups into a main opt and a main and an aliased opts
Seldaek Aug 5, 2013
9b221f5
add inflate_bytes_zlib to exra::flate
darkf Aug 5, 2013
dd5e8b2
add extra::flate::deflate_bytes_zlib and a test
darkf Aug 5, 2013
cc160a0
extra: add `internal` to {de,in}flate_bytes_ naming to address nit
darkf Aug 5, 2013
ffd80aa
Fix unit structs in cross-crate situtations
alexcrichton Aug 7, 2013
19d0eb9
Change Freeze to Static
sammykim Aug 7, 2013
a9b7bec
Change const to static
sammykim Aug 7, 2013
3db9dc1
Document rand module with more emphasis on cryptographic security
Seldaek Aug 6, 2013
403c52d
Add weak_rng to get a random algo that puts more emphasis on speed th…
Seldaek Aug 6, 2013
1b10391
Add some documentation about globals in ffi docs
alexcrichton Aug 6, 2013
a185343
misc help message fix
Aug 6, 2013
8460dac
std: add missing #[inline] annotation to the f64 arithmetic trait impls.
huonw Aug 6, 2013
240f8f0
Gedit/gtksourceview language spec: add 'in' keyword
SimonSapin Aug 6, 2013
4ab05f9
extra: Simplify Eq/Ord in treemap
Aug 6, 2013
52b01c5
extra: External iterators for TreeSet set operations
Aug 6, 2013
7afface
extra: Implement .rev_iter() in treemap
Aug 6, 2013
898226f
extra: Remove all each_* methods in treemap
Aug 6, 2013
8ebdb37
fix recv_ready for Port to take &self and not need to return a tuple.…
bblum Aug 5, 2013
fb1575b
(cleanup) Improve rtabort message for atomic-sleep.
bblum Aug 6, 2013
0627089
Fix fallout
emberian Aug 8, 2013
86d581f
xfail-fast an aux test
emberian Aug 8, 2013
878e74e
Fix more priv fallout
emberian Aug 8, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,48 @@ unsafe fn kaboom(ptr: *int) -> int { *ptr }

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

# Accessing foreign globals

Foreign APIs often export a global variable which could do something like track
global state. In order to access these variables, you declare them in `extern`
blocks with the `static` keyword:

~~~{.xfail-test}
use std::libc;

#[link_args = "-lreadline"]
extern {
static rl_readline_version: libc::c_int;
}

fn main() {
println(fmt!("You have readline version %d installed.",
rl_readline_version as int));
}
~~~

Alternatively, you may need to alter global state provided by a foreign
interface. To do this, statics can be declared with `mut` so rust can mutate
them.

~~~{.xfail-test}
use std::libc;
use std::ptr;

#[link_args = "-lreadline"]
extern {
static mut rl_prompt: *libc::c_char;
}

fn main() {
do "[my-awesome-shell] $".as_c_str |buf| {
unsafe { rl_prompt = buf; }
// get a line, process it
unsafe { rl_prompt = ptr::null(); }
}
}
~~~

# Foreign calling conventions

Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2288,8 +2288,8 @@ pub mod farm {
}

impl Farm {
priv fn feed_chickens(&self) { ... }
priv fn feed_cows(&self) { ... }
fn feed_chickens(&self) { ... }
fn feed_cows(&self) { ... }
pub fn add_chicken(&self, c: Chicken) { ... }
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
}
}

for i in range(0u, found_flags.len()) {
if !found_flags[i] {
for (i, &flag) in found_flags.iter().enumerate() {
if !flag {
let ee = &expected_errors[i];
fatal_ProcRes(fmt!("expected %s on line %u not found: %s",
ee.kind, ee.line, ee.msg), ProcRes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<keyword>for</keyword>
<keyword>if</keyword>
<keyword>impl</keyword>
<keyword>in</keyword>
<keyword>let</keyword>
<keyword>log</keyword>
<keyword>loop</keyword>
Expand Down
14 changes: 4 additions & 10 deletions src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,22 +846,16 @@ mod tests {
}
assert_eq!(*state, 42);
*state = 31337;
// FIXME: #7372: hits type inference bug with iterators
// send to other readers
for i in range(0u, reader_convos.len()) {
match reader_convos[i] {
(ref rc, _) => rc.send(()),
}
for &(ref rc, _) in reader_convos.iter() {
rc.send(())
}
}
let read_mode = arc.downgrade(write_mode);
do (&read_mode).read |state| {
// FIXME: #7372: hits type inference bug with iterators
// complete handshake with other readers
for i in range(0u, reader_convos.len()) {
match reader_convos[i] {
(_, ref rp) => rp.recv(),
}
for &(_, ref rp) in reader_convos.iter() {
rp.recv()
}
wc1.send(()); // tell writer to try again
assert_eq!(*state, 31337);
Expand Down
17 changes: 9 additions & 8 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,24 @@ impl BigBitv {
let len = b.storage.len();
assert_eq!(self.storage.len(), len);
let mut changed = false;
for i in range(0, len) {
for (i, (a, b)) in self.storage.mut_iter()
.zip(b.storage.iter())
.enumerate() {
let mask = big_mask(nbits, i);
let w0 = self.storage[i] & mask;
let w1 = b.storage[i] & mask;
let w0 = *a & mask;
let w1 = *b & mask;
let w = op(w0, w1) & mask;
if w0 != w {
changed = true;
self.storage[i] = w;
*a = w;
}
}
changed
}

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

#[inline]
Expand Down Expand Up @@ -205,10 +207,9 @@ impl BigBitv {

#[inline]
pub fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
let len = b.storage.len();
for i in range(0, len) {
for (i, elt) in b.storage.iter().enumerate() {
let mask = big_mask(nbits, i);
if mask & self.storage[i] != mask & b.storage[i] {
if mask & self.storage[i] != mask & *elt {
return false;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,27 @@ struct FileInput_ {
`Some(path)` is the file represented by `path`, `None` is
`stdin`. Consumed as the files are read.
*/
priv files: ~[Option<Path>],
files: ~[Option<Path>],
/**
The current file: `Some(r)` for an open file, `None` before
starting and after reading everything.
*/
priv current_reader: Option<@io::Reader>,
priv state: FileInputState,
current_reader: Option<@io::Reader>,
state: FileInputState,

/**
Used to keep track of whether we need to insert the newline at the
end of a file that is missing it, which is needed to separate the
last and first lines.
*/
priv previous_was_newline: bool
previous_was_newline: bool
}

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

impl FileInput {
Expand Down Expand Up @@ -198,7 +198,7 @@ impl FileInput {
FileInput::from_vec(pathed)
}

priv fn current_file_eof(&self) -> bool {
fn current_file_eof(&self) -> bool {
match self.fi.current_reader {
None => false,
Some(r) => r.eof()
Expand Down Expand Up @@ -240,7 +240,7 @@ impl FileInput {
Returns `true` if it had to move to the next file and did
so successfully.
*/
priv fn next_file_if_eof(&self) -> bool {
fn next_file_if_eof(&self) -> bool {
match self.fi.current_reader {
None => self.next_file(),
Some(r) => {
Expand Down
34 changes: 30 additions & 4 deletions src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ static LZ_NONE : c_int = 0x0; // Huffman-coding only.
static LZ_FAST : c_int = 0x1; // LZ with only one probe
static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum

pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
len as size_t,
&mut outsz,
LZ_NORM);
flags);
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
Expand All @@ -62,15 +64,23 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
}
}

pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
deflate_bytes_internal(bytes, LZ_NORM)
}

pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
}

fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
len as size_t,
&mut outsz,
0);
flags);
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
Expand All @@ -80,6 +90,14 @@ pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
}
}

pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
inflate_bytes_internal(bytes, 0)
}

pub fn inflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -109,4 +127,12 @@ mod tests {
assert_eq!(input, out);
}
}

#[test]
fn test_zlib_flate() {
let bytes = ~[1, 2, 3, 4, 5];
let deflated = deflate_bytes(bytes);
let inflated = inflate_bytes(deflated);
assert_eq!(inflated, bytes);
}
}
2 changes: 1 addition & 1 deletion src/libextra/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<A> Drop for Future<A> {
fn drop(&self) {}
}

priv enum FutureState<A> {
enum FutureState<A> {
Pending(~fn() -> A),
Evaluating,
Forced(A)
Expand Down
Loading