From 0e1a4a4da29caadba5d5cd86a5cd2587fdb0755d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 25 Aug 2012 16:53:44 -0700 Subject: [PATCH 1/9] libcore: rewrite vec::unsafe::from_buf in pure rust --- src/libcore/vec.rs | 12 +++++------- src/rt/rust_builtin.cpp | 19 ++----------------- src/rt/rustrt.def.in | 1 - 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 1533b07861c96..448678abf7c60 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -102,9 +102,6 @@ extern mod rustrt { fn vec_reserve_shared(++t: *sys::TypeDesc, ++v: **unsafe::VecRepr, ++n: libc::size_t); - fn vec_from_buf_shared(++t: *sys::TypeDesc, - ++ptr: *(), - ++count: libc::size_t) -> *unsafe::VecRepr; } #[abi = "rust-intrinsic"] @@ -1727,10 +1724,11 @@ mod unsafe { */ #[inline(always)] unsafe fn from_buf(ptr: *T, elts: uint) -> ~[T] { - return ::unsafe::reinterpret_cast( - rustrt::vec_from_buf_shared(sys::get_type_desc::(), - ptr as *(), - elts as size_t)); + let mut dst = ~[]; + reserve(dst, elts); + set_len(dst, elts); + as_buf(dst, |p_dst, _len_dst| ptr::memcpy(p_dst, ptr, elts)); + dst } /** diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index cabc086b64ff0..a74f5985d36c2 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -148,22 +148,6 @@ str_reserve_shared(rust_vec_box** sp, reserve_vec_exact(task, sp, n_elts + 1); } -/** - * Copies elements in an unsafe buffer to the given interior vector. The - * vector must have size zero. - */ -extern "C" CDECL rust_vec_box* -vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) { - rust_task *task = rust_get_current_task(); - size_t fill = ty->size * count; - rust_vec_box* v = (rust_vec_box*) - task->kernel->malloc(fill + sizeof(rust_vec_box), - "vec_from_buf"); - v->body.fill = v->body.alloc = fill; - memmove(&v->body.data[0], ptr, fill); - return v; -} - extern "C" CDECL void rust_str_push(rust_vec_box** sp, uint8_t byte) { rust_task *task = rust_get_current_task(); @@ -515,8 +499,9 @@ void tm_to_rust_tm(tm* in_tm, rust_tm* out_tm, int32_t gmtoff, out_tm->tm_nsec = nsec; if (zone != NULL) { + rust_task *task = rust_get_current_task(); size_t size = strlen(zone); - str_reserve_shared(&out_tm->tm_zone, size); + reserve_vec_exact(task, &out_tm->tm_zone, size + 1); memcpy(out_tm->tm_zone->body.data, zone, size); out_tm->tm_zone->body.fill = size + 1; out_tm->tm_zone->body.data[size] = '\0'; diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index b4db45ab3a0a3..141937356e6e8 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -63,7 +63,6 @@ start_task vec_reserve_shared_actual vec_reserve_shared str_reserve_shared -vec_from_buf_shared task_clear_event_reject task_wait_event task_signal_event From 94c3975a9a4948a17cc63022e3ba5dbe87171a9d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 26 Aug 2012 09:16:36 -0700 Subject: [PATCH 2/9] libcore: str::push_char should use str::unsafe::set_len. --- src/libcore/str.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 7e13e75a93d57..c7921ade27f7e 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -220,14 +220,9 @@ fn push_char(&s: ~str, ch: char) { *ptr::mut_offset(buf, off + 5u) = (code & 63u | tag_cont) as u8; } - *ptr::mut_offset(buf, off + nb) = 0u8; } - do as_bytes(s) |bytes| { - let mut mut_bytes: ~[u8] = ::unsafe::reinterpret_cast(bytes); - vec::unsafe::set_len(mut_bytes, new_len + 1u); - ::unsafe::forget(mut_bytes); - } + unsafe::set_len(s, new_len); } } From 7bb65848a15c2970f1b7a52e870bfc98c9c981a9 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 25 Aug 2012 16:56:15 -0700 Subject: [PATCH 3/9] libcore: Rewrite str::unsafe::push_byte in pure rust. --- src/libcore/str.rs | 11 ++++++++--- src/rt/rust_builtin.cpp | 10 ---------- src/rt/rustrt.def.in | 1 - 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index c7921ade27f7e..7e5778f04454f 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -117,7 +117,6 @@ export #[abi = "cdecl"] extern mod rustrt { - fn rust_str_push(&s: ~str, ch: u8); fn str_reserve_shared(&ss: ~str, nn: libc::size_t); } @@ -1998,12 +1997,18 @@ mod unsafe { /// Appends a byte to a string. (Not UTF-8 safe). unsafe fn push_byte(&s: ~str, b: u8) { - rustrt::rust_str_push(s, b); + reserve_at_least(s, s.len() + 1); + do as_buf(s) |buf, len| { + let buf: *mut u8 = ::unsafe::reinterpret_cast(buf); + *ptr::mut_offset(buf, len) = b; + } + set_len(s, s.len() + 1); } /// Appends a vector of bytes to a string. (Not UTF-8 safe). unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) { - for vec::each(bytes) |byte| { rustrt::rust_str_push(s, byte); } + reserve_at_least(s, s.len() + bytes.len()); + for vec::each(bytes) |byte| { push_byte(s, byte); } } /// Removes the last byte from a string and returns it. (Not UTF-8 safe). diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a74f5985d36c2..a662141bf6284 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -148,16 +148,6 @@ str_reserve_shared(rust_vec_box** sp, reserve_vec_exact(task, sp, n_elts + 1); } -extern "C" CDECL void -rust_str_push(rust_vec_box** sp, uint8_t byte) { - rust_task *task = rust_get_current_task(); - size_t fill = (*sp)->body.fill; - reserve_vec(task, sp, fill + 1); - (*sp)->body.data[fill-1] = byte; - (*sp)->body.data[fill] = 0; - (*sp)->body.fill = fill + 1; -} - extern "C" CDECL rust_vec* rand_seed() { size_t size = sizeof(ub4) * RANDSIZ; diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 141937356e6e8..191ebe6f0beab 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -38,7 +38,6 @@ rust_getcwd rust_get_stdin rust_get_stdout rust_get_stderr -rust_str_push rust_list_files rust_log_console_on rust_log_console_off From 9074d9b5813c43cb1d4920dbc9de7dfde032577a Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 26 Aug 2012 21:21:59 -0700 Subject: [PATCH 4/9] libcore: rewrite io::MemBuffer.write to use memcpy (#2004) --- src/libcore/io.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 69e95274f7c6b..9ba942cb0b76e 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -663,24 +663,21 @@ type MemBuffer = @{buf: DVec, mut pos: uint}; impl MemBuffer: Writer { fn write(v: &[const u8]) { - // Fast path. - let vlen = vec::len(v); - let buf_len = self.buf.len(); - if self.pos == buf_len { - self.buf.push_all(v); - self.pos += vlen; - return; - } + do self.buf.swap |buf| { + let mut buf <- buf; + let v_len = v.len(); + let buf_len = buf.len(); + + let count = uint::max(&buf_len, &(self.pos + v_len)); + vec::reserve(buf, count); + unsafe { vec::unsafe::set_len(buf, count); } + + vec::u8::memcpy(vec::mut_view(buf, self.pos, count), v, v_len); + + self.pos += v_len; - // FIXME #2004--use memcpy here? - let mut pos = self.pos, vpos = 0u; - while vpos < vlen && pos < buf_len { - self.buf.set_elt(pos, copy v[vpos]); - pos += 1u; - vpos += 1u; + buf } - self.buf.push_slice(v, vpos, vlen); - self.pos += vlen; } fn seek(offset: int, whence: SeekStyle) { let pos = self.pos; From 628b94618695fdf24b5b8cc9f56af6d86efa265d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 27 Aug 2012 07:23:55 -0700 Subject: [PATCH 5/9] libcore: rewrite str::reserve in pure rust. --- src/libcore/str.rs | 10 +++------- src/rt/rust_builtin.cpp | 7 ------- src/rt/rustrt.def.in | 1 - 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 7e5778f04454f..888d992321cdd 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -115,11 +115,6 @@ export StrSlice, UniqueStr; -#[abi = "cdecl"] -extern mod rustrt { - fn str_reserve_shared(&ss: ~str, nn: libc::size_t); -} - /* Section: Creating a string */ @@ -1818,8 +1813,9 @@ pure fn as_buf(s: &str, f: fn(*u8, uint) -> T) -> T { * * n - The number of bytes to reserve space for */ fn reserve(&s: ~str, n: uint) { - if capacity(s) < n { - rustrt::str_reserve_shared(s, n as size_t); + unsafe { + let v: *mut ~[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s)); + vec::reserve(*v, n + 1); } } diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a662141bf6284..f9da1c91cf0d8 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -141,13 +141,6 @@ vec_reserve_shared(type_desc* ty, rust_vec_box** vp, reserve_vec_exact(task, vp, n_elts * ty->size); } -extern "C" CDECL void -str_reserve_shared(rust_vec_box** sp, - size_t n_elts) { - rust_task *task = rust_get_current_task(); - reserve_vec_exact(task, sp, n_elts + 1); -} - extern "C" CDECL rust_vec* rand_seed() { size_t size = sizeof(ub4) * RANDSIZ; diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 191ebe6f0beab..8c550833d1c38 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -61,7 +61,6 @@ shape_log_str start_task vec_reserve_shared_actual vec_reserve_shared -str_reserve_shared task_clear_event_reject task_wait_event task_signal_event From c0f357924d8c96d2f8510055ecd79ed0f3e1840b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 29 Aug 2012 21:20:10 -0700 Subject: [PATCH 6/9] libcore: vec::u8::hash is pure. --- src/libcore/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 448678abf7c60..984092e9a584d 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1877,7 +1877,7 @@ mod u8 { pure fn gt(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) > 0 } /// Byte-vec hash function - fn hash(s: &~[u8]) -> uint { + pure fn hash(s: &~[u8]) -> uint { hash::hash_bytes(*s) as uint } From afeaf7d88cda883428f0b18123f164e5d5a7e4f5 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 30 Aug 2012 13:05:52 -0700 Subject: [PATCH 7/9] libcore: fix a typo --- src/libcore/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index bdd32aa8e6667..1ac1b78f3ec6c 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -53,7 +53,7 @@ pure fn get_ref(res: &a/Result) -> &a/T { pure fn get_err(res: Result) -> U { match res { Err(u) => u, - Ok(_) => fail ~"get_error called on ok result" + Ok(_) => fail ~"get_err called on ok result" } } From 7649860339a0e298be75522d41d243b3bbb178b6 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 30 Aug 2012 15:54:05 -0700 Subject: [PATCH 8/9] libcore: result::unwrap should use moves --- src/libcore/result.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 1ac1b78f3ec6c..b9c1d8cbc0e5c 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -341,15 +341,10 @@ fn iter_vec2(ss: &[S], ts: &[T], } /// Unwraps a result, assuming it is an `ok(T)` -fn unwrap(-res: Result) -> T { - unsafe { - let addr = match res { - Ok(x) => ptr::addr_of(x), - Err(_) => fail ~"error result" - }; - let liberated_value = unsafe::reinterpret_cast(*addr); - unsafe::forget(res); - return liberated_value; +fn unwrap(+res: Result) -> T { + match move res { + Ok(move t) => t, + Err(_) => fail ~"unwrap called on an err result" } } From 5c6be13b29c44920a39b9b9e95931f734b60d92b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 30 Aug 2012 15:54:16 -0700 Subject: [PATCH 9/9] libcore: add result::unwrap_err. --- src/libcore/result.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index b9c1d8cbc0e5c..ba8e796313328 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -348,6 +348,14 @@ fn unwrap(+res: Result) -> T { } } +/// Unwraps a result, assuming it is an `err(U)` +fn unwrap_err(+res: Result) -> U { + match move res { + Err(move u) => u, + Ok(_) => fail ~"unwrap called on an ok result" + } +} + impl Result : Eq { pure fn eq(&&other: Result) -> bool { match self {