Skip to content

Use vec![] for vector creation #26904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl BitVec {
pub fn from_elem(nbits: usize, bit: bool) -> BitVec {
let nblocks = blocks_for_bits(nbits);
let mut bit_vec = BitVec {
storage: repeat(if bit { !0 } else { 0 }).take(nblocks).collect(),
storage: vec![if bit { !0 } else { 0 }; nblocks],
nbits: nbits
};
bit_vec.fix_last_block();
Expand Down
8 changes: 4 additions & 4 deletions src/libcollectionstest/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ mod bench {

#[bench]
fn mut_iterator(b: &mut Bencher) {
let mut v: Vec<_> = repeat(0).take(100).collect();
let mut v = vec![0; 100];

b.iter(|| {
let mut i = 0;
Expand Down Expand Up @@ -1419,7 +1419,7 @@ mod bench {
#[bench]
fn zero_1kb_from_elem(b: &mut Bencher) {
b.iter(|| {
repeat(0u8).take(1024).collect::<Vec<_>>()
vec![0u8; 1024]
});
}

Expand Down Expand Up @@ -1467,7 +1467,7 @@ mod bench {
fn random_inserts(b: &mut Bencher) {
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = repeat((0, 0)).take(30).collect();
let mut v = vec![(0, 0); 30];
for _ in 0..100 {
let l = v.len();
v.insert(rng.gen::<usize>() % (l + 1),
Expand All @@ -1479,7 +1479,7 @@ mod bench {
fn random_removes(b: &mut Bencher) {
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = repeat((0, 0)).take(130).collect();
let mut v = vec![(0, 0); 130];
for _ in 0..100 {
let l = v.len();
v.remove(rng.gen::<usize>() % l);
Expand Down
5 changes: 2 additions & 3 deletions src/libcoretest/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use core::ptr::*;
use core::mem;
use std::iter::repeat;

#[test]
fn test() {
Expand Down Expand Up @@ -110,7 +109,7 @@ fn test_as_mut() {
#[test]
fn test_ptr_addition() {
unsafe {
let xs = repeat(5).take(16).collect::<Vec<_>>();
let xs = vec![5; 16];
let mut ptr = xs.as_ptr();
let end = ptr.offset(16);

Expand All @@ -128,7 +127,7 @@ fn test_ptr_addition() {
m_ptr = m_ptr.offset(1);
}

assert!(xs_mut == repeat(10).take(16).collect::<Vec<_>>());
assert!(xs_mut == vec![10; 16]);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/libgraphviz/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ mod tests {
use std::io;
use std::io::prelude::*;
use std::borrow::IntoCow;
use std::iter::repeat;

/// each node is an index in a vector in the graph.
type Node = usize;
Expand Down Expand Up @@ -647,7 +646,7 @@ mod tests {
fn to_opt_strs(self) -> Vec<Option<&'static str>> {
match self {
UnlabelledNodes(len)
=> repeat(None).take(len).collect(),
=> vec![None; len],
AllNodesLabelled(lbls)
=> lbls.into_iter().map(
|l|Some(l)).collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/librand/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
const FILL_BYTES_V_LEN: usize = 13579;
#[test]
fn test_rng_fill_bytes() {
let mut v = repeat(0).take(FILL_BYTES_V_LEN).collect::<Vec<_>>();
let mut v = vec![0; FILL_BYTES_V_LEN];
::test::rng().fill_bytes(&mut v);

// Sanity test: if we've gotten here, `fill_bytes` has not infinitely
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ fn is_useful(cx: &MatchCheckCtxt,
match is_useful(cx, &matrix, v.tail(), witness) {
UsefulWithWitness(pats) => {
let arity = constructor_arity(cx, &constructor, left_ty);
let wild_pats: Vec<_> = repeat(DUMMY_WILD_PAT).take(arity).collect();
let wild_pats = vec![DUMMY_WILD_PAT; arity];
let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
let mut new_pats = vec![enum_pat];
new_pats.extend(pats);
Expand Down Expand Up @@ -862,7 +862,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
} = raw_pat(r[col]);
let head: Option<Vec<&Pat>> = match *node {
ast::PatWild(_) =>
Some(repeat(DUMMY_WILD_PAT).take(arity).collect()),
Some(vec![DUMMY_WILD_PAT; arity]),

ast::PatIdent(_, _, _) => {
let opt_def = cx.tcx.def_map.borrow().get(&pat_id).map(|d| d.full_def());
Expand All @@ -875,7 +875,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
} else {
None
},
_ => Some(repeat(DUMMY_WILD_PAT).take(arity).collect())
_ => Some(vec![DUMMY_WILD_PAT; arity])
}
}

Expand All @@ -889,7 +889,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
DefVariant(..) | DefStruct(..) => {
Some(match args {
&Some(ref args) => args.iter().map(|p| &**p).collect(),
&None => repeat(DUMMY_WILD_PAT).take(arity).collect(),
&None => vec![DUMMY_WILD_PAT; arity],
})
}
_ => None
Expand Down
13 changes: 6 additions & 7 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use middle::cfg::CFGIndex;
use middle::ty;
use std::io;
use std::usize;
use std::iter::repeat;
use syntax::ast;
use syntax::ast_util::IdRange;
use syntax::visit;
Expand Down Expand Up @@ -239,11 +238,11 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {

let entry = if oper.initial_value() { usize::MAX } else {0};

let zeroes: Vec<_> = repeat(0).take(num_nodes * words_per_id).collect();
let gens: Vec<_> = zeroes.clone();
let kills1: Vec<_> = zeroes.clone();
let kills2: Vec<_> = zeroes;
let on_entry: Vec<_> = repeat(entry).take(num_nodes * words_per_id).collect();
let zeroes = vec![0; num_nodes * words_per_id];
let gens = zeroes.clone();
let kills1 = zeroes.clone();
let kills2 = zeroes;
let on_entry = vec![entry; num_nodes * words_per_id];

let nodeid_to_index = build_nodeid_to_index(decl, cfg);

Expand Down Expand Up @@ -511,7 +510,7 @@ impl<'a, 'tcx, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, 'tcx, O> {
changed: true
};

let mut temp: Vec<_> = repeat(0).take(words_per_id).collect();
let mut temp = vec![0; words_per_id];
while propcx.changed {
propcx.changed = false;
propcx.reset(&mut temp);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/infer/region_inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use util::nodemap::{FnvHashMap, FnvHashSet};
use std::cell::{Cell, RefCell};
use std::cmp::Ordering::{self, Less, Greater, Equal};
use std::fmt;
use std::iter::repeat;
use std::u32;
use syntax::ast;

Expand Down Expand Up @@ -1304,7 +1303,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
// idea is to report errors that derive from independent
// regions of the graph, but not those that derive from
// overlapping locations.
let mut dup_vec: Vec<_> = repeat(u32::MAX).take(self.num_vars() as usize).collect();
let mut dup_vec = vec![u32::MAX; self.num_vars() as usize];

for idx in 0..self.num_vars() as usize {
match var_data[idx].value {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ use util::nodemap::NodeMap;
use std::{fmt, usize};
use std::io::prelude::*;
use std::io;
use std::iter::repeat;
use std::rc::Rc;
use syntax::ast::{self, NodeId, Expr};
use syntax::codemap::{BytePos, original_sp, Span};
Expand Down Expand Up @@ -566,8 +565,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
Liveness {
ir: ir,
s: specials,
successors: repeat(invalid_node()).take(num_live_nodes).collect(),
users: repeat(invalid_users()).take(num_live_nodes * num_vars).collect(),
successors: vec![invalid_node(); num_live_nodes],
users: vec![invalid_users(); num_live_nodes * num_vars],
loop_scope: Vec::new(),
break_ln: NodeMap(),
cont_ln: NodeMap(),
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_back/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//! use. This implementation is not intended for external use or for any use where security is
//! important.

use std::iter::repeat;
use std::slice::bytes::{MutableByteVector, copy_memory};
use serialize::hex::ToHex;

Expand Down Expand Up @@ -255,7 +254,7 @@ pub trait Digest {
/// Convenience function that retrieves the result of a digest as a
/// newly allocated vec of bytes.
fn result_bytes(&mut self) -> Vec<u8> {
let mut buf: Vec<u8> = repeat(0).take((self.output_bits()+7)/8).collect();
let mut buf = vec![0; (self.output_bits()+7)/8];
self.result(&mut buf);
buf
}
Expand Down Expand Up @@ -534,7 +533,6 @@ mod tests {
use self::rand::Rng;
use self::rand::isaac::IsaacRng;
use serialize::hex::FromHex;
use std::iter::repeat;
use std::u64;
use super::{Digest, Sha256, FixedBuffer};

Expand Down Expand Up @@ -613,7 +611,7 @@ mod tests {
/// correct.
fn test_digest_1million_random<D: Digest>(digest: &mut D, blocksize: usize, expected: &str) {
let total_size = 1000000;
let buffer: Vec<u8> = repeat('a' as u8).take(blocksize * 2).collect();
let buffer = vec![b'a'; blocksize * 2];
let mut rng = IsaacRng::new_unseeded();
let mut count = 0;

Expand Down
4 changes: 1 addition & 3 deletions src/librustc_data_structures/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::iter;

/// A very simple BitVector type.
pub struct BitVector {
data: Vec<u64>
Expand All @@ -18,7 +16,7 @@ pub struct BitVector {
impl BitVector {
pub fn new(num_bits: usize) -> BitVector {
let num_words = (num_bits + 63) / 64;
BitVector { data: iter::repeat(0).take(num_words).collect() }
BitVector { data: vec![0; num_words] }
}

fn word_mask(&self, bit: usize) -> (usize, u64) {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/trans/cabi_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use trans::context::CrateContext;
use trans::type_::Type;

use std::cmp;
use std::iter::repeat;

#[derive(Clone, Copy, PartialEq)]
enum RegClass {
Expand Down Expand Up @@ -319,7 +318,7 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
}

let words = (ty_size(ty) + 7) / 8;
let mut cls: Vec<_> = repeat(NoClass).take(words).collect();
let mut cls = vec![NoClass; words];
if words > 4 {
all_mem(&mut cls);
return cls;
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use middle::subst::Substs;
use middle::ty::{self, Ty};
use util::nodemap::NodeMap;

use std::iter::repeat;
use libc::c_uint;
use syntax::{ast, ast_util};
use syntax::parse::token;
Expand Down Expand Up @@ -780,7 +779,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
let llunitty = type_of::type_of(cx, unit_ty);
let n = cx.tcx().eval_repeat_count(count);
let unit_val = const_expr(cx, &**elem, param_substs, fn_args).0;
let vs: Vec<_> = repeat(unit_val).take(n).collect();
let vs = vec![unit_val; n];
if val_ty(unit_val) != llunitty {
C_struct(cx, &vs[..], false)
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ use syntax::{ast, ast_util, codemap};
use syntax::parse::token::InternedString;
use syntax::ptr::P;
use syntax::parse::token;
use std::iter::repeat;
use std::mem;

// Destinations
Expand Down Expand Up @@ -1401,7 +1400,7 @@ fn trans_struct<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,

let tcx = bcx.tcx();
with_field_tys(tcx, ty, Some(expr_id), |discr, field_tys| {
let mut need_base: Vec<bool> = repeat(true).take(field_tys.len()).collect();
let mut need_base = vec![true; field_tys.len()];

let numbered_fields = fields.iter().map(|field| {
let opt_pos =
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/trans/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::ffi::CString;
use std::mem;
use std::ptr;
use std::cell::RefCell;
use std::iter::repeat;

use libc::c_uint;

Expand Down Expand Up @@ -253,7 +252,7 @@ impl Type {
if n_elts == 0 {
return Vec::new();
}
let mut elts: Vec<_> = repeat(Type { rf: ptr::null_mut() }).take(n_elts).collect();
let mut elts = vec![Type { rf: ptr::null_mut() }; n_elts];
llvm::LLVMGetStructElementTypes(self.to_ref(),
elts.as_mut_ptr() as *mut TypeRef);
elts
Expand All @@ -267,7 +266,7 @@ impl Type {
pub fn func_params(&self) -> Vec<Type> {
unsafe {
let n_args = llvm::LLVMCountParamTypes(self.to_ref()) as usize;
let mut args: Vec<_> = repeat(Type { rf: ptr::null_mut() }).take(n_args).collect();
let mut args = vec![Type { rf: ptr::null_mut() }; n_args];
llvm::LLVMGetParamTypes(self.to_ref(),
args.as_mut_ptr() as *mut TypeRef);
args
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ use rscope::{self, UnelidableRscope, RegionScope, ElidableRscope, ExplicitRscope
use util::common::{ErrorReported, FN_OUTPUT_NAME};
use util::nodemap::FnvHashSet;

use std::iter::repeat;
use std::slice;
use syntax::{abi, ast, ast_util};
use syntax::codemap::{Span, Pos};
Expand Down Expand Up @@ -588,7 +587,7 @@ fn convert_parenthesized_parameters<'tcx>(this: &AstConv<'tcx>,
0, &region_substs, a_t))
.collect::<Vec<Ty<'tcx>>>();

let input_params: Vec<_> = repeat(String::new()).take(inputs.len()).collect();
let input_params = vec![String::new(); inputs.len()];
let implied_output_region = find_implied_output_region(this.tcx(), &inputs, input_params);

let input_ty = this.tcx().mk_tup(inputs);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_typeck/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use middle::infer;
use middle::infer::InferCtxt;
use syntax::ast;
use syntax::codemap::Span;
use std::iter::repeat;

struct ConfirmContext<'a, 'tcx:'a> {
fcx: &'a FnCtxt<'a, 'tcx>,
Expand Down Expand Up @@ -322,7 +321,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
} else if num_supplied_types != num_method_types {
span_err!(self.tcx().sess, self.span, E0036,
"incorrect number of type parameters given for this method");
repeat(self.tcx().types.err).take(num_method_types).collect()
vec![self.tcx().types.err; num_method_types]
} else {
supplied_method_types
}
Expand Down
Loading