Skip to content

Commit 4349611

Browse files
committed
Add try_opt_collect!
1 parent bc4981f commit 4349611

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/expr.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,10 @@ 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 =
146+
try_opt_collect!(args.iter()
147+
.map(|arg| arg.rewrite(context, remaining_width, offset)
148+
.map(|arg_str| (arg_str, String::new()))));
150149
let fmt = ListFormatting {
151150
tactic: ListTactic::HorizontalVertical,
152151
separator: ",",
@@ -188,15 +187,15 @@ fn rewrite_struct_lit(context: &RewriteContext,
188187
let indent = offset + path_str.len() + 3;
189188
let budget = width - (path_str.len() + 5);
190189

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-
190+
let field_strs =
191+
try_opt_collect!(fields.iter()
192+
.map(|field| rewrite_field(context, field, budget, indent))
193+
.chain(base.iter()
194+
.map(|expr| expr.rewrite(context,
195+
// 2 = ".."
196+
budget - 2,
197+
indent + 2)
198+
.map(|s| format!("..{}", s)))));
200199
// FIXME comments
201200
let field_strs: Vec<_> = field_strs.into_iter().map(|s| (s, String::new())).collect();
202201
let fmt = ListFormatting {
@@ -239,15 +238,17 @@ fn rewrite_tuple_lit(context: &RewriteContext,
239238
return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
240239
}
241240
// 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-
}
241+
let item_strs =
242+
try_opt_collect!(items.iter()
243+
.enumerate()
244+
.map(|(i, item)| {
245+
let rem_width = if i == items.len() - 1 {
246+
width - 2
247+
} else {
248+
config!(max_width) - indent - 2
249+
};
250+
item.rewrite(context, rem_width, indent)
251+
}));
251252
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
252253
ListTactic::Vertical
253254
} else {

src/utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ macro_rules! try_opt {
168168
})
169169
}
170170

171+
// Unwraps an iterator of Options. If any element is None, return None
172+
#[macro_export]
173+
macro_rules! try_opt_collect {
174+
($expr:expr) => ({
175+
let mut res = Vec::with_capacity($expr.size_hint().0);
176+
for x in $expr {
177+
res.push(try_opt!(x));
178+
}
179+
res
180+
})
181+
}
182+
171183
#[test]
172184
fn power_rounding() {
173185
assert_eq!(1, round_up_to_power_of_two(1));

0 commit comments

Comments
 (0)