From 5d540de76993eb6dac9893138e45d0324c23e631 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 12 Sep 2012 10:38:17 -0700 Subject: [PATCH] fixup mutability of vec::each, make iter_bytes pure also, change DVec() to work with imm vectors rather than mut ones --- src/libcore/dvec.rs | 46 ++++-- src/libcore/hash.rs | 4 +- src/libcore/io.rs | 2 +- src/libcore/ptr.rs | 50 ++++-- src/libcore/str.rs | 8 +- src/libcore/to_bytes.rs | 75 +++++---- src/libcore/unsafe.rs | 12 ++ src/libcore/vec.rs | 149 ++++++++++++------ src/libstd/arena.rs | 6 +- src/libstd/deque.rs | 7 +- src/libstd/json.rs | 4 +- src/libstd/net_url.rs | 8 +- src/libstd/set.rs | 62 ++++++++ src/libstd/sha1.rs | 2 +- src/libstd/smallintmap.rs | 2 +- src/libsyntax/ast.rs | 22 +-- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/ext/qquote.rs | 2 +- src/libsyntax/parse/obsolete.rs | 2 +- src/libsyntax/parse/parser.rs | 4 +- src/rustc/middle/astencode.rs | 2 +- src/rustc/middle/borrowck.rs | 2 +- src/rustc/middle/borrowck/gather_loans.rs | 2 +- src/rustc/middle/trans/alt.rs | 2 +- src/rustc/middle/trans/build.rs | 2 +- src/rustc/middle/trans/common.rs | 4 +- src/rustc/middle/trans/shape.rs | 2 +- src/rustc/middle/trans/type_use.rs | 2 +- src/rustc/middle/ty.rs | 38 ++--- .../typeck/infer/region_var_bindings.rs | 4 +- src/test/bench/core-vec-append.rs | 4 +- src/test/compile-fail/issue-2590.rs | 4 +- src/test/run-pass/dvec-test.rs | 4 +- src/test/run-pass/issue-2631-b.rs | 2 +- 34 files changed, 357 insertions(+), 186 deletions(-) create mode 100644 src/libstd/set.rs diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index ed8a814bba8..d59d3828206 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -50,7 +50,7 @@ export unwrap; * type could only produce 47 million pushes/second. */ type DVec_ = { - mut data: ~[mut A] + mut data: ~[A] }; enum DVec { @@ -59,21 +59,21 @@ enum DVec { /// Creates a new, empty dvec fn DVec() -> DVec { - DVec_({mut data: ~[mut]}) + DVec_({mut data: ~[]}) } /// Creates a new dvec with a single element fn from_elem(+e: A) -> DVec { - DVec_({mut data: ~[mut move e]}) + DVec_({mut data: ~[move e]}) } /// Creates a new dvec with the contents of a vector -fn from_vec(+v: ~[mut A]) -> DVec { +fn from_vec(+v: ~[A]) -> DVec { DVec_({mut data: move v}) } /// Consumes the vector and returns its contents -fn unwrap(+d: DVec) -> ~[mut A] { +fn unwrap(+d: DVec) -> ~[A] { let DVec_({data: v}) <- d; move v } @@ -89,7 +89,7 @@ priv impl DVec { } #[inline(always)] - fn check_out(f: fn(-~[mut A]) -> B) -> B { + fn check_out(f: fn(-~[A]) -> B) -> B { unsafe { let mut data = unsafe::reinterpret_cast(&null::<()>()); data <-> self.data; @@ -100,9 +100,9 @@ priv impl DVec { } #[inline(always)] - fn give_back(-data: ~[mut A]) { + fn give_back(+data: ~[A]) { unsafe { - self.data <- data; + self.data = move data; } } } @@ -122,10 +122,22 @@ impl DVec { * and return a new vector to replace it with. */ #[inline(always)] - fn swap(f: fn(-~[mut A]) -> ~[mut A]) { + fn swap(f: fn(-~[A]) -> ~[A]) { self.check_out(|v| self.give_back(f(move v))) } + /** + * Swaps out the current vector and hands it off to a user-provided + * function `f`. The function should transform it however is desired + * and return a new vector to replace it with. + */ + #[inline(always)] + fn swap_mut(f: fn(-~[mut A]) -> ~[mut A]) { + do self.swap |v| { + vec::from_mut(f(vec::to_mut(move v))) + } + } + /// Returns the number of elements currently in the dvec pure fn len() -> uint { unchecked { @@ -138,7 +150,7 @@ impl DVec { } /// Overwrite the current contents - fn set(+w: ~[mut A]) { + fn set(+w: ~[A]) { self.check_not_borrowed(); self.data <- w; } @@ -161,7 +173,7 @@ impl DVec { let data_ptr: *() = unsafe::reinterpret_cast(&data); if data_ptr.is_null() { fail ~"Recursive use of dvec"; } log(error, ~"a"); - self.data <- ~[mut move t]; + self.data <- ~[move t]; vec::push_all_move(self.data, move data); log(error, ~"b"); } @@ -176,9 +188,9 @@ impl DVec { /// Remove and return the first element fn shift() -> A { do self.check_out |v| { - let mut v = vec::from_mut(move v); + let mut v = move v; let result = vec::shift(v); - self.give_back(vec::to_mut(move v)); + self.give_back(move v); move result } } @@ -186,6 +198,7 @@ impl DVec { /// Reverse the elements in the list, in place fn reverse() { do self.check_out |v| { + let mut v = move v; vec::reverse(v); self.give_back(move v); } @@ -203,6 +216,7 @@ impl DVec { /// Gives access to the vector as a slice with mutable contents fn borrow_mut(op: fn(x: &[mut A]) -> R) -> R { do self.check_out |v| { + let mut v = move v; let result = op(v); self.give_back(move v); move result @@ -268,7 +282,7 @@ impl DVec { pure fn get() -> ~[A] { unchecked { do self.check_out |v| { - let w = vec::from_mut(copy v); + let w = copy v; self.give_back(move v); move w } @@ -295,9 +309,9 @@ impl DVec { */ fn grow_set_elt(idx: uint, initval: A, val: A) { do self.swap |v| { - let mut v <- v; + let mut v = vec::to_mut(move v); vec::grow_set(v, idx, initval, val); - move v + move vec::from_mut(v) } } diff --git a/src/libcore/hash.rs b/src/libcore/hash.rs index 9fdfd7b102e..13fef207fac 100644 --- a/src/libcore/hash.rs +++ b/src/libcore/hash.rs @@ -130,7 +130,7 @@ pure fn hash_keyed_5 u64 { +pure fn hash_bytes_keyed(val: &[u8], k0: u64, k1: u64) -> u64 { val.hash_keyed(k0, k1) } pure fn hash_str_keyed(val: &str, k0: u64, k1: u64) -> u64 { @@ -152,7 +152,7 @@ pure fn hash_uint_keyed(val: uint, k0: u64, k1: u64) -> u64 { val.hash_keyed(k0, k1) } -pure fn hash_bytes(val: &[const u8]) -> u64 { hash_bytes_keyed(val, 0, 0) } +pure fn hash_bytes(val: &[u8]) -> u64 { hash_bytes_keyed(val, 0, 0) } pure fn hash_str(val: &str) -> u64 { hash_str_keyed(val, 0, 0) } pure fn hash_u64(val: u64) -> u64 { hash_u64_keyed(val, 0, 0) } pure fn hash_u32(val: u32) -> u64 { hash_u32_keyed(val, 0, 0) } diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 200d448e6b1..9d4176ea40b 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -214,7 +214,7 @@ fn convert_whence(whence: SeekStyle) -> i32 { impl *libc::FILE: Reader { fn read(buf: &[mut u8], len: uint) -> uint { - do vec::as_buf(buf) |buf_p, buf_len| { + do vec::as_mut_buf(buf) |buf_p, buf_len| { assert buf_len <= len; let count = libc::fread(buf_p as *mut c_void, 1u as size_t, diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index be3ab40dfcd..79ec6fea668 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2,6 +2,7 @@ export addr_of; export to_unsafe_ptr; +export to_const_unsafe_ptr; export to_mut_unsafe_ptr; export mut_addr_of; export offset; @@ -26,11 +27,16 @@ use libc::{c_void, size_t}; #[abi = "cdecl"] extern mod libc_ { #[rust_stack] - fn memcpy(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void; + fn memcpy(dest: *mut c_void, src: *const c_void, + n: libc::size_t) -> *c_void; + #[rust_stack] - fn memmove(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void; + fn memmove(dest: *mut c_void, src: *const c_void, + n: libc::size_t) -> *c_void; + #[rust_stack] - fn memset(dest: *c_void, c: libc::c_int, len: libc::size_t) -> *c_void; + fn memset(dest: *mut c_void, c: libc::c_int, + len: libc::size_t) -> *c_void; } #[abi = "rust-intrinsic"] @@ -105,9 +111,9 @@ pure fn is_not_null(ptr: *const T) -> bool { !is_null(ptr) } * and destination may not overlap. */ #[inline(always)] -unsafe fn memcpy(dst: *T, src: *T, count: uint) { +unsafe fn memcpy(dst: *mut T, src: *const T, count: uint) { let n = count * sys::size_of::(); - libc_::memcpy(dst as *c_void, src as *c_void, n as size_t); + libc_::memcpy(dst as *mut c_void, src as *c_void, n as size_t); } /** @@ -117,15 +123,15 @@ unsafe fn memcpy(dst: *T, src: *T, count: uint) { * and destination may overlap. */ #[inline(always)] -unsafe fn memmove(dst: *T, src: *T, count: uint) { +unsafe fn memmove(dst: *mut T, src: *const T, count: uint) { let n = count * sys::size_of::(); - libc_::memmove(dst as *c_void, src as *c_void, n as size_t); + libc_::memmove(dst as *mut c_void, src as *c_void, n as size_t); } #[inline(always)] unsafe fn memset(dst: *mut T, c: int, count: uint) { let n = count * sys::size_of::(); - libc_::memset(dst as *c_void, c as libc::c_int, n as size_t); + libc_::memset(dst as *mut c_void, c as libc::c_int, n as size_t); } @@ -135,8 +141,18 @@ unsafe fn memset(dst: *mut T, c: int, count: uint) { reinterpret_cast. */ #[inline(always)] -fn to_unsafe_ptr(thing: &T) -> *T unsafe { - unsafe::reinterpret_cast(&thing) +fn to_unsafe_ptr(thing: &T) -> *T { + unsafe { unsafe::reinterpret_cast(&thing) } +} + +/** + Transform a const region pointer - &const T - to a const unsafe pointer - + *const T. This is safe, but is implemented with an unsafe block due to + reinterpret_cast. +*/ +#[inline(always)] +fn to_const_unsafe_ptr(thing: &const T) -> *const T { + unsafe { unsafe::reinterpret_cast(&thing) } } /** @@ -145,8 +161,8 @@ fn to_unsafe_ptr(thing: &T) -> *T unsafe { reinterpret_cast. */ #[inline(always)] -fn to_mut_unsafe_ptr(thing: &mut T) -> *mut T unsafe { - unsafe::reinterpret_cast(&thing) +fn to_mut_unsafe_ptr(thing: &mut T) -> *mut T { + unsafe { unsafe::reinterpret_cast(&thing) } } /** @@ -246,16 +262,16 @@ fn test() { assert (p.fst == 50); assert (p.snd == 60); - let v0 = ~[32000u16, 32001u16, 32002u16]; - let v1 = ~[0u16, 0u16, 0u16]; + let mut v0 = ~[32000u16, 32001u16, 32002u16]; + let mut v1 = ~[0u16, 0u16, 0u16]; - ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u), + ptr::memcpy(ptr::mut_offset(vec::unsafe::to_mut_ptr(v1), 1u), ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u); assert (v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16); - ptr::memcpy(vec::unsafe::to_ptr(v1), + ptr::memcpy(vec::unsafe::to_mut_ptr(v1), ptr::offset(vec::unsafe::to_ptr(v0), 2u), 1u); assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16); - ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u), + ptr::memcpy(ptr::mut_offset(vec::unsafe::to_mut_ptr(v1), 2u), vec::unsafe::to_ptr(v0), 1u); assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16); } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 3a2aa0b0f0d..88858e2b1ab 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -250,6 +250,7 @@ fn push_str_no_overallocate(&lhs: ~str, rhs: &str) { do as_buf(lhs) |lbuf, _llen| { do as_buf(rhs) |rbuf, _rlen| { let dst = ptr::offset(lbuf, llen); + let dst = ::unsafe::transmute_mut_unsafe(dst); ptr::memcpy(dst, rbuf, rlen); } } @@ -266,6 +267,7 @@ fn push_str(&lhs: ~str, rhs: &str) { do as_buf(lhs) |lbuf, _llen| { do as_buf(rhs) |rbuf, _rlen| { let dst = ptr::offset(lbuf, llen); + let dst = ::unsafe::transmute_mut_unsafe(dst); ptr::memcpy(dst, rbuf, rlen); } } @@ -1990,7 +1992,10 @@ mod unsafe { unsafe fn from_buf_len(buf: *const u8, len: uint) -> ~str { let mut v: ~[mut u8] = ~[mut]; vec::reserve(v, len + 1u); - vec::as_buf(v, |b, _len| ptr::memcpy(b, buf as *u8, len)); + vec::as_buf(v, |vbuf, _len| { + let vbuf = ::unsafe::transmute_mut_unsafe(vbuf); + ptr::memcpy(vbuf, buf as *u8, len) + }); vec::unsafe::set_len(v, len); vec::push(v, 0u8); @@ -2045,6 +2050,7 @@ mod unsafe { vec::reserve(v, end - begin + 1u); unsafe { do vec::as_buf(v) |vbuf, _vlen| { + let vbuf = ::unsafe::transmute_mut_unsafe(vbuf); let src = ptr::offset(sbuf, begin); ptr::memcpy(vbuf, src, end - begin); } diff --git a/src/libcore/to_bytes.rs b/src/libcore/to_bytes.rs index f619085bd4b..337aa5980d7 100644 --- a/src/libcore/to_bytes.rs +++ b/src/libcore/to_bytes.rs @@ -7,12 +7,12 @@ use io::Writer; type Cb = fn(buf: &[const u8]) -> bool; trait IterBytes { - fn iter_bytes(lsb0: bool, f: Cb); + pure fn iter_bytes(lsb0: bool, f: Cb); } impl u8: IterBytes { #[inline(always)] - fn iter_bytes(_lsb0: bool, f: Cb) { + pure fn iter_bytes(_lsb0: bool, f: Cb) { f([ self ]); @@ -21,7 +21,7 @@ impl u8: IterBytes { impl u16: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { if lsb0 { f([ self as u8, @@ -38,7 +38,7 @@ impl u16: IterBytes { impl u32: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { if lsb0 { f([ self as u8, @@ -59,7 +59,7 @@ impl u32: IterBytes { impl u64: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { if lsb0 { f([ self as u8, @@ -88,36 +88,43 @@ impl u64: IterBytes { impl i8: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u8).iter_bytes(lsb0, f) } } impl i16: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u16).iter_bytes(lsb0, f) } } impl i32: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u32).iter_bytes(lsb0, f) } } impl i64: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u64).iter_bytes(lsb0, f) } } +impl char: IterBytes { + #[inline(always)] + pure fn iter_bytes(lsb0: bool, f: Cb) { + (self as u32).iter_bytes(lsb0, f) + } +} + #[cfg(target_word_size = "32")] impl uint: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u32).iter_bytes(lsb0, f) } } @@ -125,21 +132,21 @@ impl uint: IterBytes { #[cfg(target_word_size = "64")] impl uint: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as u64).iter_bytes(lsb0, f) } } impl int: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as uint).iter_bytes(lsb0, f) } } -impl &[const A]: IterBytes { +impl &[A]: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { for self.each |elt| { do elt.iter_bytes(lsb0) |bytes| { f(bytes) @@ -149,26 +156,26 @@ impl &[const A]: IterBytes { } // Move this to vec, probably. -fn borrow(a: &x/[const A]) -> &x/[const A] { +pure fn borrow(a: &x/[A]) -> &x/[A] { a } -impl ~[const A]: IterBytes { +impl ~[A]: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { borrow(self).iter_bytes(lsb0, f) } } -impl @[const A]: IterBytes { +impl @[A]: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { borrow(self).iter_bytes(lsb0, f) } } -fn iter_bytes_2(a: &A, b: &B, +pure fn iter_bytes_2(a: &A, b: &B, lsb0: bool, z: Cb) { let mut flag = true; a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag}); @@ -176,7 +183,7 @@ fn iter_bytes_2(a: &A, b: &B, b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag}); } -fn iter_bytes_3(a: &A, b: &B, c: &C, lsb0: bool, z: Cb) { @@ -188,7 +195,7 @@ fn iter_bytes_3(a: &A, b: &B, c: &C, @@ -204,7 +211,7 @@ fn iter_bytes_4 Option: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { match self { Some(a) => iter_bytes_2(&0u8, &a, lsb0, f), None => 1u8.iter_bytes(lsb0, f) @@ -310,30 +317,30 @@ impl Option: IterBytes { impl &A: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (*self).iter_bytes(lsb0, f); } } impl @A: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (*self).iter_bytes(lsb0, f); } } impl ~A: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (*self).iter_bytes(lsb0, f); } } // NB: raw-pointer IterBytes does _not_ dereference // to the target; it just gives you the pointer-bytes. -impl *A: IterBytes { +impl *const A: IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: Cb) { + pure fn iter_bytes(lsb0: bool, f: Cb) { (self as uint).iter_bytes(lsb0, f); } } diff --git a/src/libcore/unsafe.rs b/src/libcore/unsafe.rs index eb3fd3974cd..8875fc1d0ea 100644 --- a/src/libcore/unsafe.rs +++ b/src/libcore/unsafe.rs @@ -2,6 +2,7 @@ export reinterpret_cast, forget, bump_box_refcount, transmute; export transmute_mut, transmute_immut, transmute_region, transmute_mut_region; +export transmute_mut_unsafe, transmute_immut_unsafe; export SharedMutableState, shared_mutable_state, clone_shared_mutable_state; export get_shared_mutable_state, get_shared_immutable_state; @@ -68,6 +69,12 @@ unsafe fn transmute_immut(+ptr: &a/mut T) -> &a/T { transmute(move ptr) } /// Coerce a borrowed pointer to have an arbitrary associated region. unsafe fn transmute_region(+ptr: &a/T) -> &b/T { transmute(move ptr) } +/// Coerce an immutable reference to be mutable. +unsafe fn transmute_mut_unsafe(+ptr: *const T) -> *mut T { transmute(ptr) } + +/// Coerce an immutable reference to be mutable. +unsafe fn transmute_immut_unsafe(+ptr: *const T) -> *T { transmute(ptr) } + /// Coerce a borrowed mutable pointer to have an arbitrary associated region. unsafe fn transmute_mut_region(+ptr: &a/mut T) -> &b/mut T { transmute(move ptr) @@ -78,6 +85,11 @@ unsafe fn copy_lifetime(_ptr: &a/S, ptr: &T) -> &a/T { transmute_region(ptr) } +/// Transforms lifetime of the second pointer to match the first. +unsafe fn copy_lifetime_to_unsafe(_ptr: &a/S, +ptr: *T) -> &a/T { + transmute(ptr) +} + /**************************************************************************** * Shared state & exclusive ARC diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 38340c2b8d1..62cc5ce6f60 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -77,7 +77,7 @@ export swap; export reverse; export reversed; export iter, iter_between, each, eachi, reach, reachi; -export each_mut, each_const; +export each_ref, each_mut_ref, each_const_ref; export iter2; export iteri; export riter; @@ -336,7 +336,8 @@ pure fn view(v: &[T], start: uint, end: uint) -> &[T] { do as_buf(v) |p, _len| { unsafe { ::unsafe::reinterpret_cast( - &(ptr::offset(p, start), (end - start) * sys::size_of::())) + &(ptr::offset(p, start), + (end - start) * sys::size_of::())) } } } @@ -345,10 +346,11 @@ pure fn view(v: &[T], start: uint, end: uint) -> &[T] { pure fn mut_view(v: &[mut T], start: uint, end: uint) -> &[mut T] { assert (start <= end); assert (end <= len(v)); - do as_buf(v) |p, _len| { + do as_mut_buf(v) |p, _len| { unsafe { ::unsafe::reinterpret_cast( - &(ptr::offset(p, start), (end - start) * sys::size_of::())) + &(ptr::mut_offset(p, start), + (end - start) * sys::size_of::())) } } } @@ -357,10 +359,11 @@ pure fn mut_view(v: &[mut T], start: uint, end: uint) -> &[mut T] { pure fn const_view(v: &[const T], start: uint, end: uint) -> &[const T] { assert (start <= end); assert (end <= len(v)); - do as_buf(v) |p, _len| { + do as_const_buf(v) |p, _len| { unsafe { ::unsafe::reinterpret_cast( - &(ptr::offset(p, start), (end - start) * sys::size_of::())) + &(ptr::const_offset(p, start), + (end - start) * sys::size_of::())) } } } @@ -1141,7 +1144,7 @@ fn swap(v: &[mut T], a: uint, b: uint) { } /// Reverse the order of elements in a vector, in place -fn reverse(v: ~[mut T]) { +fn reverse(v: &[mut T]) { let mut i: uint = 0u; let ln = len::(v); while i < ln / 2u { v[i] <-> v[ln - i - 1u]; i += 1u; } @@ -1203,7 +1206,12 @@ pure fn iter_between(v: &[T], start: uint, end: uint, f: fn(T)) { * Return true to continue, false to break. */ #[inline(always)] -pure fn each(v: &[const T], f: fn(T) -> bool) { +pure fn each(v: &[T], f: fn(T) -> bool) { + // ^^^^ + // NB---this CANNOT be &[const T]! The reason + // is that you are passing it to `f()` using + // an immutable. + do vec::as_buf(v) |p, n| { let mut n = n; let mut p = p; @@ -1217,21 +1225,52 @@ pure fn each(v: &[const T], f: fn(T) -> bool) { } } +/** + * Iterates over a vector, with option to break + * + * Return true to continue, false to break. + */ +#[inline(always)] +pure fn each_ref(v: &r/[T], f: fn(v: &r/T) -> bool) { + // this is not the most efficient impl, as it repeats the bound checks, + // but it's good enough + let mut i = 0; + let n = v.len(); + while i < n { + if !f(&v[i]) { + return; + } + i += 1; + } +} + /// Like `each()`, but for the case where you have /// a vector with mutable contents and you would like /// to mutate the contents as you iterate. #[inline(always)] -pure fn each_mut(v: &[mut T], f: fn(elem: &mut T) -> bool) { - do vec::as_mut_buf(v) |p, n| { - let mut n = n; - let mut p = p; - while n > 0u { - unsafe { - if !f(&mut *p) { break; } - p = ptr::mut_offset(p, 1u); - } - n -= 1u; +fn each_mut_ref(v: &[mut T], f: fn(elem: &mut T) -> bool) { + let mut i = 0; + let n = v.len(); + while i < n { + if !f(&mut v[i]) { + return; } + i += 1; + } +} + +/// Like `each()`, but for the case where you have +/// a vector with mutable contents and you would like +/// to mutate the contents as you iterate. +#[inline(always)] +pure fn each_const_ref(v: &[const T], f: fn(elem: &const T) -> bool) { + let mut i = 0; + let n = v.len(); + while i < n { + if !f(&const v[i]) { + return; + } + i += 1; } } @@ -1241,7 +1280,7 @@ pure fn each_mut(v: &[mut T], f: fn(elem: &mut T) -> bool) { * Return true to continue, false to break. */ #[inline(always)] -pure fn eachi(v: &[const T], f: fn(uint, T) -> bool) { +pure fn eachi(v: &[T], f: fn(uint, T) -> bool) { do vec::as_buf(v) |p, n| { let mut i = 0u; let mut p = p; @@ -1392,10 +1431,18 @@ pure fn windowed(nn: uint, xx: &[TT]) -> ~[~[TT]] { * foreign interop. */ #[inline(always)] -pure fn as_buf(s: &[const T], +pure fn as_buf(s: &[T], /* NB---this CANNOT be const, see below */ f: fn(*T, uint) -> U) -> U { + + // NB---People keep changing the type of s to `&[const T]`. This is + // unsound. The reason is that we are going to create immutable pointers + // into `s` and pass them to `f()`, but in fact they are potentially + // pointing at *mutable memory*. Use `as_const_buf` or `as_mut_buf` + // instead! + unsafe { - let v : *(*T,uint) = ::unsafe::reinterpret_cast(&ptr::addr_of(s)); + let v : *(*T,uint) = + ::unsafe::reinterpret_cast(&ptr::addr_of(s)); let (buf,len) = *v; f(buf, len / sys::size_of::()) } @@ -1405,11 +1452,12 @@ pure fn as_buf(s: &[const T], #[inline(always)] pure fn as_const_buf(s: &[const T], f: fn(*const T, uint) -> U) -> U { - do as_buf(s) |p, len| { - unsafe { - let pp : *const T = ::unsafe::reinterpret_cast(&p); - f(pp, len) - } + + unsafe { + let v : *(*const T,uint) = + ::unsafe::reinterpret_cast(&ptr::addr_of(s)); + let (buf,len) = *v; + f(buf, len / sys::size_of::()) } } @@ -1417,11 +1465,12 @@ pure fn as_const_buf(s: &[const T], #[inline(always)] pure fn as_mut_buf(s: &[mut T], f: fn(*mut T, uint) -> U) -> U { - do as_buf(s) |p, len| { - unsafe { - let pp : *mut T = ::unsafe::reinterpret_cast(&p); - f(pp, len) - } + + unsafe { + let v : *(*mut T,uint) = + ::unsafe::reinterpret_cast(&ptr::addr_of(s)); + let (buf,len) = *v; + f(buf, len / sys::size_of::()) } } @@ -1765,7 +1814,7 @@ mod unsafe { let mut dst = ~[]; reserve(dst, elts); set_len(dst, elts); - as_buf(dst, |p_dst, _len_dst| ptr::memcpy(p_dst, ptr, elts)); + as_mut_buf(dst, |p_dst, _len_dst| ptr::memcpy(p_dst, ptr, elts)); move dst } @@ -1792,18 +1841,24 @@ mod unsafe { * would also make any pointers to it invalid. */ #[inline(always)] - unsafe fn to_ptr(v: ~[const T]) -> *T { - let repr: **VecRepr = ::unsafe::reinterpret_cast(&addr_of(v)); - return ::unsafe::reinterpret_cast(&addr_of((**repr).data)); - } - - - #[inline(always)] - unsafe fn to_ptr_slice(v: &[const T]) -> *T { + unsafe fn to_ptr(v: &[T]) -> *T { let repr: **SliceRepr = ::unsafe::reinterpret_cast(&addr_of(v)); return ::unsafe::reinterpret_cast(&addr_of((**repr).data)); } + /** see `to_ptr()` */ + #[inline(always)] + unsafe fn to_const_ptr(v: &[const T]) -> *const T { + let repr: **SliceRepr = ::unsafe::reinterpret_cast(&addr_of(v)); + return ::unsafe::reinterpret_cast(&addr_of((**repr).data)); + } + + /** see `to_ptr()` */ + #[inline(always)] + unsafe fn to_mut_ptr(v: &[mut T]) -> *mut T { + let repr: **SliceRepr = ::unsafe::reinterpret_cast(&addr_of(v)); + return ::unsafe::reinterpret_cast(&addr_of((**repr).data)); + } /** * Form a slice from a pointer and length (as a number of units, @@ -1822,7 +1877,7 @@ mod unsafe { */ #[inline(always)] unsafe fn get(v: &[const T], i: uint) -> T { - as_buf(v, |p, _len| *ptr::offset(p, i)) + as_const_buf(v, |p, _len| *ptr::const_offset(p, i)) } /** @@ -1846,8 +1901,8 @@ mod unsafe { * may overlap. */ unsafe fn memcpy(dst: &[mut T], src: &[const T], count: uint) { - do as_buf(dst) |p_dst, _len_dst| { - do as_buf(src) |p_src, _len_src| { + do as_mut_buf(dst) |p_dst, _len_dst| { + do as_const_buf(src) |p_src, _len_src| { ptr::memcpy(p_dst, p_src, count) } } @@ -1860,8 +1915,8 @@ mod unsafe { * may overlap. */ unsafe fn memmove(dst: &[mut T], src: &[const T], count: uint) { - do as_buf(dst) |p_dst, _len_dst| { - do as_buf(src) |p_src, _len_src| { + do as_mut_buf(dst) |p_dst, _len_dst| { + do as_const_buf(src) |p_src, _len_src| { ptr::memmove(p_dst, p_src, count) } } @@ -1952,12 +2007,12 @@ mod u8 { // This cannot be used with iter-trait.rs because of the region pointer // required in the slice. -impl &[const A]: iter::BaseIter { +impl &[A]: iter::BaseIter { pure fn each(blk: fn(A) -> bool) { each(self, blk) } pure fn size_hint() -> Option { Some(len(self)) } } -impl &[const A]: iter::ExtendedIter { +impl &[A]: iter::ExtendedIter { pure fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) } pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) } pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) } diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index a9010759ef9..173348d8dca 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -89,7 +89,7 @@ fn round_up_to(base: uint, align: uint) -> uint { // in it. unsafe fn destroy_chunk(chunk: Chunk) { let mut idx = 0; - let buf = vec::unsafe::to_ptr_slice(chunk.data); + let buf = vec::unsafe::to_ptr(chunk.data); let fill = chunk.fill; while idx < fill { @@ -156,7 +156,7 @@ impl &Arena { // start, n_bytes, align, head.fill); unsafe { - ptr::offset(vec::unsafe::to_ptr_slice(head.data), start) + ptr::offset(vec::unsafe::to_ptr(head.data), start) } } @@ -200,7 +200,7 @@ impl &Arena { // start, n_bytes, align, head.fill); unsafe { - let buf = vec::unsafe::to_ptr_slice(head.data); + let buf = vec::unsafe::to_ptr(head.data); return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start)); } } diff --git a/src/libstd/deque.rs b/src/libstd/deque.rs index 1eac198c773..920b969eea9 100644 --- a/src/libstd/deque.rs +++ b/src/libstd/deque.rs @@ -57,7 +57,7 @@ fn create() -> Deque { self.lo = self.elts.len() - 1u; } else { self.lo -= 1u; } if self.lo == self.hi { - self.elts.swap(|v| grow(self.nelts, oldlo, move v)); + self.elts.swap_mut(|v| grow(self.nelts, oldlo, move v)); self.lo = self.elts.len() - 1u; self.hi = self.nelts; } @@ -66,7 +66,7 @@ fn create() -> Deque { } fn add_back(t: T) { if self.lo == self.hi && self.nelts != 0u { - self.elts.swap(|v| grow(self.nelts, self.lo, move v)); + self.elts.swap_mut(|v| grow(self.nelts, self.lo, move v)); self.lo = 0u; self.hi = self.nelts; } @@ -108,8 +108,7 @@ fn create() -> Deque { mut hi: 0u, elts: dvec::from_vec( - vec::to_mut( - vec::from_elem(initial_capacity, None))) + vec::from_elem(initial_capacity, None)) }; (move repr) as Deque:: } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 79a99fb4220..182c1d61bc0 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -646,12 +646,12 @@ pure fn lt(value0: Json, value1: Json) -> bool { let (d0_flat, d1_flat) = { let d0_flat = dvec::DVec(); for d0.each |k, v| { d0_flat.push((k, v)); } - let d0_flat = dvec::unwrap(move d0_flat); + let mut d0_flat = dvec::unwrap(move d0_flat); d0_flat.qsort(); let mut d1_flat = dvec::DVec(); for d1.each |k, v| { d1_flat.push((k, v)); } - let d1_flat = dvec::unwrap(move d1_flat); + let mut d1_flat = dvec::unwrap(move d1_flat); d1_flat.qsort(); (move d0_flat, move d1_flat) diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 5652b81b55e..0e422aa1eeb 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -736,8 +736,8 @@ impl Url: Eq { } impl Url: IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { - self.to_str().iter_bytes(lsb0, f) + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + unchecked { self.to_str() }.iter_bytes(lsb0, f) } } @@ -1077,11 +1077,11 @@ mod tests { assert encode_form_urlencoded(m) == ~""; let m = str_hash(); - m.insert(~"foo", @dvec::from_vec(~[mut @~"bar", @~"123"])); + m.insert(~"foo", @dvec::from_vec(~[@~"bar", @~"123"])); assert encode_form_urlencoded(m) == ~"foo=bar&foo=123"; let m = str_hash(); - m.insert(~"foo bar", @dvec::from_vec(~[mut @~"abc", @~"12 = 34"])); + m.insert(~"foo bar", @dvec::from_vec(~[@~"abc", @~"12 = 34"])); assert encode_form_urlencoded(m) == ~"foo+bar=abc&foo+bar=12+%3D+34"; } diff --git a/src/libstd/set.rs b/src/libstd/set.rs new file mode 100644 index 00000000000..15c1c160ed1 --- /dev/null +++ b/src/libstd/set.rs @@ -0,0 +1,62 @@ +import dvec::dvec; +import map::{hashfn, eqfn, hashmap}; + +struct set { + mut implementation: option> +} + +struct list_set { + hasher: hashfn; + eqer: eqfn; + elements: ~[K]; +} + +enum set_implementation { + impl_with_list(list_set), + impl_with_map(hashmap) +} + +const threshold: uint = 25; // completely arbitrary. + +impl &list_set { + pure fn contains(element: &K) { + for self.elements.each |existing_element| { + if self.eqer(element, existing_element) { + return true; + } + } + return false; + } + + pure fn convert_to_map() -> hashmap { + ... + } +} + +impl set { + fn add(+element: K) -> bool { + let mut set_impl = option::swap_unwrap(&mut self.implementation); + let contained_before = match set_impl { + impl_with_list(ref mut list_set) => { + if list_set.elements.len() >= threshold { + // convert to a map + self.implementation = some(list_set.convert_to_map()); + return self.add(move element); + } + + if list_set.contains(&element) { + false + } else { + vec::push(list_set.elements, element); + true + } + } + + impl_with_map(ref map) => { + let contained_before = map.insert(element, ()); + } + } + self.implementation = some(move set_impl); + return true; + } +} \ No newline at end of file diff --git a/src/libstd/sha1.rs b/src/libstd/sha1.rs index f7cdfd60f50..9808b15dc66 100644 --- a/src/libstd/sha1.rs +++ b/src/libstd/sha1.rs @@ -161,7 +161,7 @@ fn sha1() -> Sha1 { fn mk_result(st: &Sha1State) -> ~[u8] { if !(*st).computed { pad_msg(st); (*st).computed = true; } let mut rs: ~[u8] = ~[]; - for vec::each_mut((*st).h) |ptr_hpart| { + for vec::each_mut_ref((*st).h) |ptr_hpart| { let hpart = *ptr_hpart; let a = (hpart >> 24u32 & 0xFFu32) as u8; let b = (hpart >> 16u32 & 0xFFu32) as u8; diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 339278d3c66..1cb4adac45c 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -92,7 +92,7 @@ impl SmallIntMap: map::Map { old.is_some() } fn clear() { - self.v.set(~[mut]); + self.v.set(~[]); } fn contains_key(+key: uint) -> bool { contains_key(self, key) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index af7d8d83b6b..38eccd3593f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -311,7 +311,7 @@ enum binding_mode { } impl binding_mode : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { bind_by_value => 0u8.iter_bytes(lsb0, f), @@ -385,7 +385,7 @@ enum pat_ { enum mutability { m_mutbl, m_imm, m_const, } impl mutability : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -524,7 +524,7 @@ enum inferable { } impl inferable : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { expl(ref t) => to_bytes::iter_bytes_2(&0u8, t, lsb0, f), @@ -560,7 +560,7 @@ impl inferable : cmp::Eq { enum rmode { by_ref, by_val, by_mutbl_ref, by_move, by_copy } impl rmode : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -922,7 +922,7 @@ enum trait_method { enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } impl int_ty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -951,7 +951,7 @@ impl int_ty: cmp::Eq { enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } impl uint_ty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -978,7 +978,7 @@ impl uint_ty: cmp::Eq { enum float_ty { ty_f, ty_f32, ty_f64, } impl float_ty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -1082,7 +1082,7 @@ impl ty : cmp::Eq { } impl ty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f); } } @@ -1106,7 +1106,7 @@ enum purity { } impl purity : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -1126,7 +1126,7 @@ enum ret_style { } impl ret_style : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -1423,7 +1423,7 @@ enum item_ { enum class_mutability { class_mutable, class_immutable } impl class_mutability : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index b9fed4b46aa..6140014d848 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -254,7 +254,7 @@ pure fn is_call_expr(e: @expr) -> bool { // This makes def_id hashable impl def_id : core::to_bytes::IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: core::to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: core::to_bytes::Cb) { core::to_bytes::iter_bytes_2(&self.crate, &self.node, lsb0, f); } } diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index 638e46a9cdb..d97124d5f44 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -127,7 +127,7 @@ fn gather_anti_quotes(lo: uint, node: N) -> aq_ctxt pure fn by_lo(a: &gather_item, b: &gather_item) -> bool { a.lo < b.lo } - vec::to_mut(std::sort::merge_sort(by_lo, v)) + std::sort::merge_sort(by_lo, v) }; return cx; } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 724c824d5cd..5b0fd67ae50 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -35,7 +35,7 @@ impl ObsoleteSyntax : cmp::Eq { impl ObsoleteSyntax: to_bytes::IterBytes { #[inline(always)] - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as uint).iter_bytes(lsb0, f); } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b8d55fb1055..1089431d810 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3556,8 +3556,8 @@ impl parser { } {attrs_remaining: attrs, - view_items: vec::from_mut(dvec::unwrap(move view_items)), - items: vec::from_mut(dvec::unwrap(move items))} + view_items: dvec::unwrap(move view_items), + items: dvec::unwrap(move items)} } // Parses a source module as a crate diff --git a/src/rustc/middle/astencode.rs b/src/rustc/middle/astencode.rs index 3172704c971..e7b9cdb5d35 100644 --- a/src/rustc/middle/astencode.rs +++ b/src/rustc/middle/astencode.rs @@ -951,7 +951,7 @@ fn decode_side_tables(xcx: extended_decode_ctxt, let ids = val_dsr.read_to_vec(|| { xcx.tr_id(val_dsr.read_int()) }); - let dvec = @dvec::from_vec(vec::to_mut(ids)); + let dvec = @dvec::from_vec(move ids); dcx.maps.last_use_map.insert(id, dvec); } else if tag == (c::tag_table_method_map as uint) { dcx.maps.method_map.insert( diff --git a/src/rustc/middle/borrowck.rs b/src/rustc/middle/borrowck.rs index ea5bc48445e..f43c4dbba67 100644 --- a/src/rustc/middle/borrowck.rs +++ b/src/rustc/middle/borrowck.rs @@ -415,7 +415,7 @@ impl root_map_key : cmp::Eq { } impl root_map_key : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.id, &self.derefs, lsb0, f); } } diff --git a/src/rustc/middle/borrowck/gather_loans.rs b/src/rustc/middle/borrowck/gather_loans.rs index 449913e92b7..f5552515142 100644 --- a/src/rustc/middle/borrowck/gather_loans.rs +++ b/src/rustc/middle/borrowck/gather_loans.rs @@ -408,7 +408,7 @@ impl gather_loan_ctxt { } None => { self.req_maps.req_loan_map.insert( - scope_id, @dvec::from_vec(~[mut loans])); + scope_id, @dvec::from_vec(~[loans])); } } } diff --git a/src/rustc/middle/trans/alt.rs b/src/rustc/middle/trans/alt.rs index 725848592b1..98da65324e8 100644 --- a/src/rustc/middle/trans/alt.rs +++ b/src/rustc/middle/trans/alt.rs @@ -285,7 +285,7 @@ fn get_options(ccx: @crate_ctxt, m: match_, col: uint) -> ~[opt] { } } } - return vec::from_mut(dvec::unwrap(move found)); + return dvec::unwrap(move found); } fn extract_variant_args(bcx: block, pat_id: ast::node_id, diff --git a/src/rustc/middle/trans/build.rs b/src/rustc/middle/trans/build.rs index 23b1d964ba3..ab108f5ef55 100644 --- a/src/rustc/middle/trans/build.rs +++ b/src/rustc/middle/trans/build.rs @@ -446,7 +446,7 @@ fn InBoundsGEP(cx: block, Pointer: ValueRef, Indices: &[ValueRef]) -> unsafe { count_insn(cx, "inboundsgep"); return llvm::LLVMBuildInBoundsGEP(B(cx), Pointer, - vec::unsafe::to_ptr_slice(Indices), + vec::unsafe::to_ptr(Indices), Indices.len() as c_uint, noname()); } diff --git a/src/rustc/middle/trans/common.rs b/src/rustc/middle/trans/common.rs index 03ccea0ad50..9d0579fa88f 100644 --- a/src/rustc/middle/trans/common.rs +++ b/src/rustc/middle/trans/common.rs @@ -1133,7 +1133,7 @@ impl mono_id_: cmp::Eq { } impl mono_param_id : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { mono_precise(t, mids) => to_bytes::iter_bytes_3(&0u8, &ty::type_id(t), &mids, lsb0, f), @@ -1147,7 +1147,7 @@ impl mono_param_id : to_bytes::IterBytes { } impl mono_id_ : core::to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.def, &self.params, lsb0, f); } } diff --git a/src/rustc/middle/trans/shape.rs b/src/rustc/middle/trans/shape.rs index f1b6a042053..b40afb6dd44 100644 --- a/src/rustc/middle/trans/shape.rs +++ b/src/rustc/middle/trans/shape.rs @@ -43,7 +43,7 @@ impl nominal_id_ : core::cmp::Eq { } impl nominal_id_ : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.did, &self.parent_id, lsb0, f); for self.tps.each |t| { ty::type_id(t).iter_bytes(lsb0, f); diff --git a/src/rustc/middle/trans/type_use.rs b/src/rustc/middle/trans/type_use.rs index 7e101e4796e..0975c8f4d8a 100644 --- a/src/rustc/middle/trans/type_use.rs +++ b/src/rustc/middle/trans/type_use.rs @@ -118,7 +118,7 @@ fn type_uses_for(ccx: @crate_ctxt, fn_id: def_id, n_tps: uint) fn type_needs(cx: ctx, use_: uint, ty: ty::t) { // Optimization -- don't descend type if all params already have this use - for vec::each_mut(cx.uses) |u| { + for vec::each_mut_ref(cx.uses) |u| { if *u & use_ != use_ { type_needs_inner(cx, use_, ty, @Nil); return; diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index 631efe4a766..a1307d70972 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -247,7 +247,7 @@ impl creader_cache_key : cmp::Eq { } impl creader_cache_key : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_3(&self.cnum, &self.pos, &self.len, lsb0, f); } } @@ -262,7 +262,7 @@ impl intern_key: cmp::Eq { } impl intern_key : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.sty, &self.o_def_id, lsb0, f); } } @@ -404,7 +404,7 @@ enum closure_kind { } impl closure_kind : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (self as u8).iter_bytes(lsb0, f) } } @@ -422,7 +422,7 @@ enum fn_proto { } impl fn_proto : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { proto_bare => 0u8.iter_bytes(lsb0, f), @@ -500,7 +500,7 @@ impl param_ty: cmp::Eq { } impl param_ty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.idx, &self.def_id, lsb0, f) } } @@ -674,7 +674,7 @@ enum InferTy { } impl InferTy : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { TyVar(ref tv) => to_bytes::iter_bytes_2(&0u8, tv, lsb0, f), IntVar(ref iv) => to_bytes::iter_bytes_2(&1u8, iv, lsb0, f) @@ -683,7 +683,7 @@ impl InferTy : to_bytes::IterBytes { } impl param_bound : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { bound_copy => 0u8.iter_bytes(lsb0, f), bound_owned => 1u8.iter_bytes(lsb0, f), @@ -747,25 +747,25 @@ impl purity: purity_to_str { } impl RegionVid : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (*self).iter_bytes(lsb0, f) } } impl TyVid : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (*self).iter_bytes(lsb0, f) } } impl IntVid : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (*self).iter_bytes(lsb0, f) } } impl FnVid : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { (*self).iter_bytes(lsb0, f) } } @@ -2432,7 +2432,7 @@ fn index_sty(cx: ctxt, sty: &sty) -> Option { } impl bound_region : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { ty::br_self => 0u8.iter_bytes(lsb0, f), @@ -2449,7 +2449,7 @@ impl bound_region : to_bytes::IterBytes { } impl region : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { re_bound(ref br) => to_bytes::iter_bytes_2(&0u8, br, lsb0, f), @@ -2469,7 +2469,7 @@ impl region : to_bytes::IterBytes { } impl vstore : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { vstore_fixed(ref u) => to_bytes::iter_bytes_2(&0u8, u, lsb0, f), @@ -2484,7 +2484,7 @@ impl vstore : to_bytes::IterBytes { } impl substs : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_3(&self.self_r, &self.self_ty, &self.tps, lsb0, f) @@ -2492,28 +2492,28 @@ impl substs : to_bytes::IterBytes { } impl mt : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.ty, &self.mutbl, lsb0, f) } } impl field : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.ident, &self.mt, lsb0, f) } } impl arg : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.mode, &self.ty, lsb0, f) } } impl sty : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { ty_nil => 0u8.iter_bytes(lsb0, f), ty_bool => 1u8.iter_bytes(lsb0, f), diff --git a/src/rustc/middle/typeck/infer/region_var_bindings.rs b/src/rustc/middle/typeck/infer/region_var_bindings.rs index 282be03c13d..eba683750fc 100644 --- a/src/rustc/middle/typeck/infer/region_var_bindings.rs +++ b/src/rustc/middle/typeck/infer/region_var_bindings.rs @@ -350,7 +350,7 @@ impl Constraint: cmp::Eq { } impl Constraint : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { match self { ConstrainVarSubVar(ref v0, ref v1) => to_bytes::iter_bytes_3(&0u8, v0, v1, lsb0, f), @@ -377,7 +377,7 @@ impl TwoRegions: cmp::Eq { } impl TwoRegions : to_bytes::IterBytes { - fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.a, &self.b, lsb0, f) } } diff --git a/src/test/bench/core-vec-append.rs b/src/test/bench/core-vec-append.rs index 2602389935c..087a37ecae2 100644 --- a/src/test/bench/core-vec-append.rs +++ b/src/test/bench/core-vec-append.rs @@ -12,7 +12,7 @@ fn collect_raw(num: uint) -> ~[uint] { return result; } -fn collect_dvec(num: uint) -> ~[mut uint] { +fn collect_dvec(num: uint) -> ~[uint] { let result = DVec(); for uint::range(0u, num) |i| { result.push(i); @@ -43,7 +43,7 @@ fn main(args: ~[~str]) { let raw = mid - start; let dvec = end - mid; - + let maxf = max as float; let rawf = raw as float; let dvecf = dvec as float; diff --git a/src/test/compile-fail/issue-2590.rs b/src/test/compile-fail/issue-2590.rs index 635fa059999..f4ccd901fb3 100644 --- a/src/test/compile-fail/issue-2590.rs +++ b/src/test/compile-fail/issue-2590.rs @@ -5,11 +5,11 @@ type parser = { }; trait parse { - fn parse() -> ~[mut int]; + fn parse() -> ~[int]; } impl parser: parse { - fn parse() -> ~[mut int] { + fn parse() -> ~[int] { dvec::unwrap(move self.tokens) //~ ERROR illegal move from self } } diff --git a/src/test/run-pass/dvec-test.rs b/src/test/run-pass/dvec-test.rs index 2ab13607add..6d9c525481b 100644 --- a/src/test/run-pass/dvec-test.rs +++ b/src/test/run-pass/dvec-test.rs @@ -5,7 +5,7 @@ fn main() { d.push(3); d.push(4); assert d.get() == ~[3, 4]; - d.set(~[mut 5]); + d.set(~[5]); d.push(6); d.push(7); d.push(8); @@ -23,6 +23,6 @@ fn main() { assert e == exp[i]; } - let v = vec::from_mut(dvec::unwrap(move d)); + let v = dvec::unwrap(move d); assert v == exp; } diff --git a/src/test/run-pass/issue-2631-b.rs b/src/test/run-pass/issue-2631-b.rs index a9bbca45070..d6a0c1d34c6 100644 --- a/src/test/run-pass/issue-2631-b.rs +++ b/src/test/run-pass/issue-2631-b.rs @@ -9,7 +9,7 @@ use std::map::*; use std::map::str_hash; fn main() { - let v = ~[mut @~"hi"]; + let v = ~[@~"hi"]; let m: req::header_map = str_hash(); m.insert(~"METHOD", @dvec::from_vec(v)); request::(m);