4
4
#[ forbid( deprecated_mode) ] ;
5
5
#[ forbid( deprecated_pattern) ] ;
6
6
7
+ use cast:: transmute;
7
8
use ptr:: addr_of;
8
9
9
10
/// Code for dealing with @-vectors. This is pretty incomplete, and
@@ -48,10 +49,10 @@ pub pure fn capacity<T>(v: @[const T]) -> uint {
48
49
#[ inline( always) ]
49
50
pub pure fn build_sized < A > ( size : uint ,
50
51
builder : & fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
51
- let mut vec = @[ ] ;
52
- unsafe { raw:: reserve ( vec, size) ; }
53
- builder ( |+x| unsafe { raw:: push ( vec, move x) } ) ;
54
- return vec;
52
+ let mut vec: @ [ const A ] = @[ ] ;
53
+ unsafe { raw:: reserve ( & mut vec, size) ; }
54
+ builder ( |+x| unsafe { raw:: push ( & mut vec, move x) } ) ;
55
+ return unsafe { transmute ( vec) } ;
55
56
}
56
57
57
58
/**
@@ -125,10 +126,10 @@ pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] {
125
126
* Creates an immutable vector of size `n_elts` and initializes the elements
126
127
* to the value `t`.
127
128
*/
128
- pub pure fn from_elem < T : Copy > ( n_elts : uint , t : & T ) -> @[ T ] {
129
+ pub pure fn from_elem < T : Copy > ( n_elts : uint , + t : T ) -> @[ T ] {
129
130
do build_sized ( n_elts) |push| {
130
131
let mut i: uint = 0 u;
131
- while i < n_elts { push ( copy * t) ; i += 1 u; }
132
+ while i < n_elts { push ( copy t) ; i += 1 u; }
132
133
}
133
134
}
134
135
@@ -165,8 +166,8 @@ pub mod raw {
165
166
}
166
167
167
168
#[ inline( always) ]
168
- pub unsafe fn push < T > ( v : @[ const T ] , +initval : T ) {
169
- let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v ) ) ;
169
+ pub unsafe fn push < T > ( v : & mut @[ const T ] , +initval : T ) {
170
+ let repr: * * VecRepr = :: cast:: reinterpret_cast ( & v ) ;
170
171
let fill = ( * * repr) . unboxed . fill ;
171
172
if ( * * repr) . unboxed . alloc > fill {
172
173
push_fast ( v, move initval) ;
@@ -177,16 +178,16 @@ pub mod raw {
177
178
}
178
179
// This doesn't bother to make sure we have space.
179
180
#[ inline( always) ] // really pretty please
180
- pub unsafe fn push_fast < T > ( v : @[ const T ] , +initval : T ) {
181
- let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v ) ) ;
181
+ pub unsafe fn push_fast < T > ( v : & mut @[ const T ] , +initval : T ) {
182
+ let repr: * * VecRepr = :: cast:: reinterpret_cast ( & v ) ;
182
183
let fill = ( * * repr) . unboxed . fill ;
183
184
( * * repr) . unboxed . fill += sys:: size_of :: < T > ( ) ;
184
185
let p = ptr:: addr_of ( ( * * repr) . unboxed . data ) ;
185
186
let p = ptr:: offset ( p, fill) as * mut T ;
186
187
rusti:: move_val_init ( * p, move initval) ;
187
188
}
188
189
189
- pub unsafe fn push_slow < T > ( v : @[ const T ] , +initval : T ) {
190
+ pub unsafe fn push_slow < T > ( v : & mut @[ const T ] , +initval : T ) {
190
191
reserve_at_least ( v, v. len ( ) + 1 u) ;
191
192
push_fast ( v, move initval) ;
192
193
}
@@ -202,10 +203,10 @@ pub mod raw {
202
203
* * v - A vector
203
204
* * n - The number of elements to reserve space for
204
205
*/
205
- pub unsafe fn reserve < T > ( v : @[ const T ] , n : uint ) {
206
+ pub unsafe fn reserve < T > ( v : & mut @[ const T ] , n : uint ) {
206
207
// Only make the (slow) call into the runtime if we have to
207
- if capacity ( v) < n {
208
- let ptr = addr_of ( v ) as * * VecRepr ;
208
+ if capacity ( * v) < n {
209
+ let ptr: * * VecRepr = transmute ( copy v ) ;
209
210
rustrt:: vec_reserve_shared_actual ( sys:: get_type_desc :: < T > ( ) ,
210
211
ptr, n as libc:: size_t ) ;
211
212
}
@@ -226,7 +227,7 @@ pub mod raw {
226
227
* * v - A vector
227
228
* * n - The number of elements to reserve space for
228
229
*/
229
- pub unsafe fn reserve_at_least < T > ( v : @[ const T ] , n : uint ) {
230
+ pub unsafe fn reserve_at_least < T > ( v : & mut @[ const T ] , n : uint ) {
230
231
reserve ( v, uint:: next_power_of_two ( n) ) ;
231
232
}
232
233
0 commit comments