1
Fork 0

auto merge of #4796 : catamorphism/rust/derecord_std, r=catamorphism

See #4665
This commit is contained in:
bors 2013-02-05 07:04:15 -08:00
commit f1b05ece93
8 changed files with 74 additions and 71 deletions

View file

@ -46,11 +46,11 @@ use core::task;
/** /**
* The type representing a foreign chunk of memory * The type representing a foreign chunk of memory
* *
* Wrapped in a enum for opacity; FIXME #818 when it is possible to have
* truly opaque types, this should be revisited.
*/ */
pub enum CVec<T> { pub struct CVec<T> {
CVecCtor({ base: *mut T, len: uint, rsrc: @DtorRes}) priv base: *mut T,
priv len: uint,
priv rsrc: @DtorRes
} }
struct DtorRes { struct DtorRes {
@ -85,11 +85,11 @@ fn DtorRes(dtor: Option<fn@()>) -> DtorRes {
* * len - The number of elements in the buffer * * len - The number of elements in the buffer
*/ */
pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> { pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
return CVecCtor({ return CVec{
base: base, base: base,
len: len, len: len,
rsrc: @DtorRes(option::None) rsrc: @DtorRes(option::None)
}); };
} }
/** /**
@ -105,11 +105,11 @@ pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
*/ */
pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: fn@()) pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: fn@())
-> CVec<T> { -> CVec<T> {
return CVecCtor({ return CVec{
base: base, base: base,
len: len, len: len,
rsrc: @DtorRes(option::Some(dtor)) rsrc: @DtorRes(option::Some(dtor))
}); };
} }
/* /*
@ -123,7 +123,7 @@ pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: fn@())
*/ */
pub fn get<T: Copy>(t: CVec<T>, ofs: uint) -> T { pub fn get<T: Copy>(t: CVec<T>, ofs: uint) -> T {
assert ofs < len(t); assert ofs < len(t);
return unsafe { *ptr::mut_offset((*t).base, ofs) }; return unsafe { *ptr::mut_offset(t.base, ofs) };
} }
/** /**
@ -133,7 +133,7 @@ pub fn get<T: Copy>(t: CVec<T>, ofs: uint) -> T {
*/ */
pub fn set<T: Copy>(t: CVec<T>, ofs: uint, v: T) { pub fn set<T: Copy>(t: CVec<T>, ofs: uint, v: T) {
assert ofs < len(t); assert ofs < len(t);
unsafe { *ptr::mut_offset((*t).base, ofs) = v }; unsafe { *ptr::mut_offset(t.base, ofs) = v };
} }
/* /*
@ -141,14 +141,10 @@ pub fn set<T: Copy>(t: CVec<T>, ofs: uint, v: T) {
*/ */
/// Returns the length of the vector /// Returns the length of the vector
pub pure fn len<T>(t: CVec<T>) -> uint { pub pure fn len<T>(t: CVec<T>) -> uint { t.len }
return (*t).len;
}
/// Returns a pointer to the first element of the vector /// Returns a pointer to the first element of the vector
pub unsafe fn ptr<T>(t: CVec<T>) -> *mut T { pub unsafe fn ptr<T>(t: CVec<T>) -> *mut T { t.base }
return (*t).base;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -84,22 +84,27 @@ pub mod reader {
} }
} }
fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} { struct Res {
val: uint,
next: uint
}
fn vuint_at(data: &[u8], start: uint) -> Res {
let a = data[start]; let a = data[start];
if a & 0x80u8 != 0u8 { if a & 0x80u8 != 0u8 {
return {val: (a & 0x7fu8) as uint, next: start + 1u}; return Res {val: (a & 0x7fu8) as uint, next: start + 1u};
} }
if a & 0x40u8 != 0u8 { if a & 0x40u8 != 0u8 {
return {val: ((a & 0x3fu8) as uint) << 8u | return Res {val: ((a & 0x3fu8) as uint) << 8u |
(data[start + 1u] as uint), (data[start + 1u] as uint),
next: start + 2u}; next: start + 2u};
} else if a & 0x20u8 != 0u8 { } else if a & 0x20u8 != 0u8 {
return {val: ((a & 0x1fu8) as uint) << 16u | return Res {val: ((a & 0x1fu8) as uint) << 16u |
(data[start + 1u] as uint) << 8u | (data[start + 1u] as uint) << 8u |
(data[start + 2u] as uint), (data[start + 2u] as uint),
next: start + 3u}; next: start + 3u};
} else if a & 0x10u8 != 0u8 { } else if a & 0x10u8 != 0u8 {
return {val: ((a & 0x0fu8) as uint) << 24u | return Res {val: ((a & 0x0fu8) as uint) << 24u |
(data[start + 1u] as uint) << 16u | (data[start + 1u] as uint) << 16u |
(data[start + 2u] as uint) << 8u | (data[start + 2u] as uint) << 8u |
(data[start + 3u] as uint), (data[start + 3u] as uint),

View file

@ -14,7 +14,14 @@ use core::str;
use core::uint; use core::uint;
use core::vec; use core::vec;
pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { struct Quad {
a: u32,
b: u32,
c: u32,
d: u32
}
pub pure fn md4(msg: &[u8]) -> Quad {
// subtle: if orig_len is merely uint, then the code below // subtle: if orig_len is merely uint, then the code below
// which performs shifts by 32 bits or more has undefined // which performs shifts by 32 bits or more has undefined
// results. // results.
@ -95,11 +102,11 @@ pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
a += aa; b += bb; c += cc; d += dd; a += aa; b += bb; c += cc; d += dd;
i += 64u; i += 64u;
} }
return {a: a, b: b, c: c, d: d}; return Quad {a: a, b: b, c: c, d: d};
} }
pub pure fn md4_str(msg: &[u8]) -> ~str { pub pure fn md4_str(msg: &[u8]) -> ~str {
let {a, b, c, d} = md4(msg); let Quad {a, b, c, d} = md4(msg);
pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
f(a); f(b); f(c); f(d); f(a); f(b); f(c); f(d);
} }

View file

@ -66,7 +66,7 @@ const k3: u32 = 0xCA62C1D6u32;
/// Construct a `sha` object /// Construct a `sha` object
pub fn sha1() -> Sha1 { pub fn sha1() -> Sha1 {
type Sha1State = struct Sha1State
{h: ~[mut u32], {h: ~[mut u32],
mut len_low: u32, mut len_low: u32,
mut len_high: u32, mut len_high: u32,
@ -258,7 +258,7 @@ pub fn sha1() -> Sha1 {
return s; return s;
} }
} }
let st = { let st = Sha1State {
h: vec::cast_to_mut(vec::from_elem(digest_buf_len, 0u32)), h: vec::cast_to_mut(vec::from_elem(digest_buf_len, 0u32)),
mut len_low: 0u32, mut len_low: 0u32,
mut len_high: 0u32, mut len_high: 0u32,

View file

@ -34,10 +34,6 @@ not required in or otherwise suitable for the core library.
#[forbid(deprecated_pattern)]; #[forbid(deprecated_pattern)];
#[allow(deprecated_self)]; #[allow(deprecated_self)];
// Transitional
#[legacy_records];
#[no_core]; #[no_core];
extern mod core(vers = "0.6"); extern mod core(vers = "0.6");

View file

@ -92,7 +92,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
}; };
let filter = let filter =
if vec::len(matches.free) > 0u { if vec::len(matches.free) > 0 {
option::Some(matches.free[0]) option::Some(matches.free[0])
} else { option::None }; } else { option::None };
@ -111,26 +111,27 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
#[deriving_eq] #[deriving_eq]
pub enum TestResult { TrOk, TrFailed, TrIgnored, } pub enum TestResult { TrOk, TrFailed, TrIgnored, }
type ConsoleTestState = struct ConsoleTestState {
@{out: io::Writer, out: io::Writer,
log_out: Option<io::Writer>, log_out: Option<io::Writer>,
use_color: bool, use_color: bool,
mut total: uint, mut total: uint,
mut passed: uint, mut passed: uint,
mut failed: uint, mut failed: uint,
mut ignored: uint, mut ignored: uint,
mut failures: ~[TestDesc]}; mut failures: ~[TestDesc]
}
// A simple console test runner // A simple console test runner
pub fn run_tests_console(opts: &TestOpts, pub fn run_tests_console(opts: &TestOpts,
tests: &[TestDesc]) -> bool { tests: &[TestDesc]) -> bool {
fn callback(event: &TestEvent, st: ConsoleTestState) { fn callback(event: &TestEvent, st: @ConsoleTestState) {
debug!("callback(event=%?)", event); debug!("callback(event=%?)", event);
match *event { match *event {
TeFiltered(ref filtered_tests) => { TeFiltered(ref filtered_tests) => {
st.total = filtered_tests.len(); st.total = filtered_tests.len();
let noun = if st.total != 1u { ~"tests" } else { ~"test" }; let noun = if st.total != 1 { ~"tests" } else { ~"test" };
st.out.write_line(fmt!("\nrunning %u %s", st.total, noun)); st.out.write_line(fmt!("\nrunning %u %s", st.total, noun));
} }
TeWait(ref test) => st.out.write_str( TeWait(ref test) => st.out.write_str(
@ -142,18 +143,18 @@ pub fn run_tests_console(opts: &TestOpts,
} }
match result { match result {
TrOk => { TrOk => {
st.passed += 1u; st.passed += 1;
write_ok(st.out, st.use_color); write_ok(st.out, st.use_color);
st.out.write_line(~""); st.out.write_line(~"");
} }
TrFailed => { TrFailed => {
st.failed += 1u; st.failed += 1;
write_failed(st.out, st.use_color); write_failed(st.out, st.use_color);
st.out.write_line(~""); st.out.write_line(~"");
st.failures.push(move test); st.failures.push(move test);
} }
TrIgnored => { TrIgnored => {
st.ignored += 1u; st.ignored += 1;
write_ignored(st.out, st.use_color); write_ignored(st.out, st.use_color);
st.out.write_line(~""); st.out.write_line(~"");
} }
@ -174,19 +175,19 @@ pub fn run_tests_console(opts: &TestOpts,
}; };
let st = let st =
@{out: io::stdout(), @ConsoleTestState{out: io::stdout(),
log_out: log_out, log_out: log_out,
use_color: use_color(), use_color: use_color(),
mut total: 0u, mut total: 0,
mut passed: 0u, mut passed: 0,
mut failed: 0u, mut failed: 0,
mut ignored: 0u, mut ignored: 0,
mut failures: ~[]}; mut failures: ~[]};
run_tests(opts, tests, |x| callback(&x, st)); run_tests(opts, tests, |x| callback(&x, st));
assert (st.passed + st.failed + st.ignored == st.total); assert (st.passed + st.failed + st.ignored == st.total);
let success = st.failed == 0u; let success = st.failed == 0;
if !success { if !success {
print_failures(st); print_failures(st);
@ -234,7 +235,7 @@ pub fn run_tests_console(opts: &TestOpts,
} }
} }
fn print_failures(st: ConsoleTestState) { fn print_failures(st: @ConsoleTestState) {
st.out.write_line(~"\nfailures:"); st.out.write_line(~"\nfailures:");
let failures = copy st.failures; let failures = copy st.failures;
let failures = vec::map(failures, |test| test.name); let failures = vec::map(failures, |test| test.name);
@ -262,13 +263,13 @@ fn should_sort_failures_before_printing_them() {
}; };
let st = let st =
@{out: wr, @ConsoleTestState{out: wr,
log_out: option::None, log_out: option::None,
use_color: false, use_color: false,
mut total: 0u, mut total: 0,
mut passed: 0u, mut passed: 0,
mut failed: 0u, mut failed: 0,
mut ignored: 0u, mut ignored: 0,
mut failures: ~[move test_b, move test_a]}; mut failures: ~[move test_b, move test_a]};
print_failures(st); print_failures(st);
@ -279,7 +280,7 @@ fn should_sort_failures_before_printing_them() {
assert apos < bpos; assert apos < bpos;
} }
fn use_color() -> bool { return get_concurrency() == 1u; } fn use_color() -> bool { return get_concurrency() == 1; }
enum TestEvent { enum TestEvent {
TeFiltered(~[TestDesc]), TeFiltered(~[TestDesc]),
@ -334,7 +335,7 @@ fn run_tests(opts: &TestOpts,
// Windows tends to dislike being overloaded with threads. // Windows tends to dislike being overloaded with threads.
#[cfg(windows)] #[cfg(windows)]
const sched_overcommit : uint = 1u; const sched_overcommit : uint = 1;
#[cfg(unix)] #[cfg(unix)]
const sched_overcommit : uint = 4u; const sched_overcommit : uint = 4u;
@ -342,7 +343,7 @@ const sched_overcommit : uint = 4u;
fn get_concurrency() -> uint { fn get_concurrency() -> uint {
unsafe { unsafe {
let threads = rustrt::rust_sched_threads() as uint; let threads = rustrt::rust_sched_threads() as uint;
if threads == 1u { 1u } if threads == 1 { 1 }
else { threads * sched_overcommit } else { threads * sched_overcommit }
} }
} }
@ -556,7 +557,7 @@ mod tests {
]; ];
let filtered = filter_tests(&opts, tests); let filtered = filter_tests(&opts, tests);
assert (vec::len(filtered) == 1u); assert (vec::len(filtered) == 1);
assert (filtered[0].name == ~"1"); assert (filtered[0].name == ~"1");
assert (filtered[0].ignore == false); assert (filtered[0].ignore == false);
} }

View file

@ -27,19 +27,17 @@ use core::task::TaskBuilder;
use core::task; use core::task;
/// Used to abstract-away direct interaction with a libuv loop. /// Used to abstract-away direct interaction with a libuv loop.
pub enum IoTask { pub struct IoTask {
IoTask_({ async_handle: *ll::uv_async_t,
async_handle: *ll::uv_async_t, op_chan: SharedChan<IoTaskMsg>
op_chan: SharedChan<IoTaskMsg>
})
} }
impl IoTask: Clone { impl IoTask: Clone {
fn clone(&self) -> IoTask { fn clone(&self) -> IoTask {
IoTask_({ IoTask{
async_handle: self.async_handle, async_handle: self.async_handle,
op_chan: self.op_chan.clone() op_chan: self.op_chan.clone()
}) }
} }
} }
@ -131,10 +129,10 @@ fn run_loop(iotask_ch: &Chan<IoTask>) {
// Send out a handle through which folks can talk to us // Send out a handle through which folks can talk to us
// while we dwell in the I/O loop // while we dwell in the I/O loop
let iotask = IoTask_({ let iotask = IoTask{
async_handle: async_handle, async_handle: async_handle,
op_chan: SharedChan(msg_ch) op_chan: SharedChan(msg_ch)
}); };
iotask_ch.send(iotask); iotask_ch.send(iotask);
log(debug, ~"about to run uv loop"); log(debug, ~"about to run uv loop");

View file

@ -1543,7 +1543,7 @@ pub mod test {
let continue_async_handle_ptr = let continue_async_handle_ptr =
ptr::addr_of(&continue_async_handle); ptr::addr_of(&continue_async_handle);
let async_data = let async_data =
{ continue_chan: continue_chan }; async_handle_data { continue_chan: continue_chan };
let async_data_ptr = ptr::addr_of(&async_data); let async_data_ptr = ptr::addr_of(&async_data);
let server_data = tcp_server_data { let server_data = tcp_server_data {