1
Fork 0

Use FromIterator implementation for Option

Combined with try_opt!, this avoid an explicit for loop or another macro.
This commit is contained in:
Gaëtan Cassiers 2015-06-22 13:33:19 +02:00
parent ad658885d4
commit b7ead806f4
2 changed files with 27 additions and 24 deletions

View file

@ -142,11 +142,11 @@ fn rewrite_call(context: &RewriteContext,
let arg_count = args.len(); let arg_count = args.len();
let args_str = if arg_count > 0 { let args_str = if arg_count > 0 {
let mut args_rewritten = Vec::with_capacity(args.len()); let args_rewritten: Vec<_> =
for arg in args.iter() { try_opt!(args.iter()
args_rewritten.push((try_opt!(arg.rewrite(context, remaining_width, offset)), .map(|arg| arg.rewrite(context, remaining_width, offset)
String::new())); .map(|arg_str| (arg_str, String::new())))
} .collect());
let fmt = ListFormatting { let fmt = ListFormatting {
tactic: ListTactic::HorizontalVertical, tactic: ListTactic::HorizontalVertical,
separator: ",", separator: ",",
@ -188,15 +188,16 @@ fn rewrite_struct_lit(context: &RewriteContext,
let indent = offset + path_str.len() + 3; let indent = offset + path_str.len() + 3;
let budget = width - (path_str.len() + 5); let budget = width - (path_str.len() + 5);
let mut field_strs = Vec::with_capacity(fields.len()); let field_strs: Vec<_> =
for field in fields.iter() { try_opt!(fields.iter()
field_strs.push(try_opt!(rewrite_field(context, field, budget, indent))); .map(|field| rewrite_field(context, field, budget, indent))
} .chain(base.iter()
if let Some(expr) = base { .map(|expr| expr.rewrite(context,
// Another 2 on the width/indent for the .. // 2 = ".."
field_strs.push(format!("..{}", try_opt!(expr.rewrite(context, budget - 2, indent + 2)))); budget - 2,
} indent + 2)
.map(|s| format!("..{}", s))))
.collect());
// FIXME comments // FIXME comments
let field_strs: Vec<_> = field_strs.into_iter().map(|s| (s, String::new())).collect(); let field_strs: Vec<_> = field_strs.into_iter().map(|s| (s, String::new())).collect();
let fmt = ListFormatting { let fmt = ListFormatting {
@ -239,15 +240,18 @@ fn rewrite_tuple_lit(context: &RewriteContext,
return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s)); return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
} }
// Only last line has width-1 as budget, other may take max_width // Only last line has width-1 as budget, other may take max_width
let mut item_strs = Vec::with_capacity(items.len()); let item_strs: Vec<_> =
for (i, item) in items.iter().enumerate() { try_opt!(items.iter()
.enumerate()
.map(|(i, item)| {
let rem_width = if i == items.len() - 1 { let rem_width = if i == items.len() - 1 {
width - 2 width - 2
} else { } else {
config!(max_width) - indent - 2 config!(max_width) - indent - 2
}; };
item_strs.push(try_opt!(item.rewrite(context, rem_width, indent))); item.rewrite(context, rem_width, indent)
} })
.collect());
let tactics = if item_strs.iter().any(|s| s.contains('\n')) { let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
ListTactic::Vertical ListTactic::Vertical
} else { } else {

View file

@ -37,7 +37,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
config!(max_width) - offset, config!(max_width) - offset,
offset) { offset) {
Some(new_str) => { Some(new_str) => {
//let new_str = self.rewrite_expr(ex, config!(max_width) - offset, offset);
self.changes.push_str_span(ex.span, &new_str); self.changes.push_str_span(ex.span, &new_str);
self.last_pos = ex.span.hi; self.last_pos = ex.span.hi;
} }