Skip to content

Commit b7ead80

Browse files
committed
Use FromIterator implementation for Option
Combined with try_opt!, this avoid an explicit for loop or another macro.
1 parent ad65888 commit b7ead80

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/expr.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ fn rewrite_call(context: &RewriteContext,
142142
let arg_count = args.len();
143143

144144
let args_str = if arg_count > 0 {
145-
let mut args_rewritten = Vec::with_capacity(args.len());
146-
for arg in args.iter() {
147-
args_rewritten.push((try_opt!(arg.rewrite(context, remaining_width, offset)),
148-
String::new()));
149-
}
145+
let args_rewritten: Vec<_> =
146+
try_opt!(args.iter()
147+
.map(|arg| arg.rewrite(context, remaining_width, offset)
148+
.map(|arg_str| (arg_str, String::new())))
149+
.collect());
150150
let fmt = ListFormatting {
151151
tactic: ListTactic::HorizontalVertical,
152152
separator: ",",
@@ -188,15 +188,16 @@ fn rewrite_struct_lit(context: &RewriteContext,
188188
let indent = offset + path_str.len() + 3;
189189
let budget = width - (path_str.len() + 5);
190190

191-
let mut field_strs = Vec::with_capacity(fields.len());
192-
for field in fields.iter() {
193-
field_strs.push(try_opt!(rewrite_field(context, field, budget, indent)));
194-
}
195-
if let Some(expr) = base {
196-
// Another 2 on the width/indent for the ..
197-
field_strs.push(format!("..{}", try_opt!(expr.rewrite(context, budget - 2, indent + 2))));
198-
}
199-
191+
let field_strs: Vec<_> =
192+
try_opt!(fields.iter()
193+
.map(|field| rewrite_field(context, field, budget, indent))
194+
.chain(base.iter()
195+
.map(|expr| expr.rewrite(context,
196+
// 2 = ".."
197+
budget - 2,
198+
indent + 2)
199+
.map(|s| format!("..{}", s))))
200+
.collect());
200201
// FIXME comments
201202
let field_strs: Vec<_> = field_strs.into_iter().map(|s| (s, String::new())).collect();
202203
let fmt = ListFormatting {
@@ -239,15 +240,18 @@ fn rewrite_tuple_lit(context: &RewriteContext,
239240
return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
240241
}
241242
// Only last line has width-1 as budget, other may take max_width
242-
let mut item_strs = Vec::with_capacity(items.len());
243-
for (i, item) in items.iter().enumerate() {
244-
let rem_width = if i == items.len() - 1 {
245-
width - 2
246-
} else {
247-
config!(max_width) - indent - 2
248-
};
249-
item_strs.push(try_opt!(item.rewrite(context, rem_width, indent)));
250-
}
243+
let item_strs: Vec<_> =
244+
try_opt!(items.iter()
245+
.enumerate()
246+
.map(|(i, item)| {
247+
let rem_width = if i == items.len() - 1 {
248+
width - 2
249+
} else {
250+
config!(max_width) - indent - 2
251+
};
252+
item.rewrite(context, rem_width, indent)
253+
})
254+
.collect());
251255
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
252256
ListTactic::Vertical
253257
} else {

src/visitor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
3737
config!(max_width) - offset,
3838
offset) {
3939
Some(new_str) => {
40-
//let new_str = self.rewrite_expr(ex, config!(max_width) - offset, offset);
4140
self.changes.push_str_span(ex.span, &new_str);
4241
self.last_pos = ex.span.hi;
4342
}

0 commit comments

Comments
 (0)