From 264e1b2edbba5fcb4dc0866fa168f434fe54876b Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 26 Sep 2012 12:28:30 -0700 Subject: [PATCH] libcore: De-mode at_vec --- src/libcore/at_vec.rs | 31 ++++++++++++++++--------------- src/libstd/arena.rs | 6 +++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/libcore/at_vec.rs b/src/libcore/at_vec.rs index d7fee2cd856c..535aa4e37ec8 100644 --- a/src/libcore/at_vec.rs +++ b/src/libcore/at_vec.rs @@ -4,6 +4,7 @@ #[forbid(deprecated_mode)]; #[forbid(deprecated_pattern)]; +use cast::transmute; use ptr::addr_of; /// Code for dealing with @-vectors. This is pretty incomplete, and @@ -48,10 +49,10 @@ pub pure fn capacity(v: @[const T]) -> uint { #[inline(always)] pub pure fn build_sized(size: uint, builder: &fn(push: pure fn(+v: A))) -> @[A] { - let mut vec = @[]; - unsafe { raw::reserve(vec, size); } - builder(|+x| unsafe { raw::push(vec, move x) }); - return vec; + let mut vec: @[const A] = @[]; + unsafe { raw::reserve(&mut vec, size); } + builder(|+x| unsafe { raw::push(&mut vec, move x) }); + return unsafe { transmute(vec) }; } /** @@ -125,10 +126,10 @@ pub pure fn from_fn(n_elts: uint, op: iter::InitOp) -> @[T] { * Creates an immutable vector of size `n_elts` and initializes the elements * to the value `t`. */ -pub pure fn from_elem(n_elts: uint, t: &T) -> @[T] { +pub pure fn from_elem(n_elts: uint, +t: T) -> @[T] { do build_sized(n_elts) |push| { let mut i: uint = 0u; - while i < n_elts { push(copy *t); i += 1u; } + while i < n_elts { push(copy t); i += 1u; } } } @@ -165,8 +166,8 @@ pub mod raw { } #[inline(always)] - pub unsafe fn push(v: @[const T], +initval: T) { - let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v)); + pub unsafe fn push(v: &mut @[const T], +initval: T) { + let repr: **VecRepr = ::cast::reinterpret_cast(&v); let fill = (**repr).unboxed.fill; if (**repr).unboxed.alloc > fill { push_fast(v, move initval); @@ -177,8 +178,8 @@ pub mod raw { } // This doesn't bother to make sure we have space. #[inline(always)] // really pretty please - pub unsafe fn push_fast(v: @[const T], +initval: T) { - let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v)); + pub unsafe fn push_fast(v: &mut @[const T], +initval: T) { + let repr: **VecRepr = ::cast::reinterpret_cast(&v); let fill = (**repr).unboxed.fill; (**repr).unboxed.fill += sys::size_of::(); let p = ptr::addr_of((**repr).unboxed.data); @@ -186,7 +187,7 @@ pub mod raw { rusti::move_val_init(*p, move initval); } - pub unsafe fn push_slow(v: @[const T], +initval: T) { + pub unsafe fn push_slow(v: &mut @[const T], +initval: T) { reserve_at_least(v, v.len() + 1u); push_fast(v, move initval); } @@ -202,10 +203,10 @@ pub mod raw { * * v - A vector * * n - The number of elements to reserve space for */ - pub unsafe fn reserve(v: @[const T], n: uint) { + pub unsafe fn reserve(v: &mut @[const T], n: uint) { // Only make the (slow) call into the runtime if we have to - if capacity(v) < n { - let ptr = addr_of(v) as **VecRepr; + if capacity(*v) < n { + let ptr: **VecRepr = transmute(copy v); rustrt::vec_reserve_shared_actual(sys::get_type_desc::(), ptr, n as libc::size_t); } @@ -226,7 +227,7 @@ pub mod raw { * * v - A vector * * n - The number of elements to reserve space for */ - pub unsafe fn reserve_at_least(v: @[const T], n: uint) { + pub unsafe fn reserve_at_least(v: &mut @[const T], n: uint) { reserve(v, uint::next_power_of_two(n)); } diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index 4af647aabf70..f9c023597c4a 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -70,9 +70,9 @@ struct Arena { } fn chunk(size: uint, is_pod: bool) -> Chunk { - let mut v = @[]; - unsafe { at_vec::raw::reserve(v, size); } - { data: v, mut fill: 0u, is_pod: is_pod } + let mut v: @[const u8] = @[]; + unsafe { at_vec::raw::reserve(&mut v, size); } + { data: unsafe { cast::transmute(v) }, mut fill: 0u, is_pod: is_pod } } fn arena_with_size(initial_size: uint) -> Arena {