diff --git a/src/libextra/base64.rs b/src/libextra/base64.rs index 525ff658dba..f50108d4eae 100644 --- a/src/libextra/base64.rs +++ b/src/libextra/base64.rs @@ -315,11 +315,8 @@ mod test { use std::vec; do 1000.times { - let v: ~[u8] = do vec::build |push| { - do task_rng().gen_uint_range(1, 100).times { - push(random()); - } - }; + let times = task_rng().gen_uint_range(1, 100); + let v = vec::from_fn(times, |_| random::<u8>()); assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v); } } diff --git a/src/librustc/middle/typeck/infer/combine.rs b/src/librustc/middle/typeck/infer/combine.rs index 693aee83061..9fc29250ed0 100644 --- a/src/librustc/middle/typeck/infer/combine.rs +++ b/src/librustc/middle/typeck/infer/combine.rs @@ -61,7 +61,6 @@ use middle::typeck::infer::{TypeTrace}; use util::common::indent; use std::result; -use std::vec; use syntax::ast::{Onceness, purity}; use syntax::ast; use syntax::opt_vec; diff --git a/src/librustc/middle/typeck/infer/region_inference/mod.rs b/src/librustc/middle/typeck/infer/region_inference/mod.rs index 91b6a4ce3bc..c14e49f37e5 100644 --- a/src/librustc/middle/typeck/infer/region_inference/mod.rs +++ b/src/librustc/middle/typeck/infer/region_inference/mod.rs @@ -373,14 +373,12 @@ impl RegionVarBindings { pub fn vars_created_since_snapshot(&mut self, snapshot: uint) -> ~[RegionVid] { - do vec::build |push| { - for &elt in self.undo_log.slice_from(snapshot).iter() { - match elt { - AddVar(vid) => push(vid), - _ => () - } - } - } + self.undo_log.slice_from(snapshot).iter() + .filter_map(|&elt| match elt { + AddVar(vid) => Some(vid), + _ => None + }) + .collect() } pub fn tainted(&mut self, snapshot: uint, r0: Region) -> ~[Region] { diff --git a/src/libstd/at_vec.rs b/src/libstd/at_vec.rs index c192803efff..27bef1214b2 100644 --- a/src/libstd/at_vec.rs +++ b/src/libstd/at_vec.rs @@ -33,42 +33,7 @@ pub fn capacity<T>(v: @[T]) -> uint { /** * Builds a vector by calling a provided function with an argument * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. - * - * # Arguments - * - * * size - An initial size of the vector to reserve - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> @[A] { - let mut vec = @[]; - unsafe { raw::reserve(&mut vec, size); } - builder(|x| unsafe { raw::push(&mut vec, x) }); - vec -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * - * # Arguments - * - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build<A>(builder: &fn(push: &fn(v: A))) -> @[A] { - build_sized(4, builder) -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. + * The initial size for the vector may optionally be specified * * # Arguments * @@ -78,8 +43,11 @@ pub fn build<A>(builder: &fn(push: &fn(v: A))) -> @[A] { * onto the vector being constructed. */ #[inline] -pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] { - build_sized(size.unwrap_or_default(4), builder) +pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> @[A] { + let mut vec = @[]; + unsafe { raw::reserve(&mut vec, size.unwrap_or_default(4)); } + builder(|x| unsafe { raw::push(&mut vec, x) }); + vec } // Appending @@ -88,7 +56,7 @@ pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> /// `lhs`. Afterwards, the `lhs` is then returned for use again. #[inline] pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] { - do build_sized(lhs.len() + rhs.len()) |push| { + do build(Some(lhs.len() + rhs.len())) |push| { for x in lhs.iter() { push((*x).clone()); } @@ -101,7 +69,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] { /// Apply a function to each element of a vector and return the results pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] { - do build_sized(v.len()) |push| { + do build(Some(v.len())) |push| { for elem in v.iter() { push(f(elem)); } @@ -115,7 +83,7 @@ pub fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] { * to the value returned by the function `op`. */ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] { - do build_sized(n_elts) |push| { + do build(Some(n_elts)) |push| { let mut i: uint = 0u; while i < n_elts { push(op(i)); i += 1u; } } @@ -128,7 +96,7 @@ pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> @[T] { * to the value `t`. */ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] { - do build_sized(n_elts) |push| { + do build(Some(n_elts)) |push| { let mut i: uint = 0u; while i < n_elts { push(t.clone()); @@ -312,7 +280,7 @@ mod test { fn test() { // Some code that could use that, then: fn seq_range(lo: uint, hi: uint) -> @[uint] { - do build |push| { + do build(None) |push| { for i in range(lo, hi) { push(i); } @@ -359,7 +327,7 @@ mod test { fn bench_build_sized(b: &mut bh) { let len = 64; do b.iter { - build_sized(len, |push| for i in range(0, 1024) { push(i) }); + build(Some(len), |push| for i in range(0, 1024) { push(i) }); } } @@ -367,7 +335,7 @@ mod test { fn bench_build(b: &mut bh) { do b.iter { for i in range(0, 95) { - build(|push| push(i)); + build(None, |push| push(i)); } } } diff --git a/src/libstd/io.rs b/src/libstd/io.rs index ee948446614..ee4100f1ec9 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -777,7 +777,7 @@ impl<T:Reader> ReaderUtil for T { } fn read_lines(&self) -> ~[~str] { - do vec::build |push| { + do vec::build(None) |push| { do self.each_line |line| { push(line.to_owned()); true diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 27566bf23c8..c45e37477ce 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -194,41 +194,7 @@ pub fn with_capacity<T>(capacity: uint) -> ~[T] { /** * Builds a vector by calling a provided function with an argument * function that pushes an element to the back of a vector. - * This version takes an initial capacity for the vector. - * - * # Arguments - * - * * size - An initial size of the vector to reserve - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] { - let mut vec = with_capacity(size); - builder(|x| vec.push(x)); - vec -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * - * # Arguments - * - * * builder - A function that will construct the vector. It receives - * as an argument a function that will push an element - * onto the vector being constructed. - */ -#[inline] -pub fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A] { - build_sized(4, builder) -} - -/** - * Builds a vector by calling a provided function with an argument - * function that pushes an element to the back of a vector. - * This version takes an initial size for the vector. + * The initial capacity for the vector may optionally be specified. * * # Arguments * @@ -238,8 +204,10 @@ pub fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A] { * onto the vector being constructed. */ #[inline] -pub fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] { - build_sized(size.unwrap_or_default(4), builder) +pub fn build<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) -> ~[A] { + let mut vec = with_capacity(size.unwrap_or_default(4)); + builder(|x| vec.push(x)); + vec } /// An iterator over the slices of a vector separated by elements that @@ -3248,7 +3216,7 @@ mod tests { #[test] #[should_fail] fn test_build_fail() { - do build |push| { + do build(None) |push| { push((~0, @0)); push((~0, @0)); push((~0, @0)); diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index 7050cfbedb7..8221be1bbcb 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -958,7 +958,7 @@ fn create_struct_pattern(cx: @ExtCtxt, // struct_type is definitely not Unknown, since struct_def.fields // must be nonempty to reach here let pattern = if struct_type == Record { - let field_pats = do vec::build |push| { + let field_pats = do vec::build(None) |push| { for (&pat, &(id, _)) in subpats.iter().zip(ident_expr.iter()) { // id is guaranteed to be Some push(ast::FieldPat { ident: id.unwrap(), pat: pat }) diff --git a/src/test/run-pass/issue-3563-3.rs b/src/test/run-pass/issue-3563-3.rs index e6429aa5508..3c4c4889ba2 100644 --- a/src/test/run-pass/issue-3563-3.rs +++ b/src/test/run-pass/issue-3563-3.rs @@ -66,7 +66,7 @@ impl Drop for AsciiArt { fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt { // Use an anonymous function to build a vector of vectors containing // blank characters for each position in our canvas. - let lines = do vec::build_sized(height) |push| { + let lines = do vec::build(Some(height)) |push| { do height.times { push(vec::from_elem(width, '.')); }