2
2
3
3
use ptr:: addr_of;
4
4
5
- export init_op;
6
- export capacity;
7
- export build_sized, build, build_sized_opt;
8
- export map;
9
- export from_fn, from_elem;
10
- export raw;
11
- export traits;
12
-
13
5
/// Code for dealing with @-vectors. This is pretty incomplete, and
14
6
/// contains a bunch of duplication from the code for ~-vectors.
15
7
@@ -29,7 +21,7 @@ extern mod rusti {
29
21
30
22
/// Returns the number of elements the vector can hold without reallocating
31
23
#[ inline( always) ]
32
- pure fn capacity < T > ( & & v: @[ const T ] ) -> uint {
24
+ pub pure fn capacity < T > ( & & v: @[ const T ] ) -> uint {
33
25
unsafe {
34
26
let repr: * * raw :: VecRepr =
35
27
:: cast:: reinterpret_cast ( & addr_of ( v) ) ;
@@ -50,8 +42,8 @@ pure fn capacity<T>(&&v: @[const T]) -> uint {
50
42
* onto the vector being constructed.
51
43
*/
52
44
#[ inline( always) ]
53
- pure fn build_sized < A > ( size : uint ,
54
- builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
45
+ pub pure fn build_sized < A > ( size : uint ,
46
+ builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
55
47
let mut vec = @[ ] ;
56
48
unsafe { raw:: reserve ( vec, size) ; }
57
49
builder ( |+x| unsafe { raw:: push ( vec, move x) } ) ;
@@ -69,7 +61,7 @@ pure fn build_sized<A>(size: uint,
69
61
* onto the vector being constructed.
70
62
*/
71
63
#[ inline( always) ]
72
- 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 ] {
73
65
build_sized ( 4 , builder)
74
66
}
75
67
@@ -86,14 +78,14 @@ pure fn build<A>(builder: fn(push: pure fn(+v: A))) -> @[A] {
86
78
* onto the vector being constructed.
87
79
*/
88
80
#[ inline( always) ]
89
- pure fn build_sized_opt < A > ( size : Option < uint > ,
81
+ pub pure fn build_sized_opt < A > ( size : Option < uint > ,
90
82
builder : fn ( push : pure fn( +v : A ) ) ) -> @[ A ] {
91
83
build_sized ( size. get_default ( 4 ) , builder)
92
84
}
93
85
94
86
// Appending
95
87
#[ inline( always) ]
96
- pure fn append < T : Copy > ( lhs : @[ T ] , rhs : & [ const T ] ) -> @[ T ] {
88
+ pub pure fn append < T : Copy > ( lhs : @[ T ] , rhs : & [ const T ] ) -> @[ T ] {
97
89
do build_sized ( lhs. len ( ) + rhs. len ( ) ) |push| {
98
90
for vec:: each( lhs) |x| { push ( * x) ; }
99
91
for uint:: range( 0 , rhs. len( ) ) |i| { push ( rhs[ i] ) ; }
@@ -102,7 +94,7 @@ pure fn append<T: Copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
102
94
103
95
104
96
/// Apply a function to each element of a vector and return the results
105
- pure fn map < T , U > ( v : & [ T ] , f : fn ( T ) -> U ) -> @[ U ] {
97
+ pub pure fn map < T , U > ( v : & [ T ] , f : fn ( T ) -> U ) -> @[ U ] {
106
98
do build_sized ( v. len ( ) ) |push| {
107
99
for vec:: each( v) |elem| {
108
100
push ( f ( * elem) ) ;
@@ -116,7 +108,7 @@ pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] {
116
108
* Creates an immutable vector of size `n_elts` and initializes the elements
117
109
* to the value returned by the function `op`.
118
110
*/
119
- pure fn from_fn < T > ( n_elts : uint , op : iter:: InitOp < T > ) -> @[ T ] {
111
+ pub pure fn from_fn < T > ( n_elts : uint , op : iter:: InitOp < T > ) -> @[ T ] {
120
112
do build_sized ( n_elts) |push| {
121
113
let mut i: uint = 0 u;
122
114
while i < n_elts { push ( op ( i) ) ; i += 1 u; }
@@ -129,7 +121,7 @@ pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] {
129
121
* Creates an immutable vector of size `n_elts` and initializes the elements
130
122
* to the value `t`.
131
123
*/
132
- 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 ] {
133
125
do build_sized ( n_elts) |push| {
134
126
let mut i: uint = 0 u;
135
127
while i < n_elts { push ( t) ; i += 1 u; }
@@ -148,13 +140,12 @@ mod traits {
148
140
}
149
141
150
142
#[ cfg( test) ]
151
- mod traits {
143
+ pub mod traits {
152
144
#[ legacy_exports] ; }
153
145
154
- mod raw {
155
- #[ legacy_exports] ;
156
- type VecRepr = vec:: raw:: VecRepr ;
157
- type SliceRepr = vec:: raw:: SliceRepr ;
146
+ pub mod raw {
147
+ pub type VecRepr = vec:: raw:: VecRepr ;
148
+ pub type SliceRepr = vec:: raw:: SliceRepr ;
158
149
159
150
/**
160
151
* Sets the length of a vector
@@ -164,13 +155,13 @@ mod raw {
164
155
* the vector is actually the specified size.
165
156
*/
166
157
#[ inline( always) ]
167
- unsafe fn set_len < T > ( & & v: @[ const T ] , new_len : uint ) {
158
+ pub unsafe fn set_len < T > ( & & v: @[ const T ] , new_len : uint ) {
168
159
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
169
160
( * * repr) . unboxed . fill = new_len * sys:: size_of :: < T > ( ) ;
170
161
}
171
162
172
163
#[ inline( always) ]
173
- unsafe fn push < T > ( & v: @[ const T ] , +initval : T ) {
164
+ pub unsafe fn push < T > ( & v: @[ const T ] , +initval : T ) {
174
165
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
175
166
let fill = ( * * repr) . unboxed . fill ;
176
167
if ( * * repr) . unboxed . alloc > fill {
@@ -182,7 +173,7 @@ mod raw {
182
173
}
183
174
// This doesn't bother to make sure we have space.
184
175
#[ inline( always) ] // really pretty please
185
- unsafe fn push_fast < T > ( & v: @[ const T ] , +initval : T ) {
176
+ pub unsafe fn push_fast < T > ( & v: @[ const T ] , +initval : T ) {
186
177
let repr: * * VecRepr = :: cast:: reinterpret_cast ( & addr_of ( v) ) ;
187
178
let fill = ( * * repr) . unboxed . fill ;
188
179
( * * repr) . unboxed . fill += sys:: size_of :: < T > ( ) ;
@@ -191,7 +182,7 @@ mod raw {
191
182
rusti:: move_val_init ( * p, move initval) ;
192
183
}
193
184
194
- unsafe fn push_slow < T > ( & v: @[ const T ] , +initval : T ) {
185
+ pub unsafe fn push_slow < T > ( & v: @[ const T ] , +initval : T ) {
195
186
reserve_at_least ( v, v. len ( ) + 1 u) ;
196
187
push_fast ( v, move initval) ;
197
188
}
@@ -207,7 +198,7 @@ mod raw {
207
198
* * v - A vector
208
199
* * n - The number of elements to reserve space for
209
200
*/
210
- unsafe fn reserve < T > ( & v: @[ const T ] , n : uint ) {
201
+ pub unsafe fn reserve < T > ( & v: @[ const T ] , n : uint ) {
211
202
// Only make the (slow) call into the runtime if we have to
212
203
if capacity ( v) < n {
213
204
let ptr = addr_of ( v) as * * VecRepr ;
@@ -231,14 +222,14 @@ mod raw {
231
222
* * v - A vector
232
223
* * n - The number of elements to reserve space for
233
224
*/
234
- unsafe fn reserve_at_least < T > ( & v: @[ const T ] , n : uint ) {
225
+ pub unsafe fn reserve_at_least < T > ( & v: @[ const T ] , n : uint ) {
235
226
reserve ( v, uint:: next_power_of_two ( n) ) ;
236
227
}
237
228
238
229
}
239
230
240
231
#[ test]
241
- fn test ( ) {
232
+ pub fn test ( ) {
242
233
// Some code that could use that, then:
243
234
fn seq_range ( lo : uint , hi : uint ) -> @[ uint ] {
244
235
do build |push| {
@@ -254,7 +245,7 @@ fn test() {
254
245
}
255
246
256
247
#[ test]
257
- fn append_test ( ) {
248
+ pub fn append_test ( ) {
258
249
assert @[ 1 , 2 , 3 ] + @[ 4 , 5 , 6 ] == @[ 1 , 2 , 3 , 4 , 5 , 6 ] ;
259
250
}
260
251
0 commit comments