Skip to content

Commit c11ee0f

Browse files
author
blake2-ppc
committed
std::at_vec and vec: Unify build_sized, build_sized_opt into build
These functions have very few users since they are mostly replaced by iterator-based constructions. Convert a few remaining users in-tree, and reduce the number of functions by basically renaming build_sized_opt to build, and removing the other two. This for both the vec and the at_vec versions.
1 parent 5f69a58 commit c11ee0f

File tree

8 files changed

+27
-97
lines changed

8 files changed

+27
-97
lines changed

src/libextra/base64.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,8 @@ mod test {
315315
use std::vec;
316316
317317
do 1000.times {
318-
let v: ~[u8] = do vec::build |push| {
319-
do task_rng().gen_uint_range(1, 100).times {
320-
push(random());
321-
}
322-
};
318+
let times = task_rng().gen_uint_range(1, 100);
319+
let v = vec::from_fn(times, |_| random::<u8>());
323320
assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v);
324321
}
325322
}

src/librustc/middle/typeck/infer/combine.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use middle::typeck::infer::{TypeTrace};
6161
use util::common::indent;
6262

6363
use std::result;
64-
use std::vec;
6564
use syntax::ast::{Onceness, purity};
6665
use syntax::ast;
6766
use syntax::opt_vec;

src/librustc/middle/typeck/infer/region_inference/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,12 @@ impl RegionVarBindings {
373373

374374
pub fn vars_created_since_snapshot(&mut self, snapshot: uint)
375375
-> ~[RegionVid] {
376-
do vec::build |push| {
377-
for &elt in self.undo_log.slice_from(snapshot).iter() {
378-
match elt {
379-
AddVar(vid) => push(vid),
380-
_ => ()
381-
}
382-
}
383-
}
376+
self.undo_log.slice_from(snapshot).iter()
377+
.filter_map(|&elt| match elt {
378+
AddVar(vid) => Some(vid),
379+
_ => None
380+
})
381+
.collect()
384382
}
385383

386384
pub fn tainted(&mut self, snapshot: uint, r0: Region) -> ~[Region] {

src/libstd/at_vec.rs

+11-43
Original file line numberDiff line numberDiff line change
@@ -33,62 +33,30 @@ pub fn capacity<T>(v: @[T]) -> uint {
3333
/**
3434
* Builds a vector by calling a provided function with an argument
3535
* function that pushes an element to the back of a vector.
36-
* This version takes an initial size for the vector.
36+
* The initial size for the vector may optionally be specified
3737
*
3838
* # Arguments
3939
*
40-
* * size - An initial size of the vector to reserve
40+
* * size - An option, maybe containing initial size of the vector to reserve
4141
* * builder - A function that will construct the vector. It receives
4242
* as an argument a function that will push an element
4343
* onto the vector being constructed.
4444
*/
4545
#[inline]
46-
pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> @[A] {
46+
pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] {
4747
let mut vec = @[];
48-
unsafe { raw::reserve(&mut vec, size); }
48+
unsafe { raw::reserve(&mut vec, size.unwrap_or_default(4)); }
4949
builder(|x| unsafe { raw::push(&mut vec, x) });
5050
vec
5151
}
5252

53-
/**
54-
* Builds a vector by calling a provided function with an argument
55-
* function that pushes an element to the back of a vector.
56-
*
57-
* # Arguments
58-
*
59-
* * builder - A function that will construct the vector. It receives
60-
* as an argument a function that will push an element
61-
* onto the vector being constructed.
62-
*/
63-
#[inline]
64-
pub fn build<A>(builder: &fn(push: &fn(v: A))) -> @[A] {
65-
build_sized(4, builder)
66-
}
67-
68-
/**
69-
* Builds a vector by calling a provided function with an argument
70-
* function that pushes an element to the back of a vector.
71-
* This version takes an initial size for the vector.
72-
*
73-
* # Arguments
74-
*
75-
* * size - An option, maybe containing initial size of the vector to reserve
76-
* * builder - A function that will construct the vector. It receives
77-
* as an argument a function that will push an element
78-
* onto the vector being constructed.
79-
*/
80-
#[inline]
81-
pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] {
82-
build_sized(size.unwrap_or_default(4), builder)
83-
}
84-
8553
// Appending
8654

8755
/// Iterates over the `rhs` vector, copying each element and appending it to the
8856
/// `lhs`. Afterwards, the `lhs` is then returned for use again.
8957
#[inline]
9058
pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
91-
do build_sized(lhs.len() + rhs.len()) |push| {
59+
do build(Some(lhs.len() + rhs.len())) |push| {
9260
for x in lhs.iter() {
9361
push((*x).clone());
9462
}
@@ -101,7 +69,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
10169

10270
/// Apply a function to each element of a vector and return the results
10371
pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
104-
do build_sized(v.len()) |push| {
72+
do build(Some(v.len())) |push| {
10573
for elem in v.iter() {
10674
push(f(elem));
10775
}
@@ -115,7 +83,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
11583
* to the value returned by the function `op`.
11684
*/
11785
pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] {
118-
do build_sized(n_elts) |push| {
86+
do build(Some(n_elts)) |push| {
11987
let mut i: uint = 0u;
12088
while i < n_elts { push(op(i)); i += 1u; }
12189
}
@@ -128,7 +96,7 @@ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] {
12896
* to the value `t`.
12997
*/
13098
pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
131-
do build_sized(n_elts) |push| {
99+
do build(Some(n_elts)) |push| {
132100
let mut i: uint = 0u;
133101
while i < n_elts {
134102
push(t.clone());
@@ -312,7 +280,7 @@ mod test {
312280
fn test() {
313281
// Some code that could use that, then:
314282
fn seq_range(lo: uint, hi: uint) -> @[uint] {
315-
do build |push| {
283+
do build(None) |push| {
316284
for i in range(lo, hi) {
317285
push(i);
318286
}
@@ -359,15 +327,15 @@ mod test {
359327
fn bench_build_sized(b: &mut bh) {
360328
let len = 64;
361329
do b.iter {
362-
build_sized(len, |push| for i in range(0, 1024) { push(i) });
330+
build(Some(len), |push| for i in range(0, 1024) { push(i) });
363331
}
364332
}
365333

366334
#[bench]
367335
fn bench_build(b: &mut bh) {
368336
do b.iter {
369337
for i in range(0, 95) {
370-
build(|push| push(i));
338+
build(None, |push| push(i));
371339
}
372340
}
373341
}

src/libstd/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ impl<T:Reader> ReaderUtil for T {
777777
}
778778

779779
fn read_lines(&self) -> ~[~str] {
780-
do vec::build |push| {
780+
do vec::build(None) |push| {
781781
do self.each_line |line| {
782782
push(line.to_owned());
783783
true

src/libstd/vec.rs

+5-37
Original file line numberDiff line numberDiff line change
@@ -194,54 +194,22 @@ pub fn with_capacity<T>(capacity: uint) -> ~[T] {
194194
/**
195195
* Builds a vector by calling a provided function with an argument
196196
* function that pushes an element to the back of a vector.
197-
* This version takes an initial capacity for the vector.
197+
* The initial capacity for the vector may optionally be specified.
198198
*
199199
* # Arguments
200200
*
201-
* * size - An initial size of the vector to reserve
201+
* * size - An option, maybe containing initial size of the vector to reserve
202202
* * builder - A function that will construct the vector. It receives
203203
* as an argument a function that will push an element
204204
* onto the vector being constructed.
205205
*/
206206
#[inline]
207-
pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] {
208-
let mut vec = with_capacity(size);
207+
pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] {
208+
let mut vec = with_capacity(size.unwrap_or_default(4));
209209
builder(|x| vec.push(x));
210210
vec
211211
}
212212

213-
/**
214-
* Builds a vector by calling a provided function with an argument
215-
* function that pushes an element to the back of a vector.
216-
*
217-
* # Arguments
218-
*
219-
* * builder - A function that will construct the vector. It receives
220-
* as an argument a function that will push an element
221-
* onto the vector being constructed.
222-
*/
223-
#[inline]
224-
pub fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A] {
225-
build_sized(4, builder)
226-
}
227-
228-
/**
229-
* Builds a vector by calling a provided function with an argument
230-
* function that pushes an element to the back of a vector.
231-
* This version takes an initial size for the vector.
232-
*
233-
* # Arguments
234-
*
235-
* * size - An option, maybe containing initial size of the vector to reserve
236-
* * builder - A function that will construct the vector. It receives
237-
* as an argument a function that will push an element
238-
* onto the vector being constructed.
239-
*/
240-
#[inline]
241-
pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] {
242-
build_sized(size.unwrap_or_default(4), builder)
243-
}
244-
245213
/// An iterator over the slices of a vector separated by elements that
246214
/// match a predicate function.
247215
pub struct SplitIterator<'self, T> {
@@ -3248,7 +3216,7 @@ mod tests {
32483216
#[test]
32493217
#[should_fail]
32503218
fn test_build_fail() {
3251-
do build |push| {
3219+
do build(None) |push| {
32523220
push((~0, @0));
32533221
push((~0, @0));
32543222
push((~0, @0));

src/libsyntax/ext/deriving/generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ fn create_struct_pattern(cx: @ExtCtxt,
958958
// struct_type is definitely not Unknown, since struct_def.fields
959959
// must be nonempty to reach here
960960
let pattern = if struct_type == Record {
961-
let field_pats = do vec::build |push| {
961+
let field_pats = do vec::build(None) |push| {
962962
for (&pat, &(id, _)) in subpats.iter().zip(ident_expr.iter()) {
963963
// id is guaranteed to be Some
964964
push(ast::FieldPat { ident: id.unwrap(), pat: pat })

src/test/run-pass/issue-3563-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Drop for AsciiArt {
6666
fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt {
6767
// Use an anonymous function to build a vector of vectors containing
6868
// blank characters for each position in our canvas.
69-
let lines = do vec::build_sized(height) |push| {
69+
let lines = do vec::build(Some(height)) |push| {
7070
do height.times {
7171
push(vec::from_elem(width, '.'));
7272
}

0 commit comments

Comments
 (0)