Skip to content

Commit d79fbba

Browse files
committed
auto merge of #13203 : Kimundi/rust/de-map-vec3, r=cmr
They required unnecessary temporaries, are replaced with iterators, and would conflict with a possible future `Iterable` trait.
2 parents 86890b9 + c356e3b commit d79fbba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+278
-325
lines changed

src/doc/tutorial.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,10 @@ access local variables in the enclosing scope.
17641764
17651765
~~~~
17661766
let mut max = 0;
1767-
[1, 2, 3].map(|x| if *x > max { max = *x });
1767+
let f = |x: int| if x > max { max = x };
1768+
for x in [1, 2, 3].iter() {
1769+
f(*x);
1770+
}
17681771
~~~~
17691772
17701773
Stack closures are very efficient because their environment is

src/libnative/io/process.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: proc:(**libc::c_char) -> T) -> T
597597
// Next, convert each of the byte strings into a pointer. This is
598598
// technically unsafe as the caller could leak these pointers out of our
599599
// scope.
600-
let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
600+
let mut ptrs: Vec<_> = tmps.iter().map(|tmp| tmp.with_ref(|buf| buf)).collect();
601601

602602
// Finally, make sure we add a null pointer.
603603
ptrs.push(ptr::null());
@@ -622,7 +622,9 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: proc:(*c_void) -> T) -> T {
622622
}
623623

624624
// Once again, this is unsafe.
625-
let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
625+
let mut ptrs: Vec<*libc::c_char> = tmps.iter()
626+
.map(|tmp| tmp.with_ref(|buf| buf))
627+
.collect();
626628
ptrs.push(ptr::null());
627629

628630
cb(ptrs.as_ptr() as *c_void)

src/librustc/back/lto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
6969
}
7070

7171
// Internalize everything but the reachable symbols of the current module
72-
let cstrs = reachable.map(|s| s.to_c_str());
73-
let arr = cstrs.map(|c| c.with_ref(|p| p));
72+
let cstrs: Vec<::std::c_str::CString> = reachable.iter().map(|s| s.to_c_str()).collect();
73+
let arr: Vec<*i8> = cstrs.iter().map(|c| c.with_ref(|p| p)).collect();
7474
let ptr = arr.as_ptr();
7575
unsafe {
7676
llvm::LLVMRustRunRestrictionPass(llmod, ptr as **libc::c_char,

src/librustc/driver/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,9 @@ pub fn build_session_options(matches: &getopts::Matches) -> session::Options {
943943
NoDebugInfo
944944
};
945945

946-
let addl_lib_search_paths = matches.opt_strs("L").map(|s| {
946+
let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| {
947947
Path::new(s.as_slice())
948-
}).move_iter().collect();
948+
}).collect();
949949

950950
let cfg = parse_cfgspecs(matches.opt_strs("cfg").move_iter().collect());
951951
let test = matches.opt_present("test");

src/librustc/lib/llvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ impl TypeNames {
18611861
}
18621862

18631863
pub fn types_to_str(&self, tys: &[Type]) -> ~str {
1864-
let strs = tys.map(|t| self.type_to_str(*t));
1864+
let strs: Vec<~str> = tys.iter().map(|t| self.type_to_str(*t)).collect();
18651865
format!("[{}]", strs.connect(","))
18661866
}
18671867

src/librustc/metadata/filesearch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ pub fn get_rust_path() -> Option<~str> {
200200
pub fn rust_path() -> Vec<Path> {
201201
let mut env_rust_path: Vec<Path> = match get_rust_path() {
202202
Some(env_path) => {
203-
let env_path_components: Vec<&str> =
204-
env_path.split_str(PATH_ENTRY_SEPARATOR).collect();
205-
env_path_components.map(|&s| Path::new(s))
203+
let env_path_components =
204+
env_path.split_str(PATH_ENTRY_SEPARATOR);
205+
env_path_components.map(|s| Path::new(s)).collect()
206206
}
207207
None => Vec::new()
208208
};

src/librustc/middle/check_match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn raw_pat(p: @Pat) -> @Pat {
163163

164164
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, pats: Vec<@Pat> ) {
165165
assert!((!pats.is_empty()));
166-
let ext = match is_useful(cx, &pats.map(|p| vec!(*p)), [wild()]) {
166+
let ext = match is_useful(cx, &pats.iter().map(|p| vec!(*p)).collect(), [wild()]) {
167167
not_useful => {
168168
// This is good, wildcard pattern isn't reachable
169169
return;
@@ -692,12 +692,12 @@ fn specialize(cx: &MatchCheckCtxt,
692692
DefVariant(_, variant_id, _) => {
693693
if variant(variant_id) == *ctor_id {
694694
let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id);
695-
let args = struct_fields.map(|sf| {
695+
let args = struct_fields.iter().map(|sf| {
696696
match pattern_fields.iter().find(|f| f.ident.name == sf.name) {
697697
Some(f) => f.pat,
698698
_ => wild()
699699
}
700-
});
700+
}).collect();
701701
Some(vec::append(args, r.tail()))
702702
} else {
703703
None

src/librustc/middle/resolve.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -4707,18 +4707,20 @@ impl<'a> Resolver<'a> {
47074707
path: &Path,
47084708
namespace: Namespace)
47094709
-> Option<(Def, LastPrivate)> {
4710-
let module_path_idents = path.segments.init().map(|ps| ps.identifier);
4710+
let module_path_idents = path.segments.init().iter()
4711+
.map(|ps| ps.identifier)
4712+
.collect::<Vec<_>>();
47114713

47124714
let containing_module;
47134715
let last_private;
47144716
match self.resolve_module_path(self.current_module,
4715-
module_path_idents,
4717+
module_path_idents.as_slice(),
47164718
UseLexicalScope,
47174719
path.span,
47184720
PathSearch) {
47194721
Failed => {
47204722
let msg = format!("use of undeclared module `{}`",
4721-
self.idents_to_str(module_path_idents));
4723+
self.idents_to_str(module_path_idents.as_slice()));
47224724
self.resolve_error(path.span, msg);
47234725
return None;
47244726
}
@@ -4772,21 +4774,23 @@ impl<'a> Resolver<'a> {
47724774
path: &Path,
47734775
namespace: Namespace)
47744776
-> Option<(Def, LastPrivate)> {
4775-
let module_path_idents = path.segments.init().map(|ps| ps.identifier);
4777+
let module_path_idents = path.segments.init().iter()
4778+
.map(|ps| ps.identifier)
4779+
.collect::<Vec<_>>();
47764780

47774781
let root_module = self.graph_root.get_module();
47784782

47794783
let containing_module;
47804784
let last_private;
47814785
match self.resolve_module_path_from_root(root_module,
4782-
module_path_idents,
4786+
module_path_idents.as_slice(),
47834787
0,
47844788
path.span,
47854789
PathSearch,
47864790
LastMod(AllPublic)) {
47874791
Failed => {
47884792
let msg = format!("use of undeclared module `::{}`",
4789-
self.idents_to_str(module_path_idents));
4793+
self.idents_to_str(module_path_idents.as_slice()));
47904794
self.resolve_error(path.span, msg);
47914795
return None;
47924796
}

src/librustc/middle/resolve_lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'a> LifetimeContext<'a> {
216216
referenced_idents={:?} \
217217
early_count={}",
218218
n,
219-
referenced_idents.map(lifetime_show),
219+
referenced_idents.iter().map(lifetime_show).collect::<Vec<token::InternedString>>(),
220220
early_count);
221221
if referenced_idents.is_empty() {
222222
let scope1 = LateScope(n, &generics.lifetimes, scope);

src/librustc/middle/subst.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<T:Subst> Subst for Vec<T> {
134134
fn subst_spanned(&self, tcx: &ty::ctxt,
135135
substs: &ty::substs,
136136
span: Option<Span>) -> Vec<T> {
137-
self.map(|t| t.subst_spanned(tcx, substs, span))
137+
self.iter().map(|t| t.subst_spanned(tcx, substs, span)).collect()
138138
}
139139
}
140140
impl<T:Subst> Subst for Rc<T> {
@@ -189,7 +189,7 @@ impl Subst for ty::substs {
189189
ty::substs {
190190
regions: self.regions.subst_spanned(tcx, substs, span),
191191
self_ty: self.self_ty.map(|typ| typ.subst_spanned(tcx, substs, span)),
192-
tps: self.tps.map(|typ| typ.subst_spanned(tcx, substs, span))
192+
tps: self.tps.iter().map(|typ| typ.subst_spanned(tcx, substs, span)).collect()
193193
}
194194
}
195195
}

src/librustc/middle/trans/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,10 @@ fn compile_submatch_continue<'r,
15781578
let pat_ty = node_id_type(bcx, pat_id);
15791579
let pat_repr = adt::represent_type(bcx.ccx(), pat_ty);
15801580
expr::with_field_tys(tcx, pat_ty, Some(pat_id), |discr, field_tys| {
1581-
let rec_vals = rec_fields.map(|field_name| {
1581+
let rec_vals = rec_fields.iter().map(|field_name| {
15821582
let ix = ty::field_idx_strict(tcx, field_name.name, field_tys);
15831583
adt::trans_field_ptr(bcx, pat_repr, val, discr, ix)
1584-
});
1584+
}).collect();
15851585
compile_submatch(
15861586
bcx,
15871587
enter_rec_or_struct(bcx,

src/librustc/middle/trans/adt.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
136136
}
137137
ty::ty_struct(def_id, ref substs) => {
138138
let fields = ty::lookup_struct_fields(cx.tcx(), def_id);
139-
let mut ftys = fields.map(|field| {
139+
let mut ftys = fields.iter().map(|field| {
140140
ty::lookup_field_type(cx.tcx(), def_id, field.id, substs)
141-
});
141+
}).collect::<Vec<_>>();
142142
let packed = ty::lookup_packed(cx.tcx(), def_id);
143143
let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag();
144144
if dtor { ftys.push(ty::mk_bool()); }
@@ -158,7 +158,7 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
158158

159159
if cases.iter().all(|c| c.tys.len() == 0) {
160160
// All bodies empty -> intlike
161-
let discrs = cases.map(|c| c.discr);
161+
let discrs: Vec<u64> = cases.iter().map(|c| c.discr).collect();
162162
let bounds = IntBounds {
163163
ulo: *discrs.iter().min().unwrap(),
164164
uhi: *discrs.iter().max().unwrap(),
@@ -218,12 +218,12 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
218218
let bounds = IntBounds { ulo: 0, uhi: (cases.len() - 1) as u64,
219219
slo: 0, shi: (cases.len() - 1) as i64 };
220220
let ity = range_to_inttype(cx, hint, &bounds);
221-
return General(ity, cases.map(|c| {
221+
return General(ity, cases.iter().map(|c| {
222222
let discr = vec!(ty_of_inttype(ity));
223223
mk_struct(cx,
224224
vec::append(discr, c.tys.as_slice()).as_slice(),
225225
false)
226-
}))
226+
}).collect())
227227
}
228228
_ => cx.sess().bug("adt::represent_type called on non-ADT type")
229229
}
@@ -270,18 +270,18 @@ impl Case {
270270
}
271271

272272
fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &ty::substs) -> Vec<Case> {
273-
ty::enum_variants(tcx, def_id).map(|vi| {
274-
let arg_tys = vi.args.map(|&raw_ty| {
273+
ty::enum_variants(tcx, def_id).iter().map(|vi| {
274+
let arg_tys = vi.args.iter().map(|&raw_ty| {
275275
ty::subst(tcx, substs, raw_ty)
276-
});
276+
}).collect();
277277
Case { discr: vi.disr_val, tys: arg_tys }
278-
})
278+
}).collect()
279279
}
280280

281281

282282
fn mk_struct(cx: &CrateContext, tys: &[ty::t], packed: bool) -> Struct {
283-
let lltys = tys.map(|&ty| type_of::sizing_type_of(cx, ty));
284-
let llty_rec = Type::struct_(cx, lltys, packed);
283+
let lltys = tys.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
284+
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
285285
Struct {
286286
size: machine::llsize_of_alloc(cx, llty_rec) /*bad*/as u64,
287287
align: machine::llalign_of_min(cx, llty_rec) /*bad*/as u64,
@@ -464,9 +464,9 @@ fn generic_type_of(cx: &CrateContext, r: &Repr, name: Option<&str>, sizing: bool
464464

465465
fn struct_llfields(cx: &CrateContext, st: &Struct, sizing: bool) -> Vec<Type> {
466466
if sizing {
467-
st.fields.map(|&ty| type_of::sizing_type_of(cx, ty))
467+
st.fields.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
468468
} else {
469-
st.fields.map(|&ty| type_of::type_of(cx, ty))
469+
st.fields.iter().map(|&ty| type_of::type_of(cx, ty)).collect()
470470
}
471471
}
472472

@@ -700,7 +700,7 @@ fn struct_field_ptr(bcx: &Block, st: &Struct, val: ValueRef, ix: uint,
700700
let ccx = bcx.ccx();
701701

702702
let val = if needs_cast {
703-
let fields = st.fields.map(|&ty| type_of::type_of(ccx, ty));
703+
let fields = st.fields.iter().map(|&ty| type_of::type_of(ccx, ty)).collect::<Vec<_>>();
704704
let real_ty = Type::struct_(ccx, fields.as_slice(), st.packed);
705705
PointerCast(bcx, val, real_ty.ptr_to())
706706
} else {
@@ -773,11 +773,11 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
773773
vals).as_slice(),
774774
false)
775775
} else {
776-
let vals = nonnull.fields.map(|&ty| {
776+
let vals = nonnull.fields.iter().map(|&ty| {
777777
// Always use null even if it's not the `ptrfield`th
778778
// field; see #8506.
779779
C_null(type_of::sizing_type_of(ccx, ty))
780-
}).move_iter().collect::<Vec<ValueRef> >();
780+
}).collect::<Vec<ValueRef>>();
781781
C_struct(ccx, build_const_struct(ccx,
782782
nonnull,
783783
vals.as_slice()).as_slice(),

src/librustc/middle/trans/asm.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
3636
let temp_scope = fcx.push_custom_cleanup_scope();
3737

3838
// Prepare the output operands
39-
let outputs = ia.outputs.map(|&(ref c, out)| {
39+
let outputs = ia.outputs.iter().map(|&(ref c, out)| {
4040
constraints.push((*c).clone());
4141

4242
let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
4343
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
4444
out_datum.val
4545

46-
});
46+
}).collect::<Vec<_>>();
4747

4848
// Now the input operands
49-
let inputs = ia.inputs.map(|&(ref c, input)| {
49+
let inputs = ia.inputs.iter().map(|&(ref c, input)| {
5050
constraints.push((*c).clone());
5151

5252
let in_datum = unpack_datum!(bcx, expr::trans(bcx, input));
@@ -57,12 +57,15 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
5757
cleanup::CustomScope(temp_scope),
5858
callee::DontAutorefArg)
5959
})
60-
});
60+
}).collect::<Vec<_>>();
6161

6262
// no failure occurred preparing operands, no need to cleanup
6363
fcx.pop_custom_cleanup_scope(temp_scope);
6464

65-
let mut constraints = constraints.map(|s| s.get().to_str()).connect(",");
65+
let mut constraints = constraints.iter()
66+
.map(|s| s.get().to_str())
67+
.collect::<Vec<~str>>()
68+
.connect(",");
6669

6770
let mut clobbers = getClobbers();
6871
if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {

src/librustc/middle/trans/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn Invoke(cx: &Block,
121121
terminate(cx, "Invoke");
122122
debug!("Invoke({} with arguments ({}))",
123123
cx.val_to_str(fn_),
124-
args.map(|a| cx.val_to_str(*a)).connect(", "));
124+
args.iter().map(|a| cx.val_to_str(*a)).collect::<Vec<~str>>().connect(", "));
125125
B(cx).invoke(fn_, args, then, catch, attributes)
126126
}
127127

src/librustc/middle/trans/builder.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,13 @@ impl<'a> Builder<'a> {
780780
let alignstack = if alignstack { lib::llvm::True }
781781
else { lib::llvm::False };
782782

783-
let argtys = inputs.map(|v| {
783+
let argtys = inputs.iter().map(|v| {
784784
debug!("Asm Input Type: {:?}", self.ccx.tn.val_to_str(*v));
785785
val_ty(*v)
786-
});
786+
}).collect::<Vec<_>>();
787787

788788
debug!("Asm Output Type: {:?}", self.ccx.tn.type_to_str(output));
789-
let fty = Type::func(argtys, &output);
789+
let fty = Type::func(argtys.as_slice(), &output);
790790
unsafe {
791791
let v = llvm::LLVMInlineAsm(
792792
fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
@@ -800,7 +800,10 @@ impl<'a> Builder<'a> {
800800

801801
debug!("Call {} with args ({})",
802802
self.ccx.tn.val_to_str(llfn),
803-
args.map(|&v| self.ccx.tn.val_to_str(v)).connect(", "));
803+
args.iter()
804+
.map(|&v| self.ccx.tn.val_to_str(v))
805+
.collect::<Vec<~str>>()
806+
.connect(", "));
804807

805808
unsafe {
806809
let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),

src/librustc/middle/trans/common.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,10 @@ pub fn node_id_type_params(bcx: &Block, node: ExprOrMethodCall) -> Vec<ty::t> {
816816
if !params.iter().all(|t| !ty::type_needs_infer(*t)) {
817817
bcx.sess().bug(
818818
format!("type parameters for node {:?} include inference types: {}",
819-
node, params.map(|t| bcx.ty_to_str(*t)).connect(",")));
819+
node, params.iter()
820+
.map(|t| bcx.ty_to_str(*t))
821+
.collect::<Vec<~str>>()
822+
.connect(",")));
820823
}
821824

822825
match bcx.fcx.param_substs {

0 commit comments

Comments
 (0)