1
1
//! Managed vectors
2
2
3
- // NB: transitionary, de-mode-ing.
4
- #[ forbid( deprecated_mode) ] ;
5
- #[ forbid( deprecated_pattern) ] ;
6
-
7
3
use ptr:: addr_of;
8
4
9
5
/// Code for dealing with @-vectors. This is pretty incomplete, and
@@ -25,7 +21,7 @@ extern mod rusti {
25
21
26
22
/// Returns the number of elements the vector can hold without reallocating
27
23
#[ inline( always) ]
28
- pub pure fn capacity < T > ( v : @[ const T ] ) -> uint {
24
+ pub pure fn capacity < T > ( & & v: @[ const T ] ) -> uint {
29
25
unsafe {
30
26
let repr: * * raw :: VecRepr =
31
27
:: cast:: reinterpret_cast ( & addr_of ( v) ) ;
@@ -47,7 +43,7 @@ pub pure fn capacity<T>(v: @[const T]) -> uint {
47
43
*/
48
44
#[ inline( always) ]
49
45
pub pure fn build_sized < A > ( size : uint ,
50
- builder : & fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
46
+ builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
51
47
let mut vec = @[ ] ;
52
48
unsafe { raw:: reserve ( vec, size) ; }
53
49
builder ( |+x| unsafe { raw:: push ( vec, move x) } ) ;
@@ -65,7 +61,7 @@ pub pure fn build_sized<A>(size: uint,
65
61
* onto the vector being constructed.
66
62
*/
67
63
#[ inline( always) ]
68
- pub pure fn build < A > ( builder : & fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
64
+ pub pure fn build < A > ( builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
69
65
build_sized ( 4 , builder)
70
66
}
71
67
@@ -82,8 +78,8 @@ pub pure fn build<A>(builder: &fn(push: pure fn(+v: A))) -> @[A] {
82
78
* onto the vector being constructed.
83
79
*/
84
80
#[ inline( always) ]
85
- pub pure fn build_sized_opt < A > ( + size : Option < uint > ,
86
- builder : & fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
81
+ pub pure fn build_sized_opt < A > ( size : Option < uint > ,
82
+ builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
87
83
build_sized ( size. get_default ( 4 ) , builder)
88
84
}
89
85
@@ -98,10 +94,10 @@ pub pure fn append<T: Copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
98
94
99
95
100
96
/// Apply a function to each element of a vector and return the results
101
- pub pure fn map < T , U > ( v : & [ T ] , f : & fn ( x : & T ) -> U ) -> @[ U ] {
97
+ pub pure fn map < T , U > ( v : & [ T ] , f : fn ( T ) -> U ) -> @[ U ] {
102
98
do build_sized ( v. len ( ) ) |push| {
103
99
for vec:: each( v) |elem| {
104
- push( f( elem) ) ;
100
+ push ( f ( * elem) ) ;
105
101
}
106
102
}
107
103
}
@@ -125,10 +121,10 @@ pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] {
125
121
* Creates an immutable vector of size `n_elts` and initializes the elements
126
122
* to the value `t`.
127
123
*/
128
- pub pure fn from_elem < T : Copy > ( n_elts : uint , t : & T ) -> @[ T ] {
124
+ pub pure fn from_elem < T : Copy > ( n_elts : uint , t : T ) -> @[ T ] {
129
125
do build_sized ( n_elts) |push| {
130
126
let mut i: uint = 0 u;
131
- while i < n_elts { push ( copy * t) ; i += 1 u; }
127
+ while i < n_elts { push ( t) ; i += 1 u; }
132
128
}
133
129
}
134
130
@@ -159,13 +155,13 @@ pub mod raw {
159
155
* the vector is actually the specified size.
160
156
*/
161
157
#[ inline( always) ]
162
- pub unsafe fn set_len < T > ( v : @[ const T ] , new_len : uint ) {
158
+ pub unsafe fn set_len < T > ( & & v: @[ const T ] , new_len : uint ) {
163
159
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
164
160
( * * repr) . unboxed . fill = new_len * sys:: size_of :: < T > ( ) ;
165
161
}
166
162
167
163
#[ inline( always) ]
168
- pub unsafe fn push < T > ( v : @[ const T ] , +initval : T ) {
164
+ pub unsafe fn push < T > ( & v: @[ const T ] , +initval : T ) {
169
165
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
170
166
let fill = ( * * repr) . unboxed . fill ;
171
167
if ( * * repr) . unboxed . alloc > fill {
@@ -177,7 +173,7 @@ pub mod raw {
177
173
}
178
174
// This doesn't bother to make sure we have space.
179
175
#[ inline( always) ] // really pretty please
180
- pub unsafe fn push_fast < T > ( v : @[ const T ] , +initval : T ) {
176
+ pub unsafe fn push_fast < T > ( & v: @[ const T ] , +initval : T ) {
181
177
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
182
178
let fill = ( * * repr) . unboxed . fill ;
183
179
( * * repr) . unboxed . fill += sys:: size_of :: < T > ( ) ;
@@ -186,7 +182,7 @@ pub mod raw {
186
182
rusti:: move_val_init ( * p, move initval) ;
187
183
}
188
184
189
- pub unsafe fn push_slow < T > ( v : @[ const T ] , +initval : T ) {
185
+ pub unsafe fn push_slow < T > ( & v: @[ const T ] , +initval : T ) {
190
186
reserve_at_least ( v, v. len ( ) + 1 u) ;
191
187
push_fast ( v, move initval) ;
192
188
}
@@ -202,7 +198,7 @@ pub mod raw {
202
198
* * v - A vector
203
199
* * n - The number of elements to reserve space for
204
200
*/
205
- pub unsafe fn reserve < T > ( v : @[ const T ] , n : uint ) {
201
+ pub unsafe fn reserve < T > ( & v: @[ const T ] , n : uint ) {
206
202
// Only make the (slow) call into the runtime if we have to
207
203
if capacity ( v) < n {
208
204
let ptr = addr_of ( v) as * * VecRepr ;
@@ -226,7 +222,7 @@ pub mod raw {
226
222
* * v - A vector
227
223
* * n - The number of elements to reserve space for
228
224
*/
229
- pub unsafe fn reserve_at_least < T > ( v : @[ const T ] , n : uint ) {
225
+ pub unsafe fn reserve_at_least < T > ( & v: @[ const T ] , n : uint ) {
230
226
reserve ( v, uint:: next_power_of_two ( n) ) ;
231
227
}
232
228
0 commit comments