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