Merge pull request #1042 from imjacobclark/1014-refactoring-vector-string-to-join

Refactor string collects to itertools join
This commit is contained in:
Marcus Klaas de Vries 2016-06-07 20:21:54 +02:00
commit c31a366f0b
2 changed files with 35 additions and 31 deletions

View File

@ -21,6 +21,7 @@ use comment::{FindUncommented, contains_comment};
use visitor::FmtVisitor;
use rewrite::{Rewrite, RewriteContext};
use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, FnArgLayoutStyle};
use itertools::Itertools;
use syntax::{ast, abi, ptr, codemap};
use syntax::codemap::{Span, BytePos, mk_sp};
@ -1054,10 +1055,10 @@ pub fn rewrite_associated_type(ident: ast::Ident,
let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
let bounds: &[_] = &ty_param_bounds;
let bound_str = bounds.iter()
.filter_map(|ty_bound| ty_bound.rewrite(context, context.config.max_width, indent))
.collect::<Vec<String>>()
.join(" + ");
let bound_str = try_opt!(bounds.iter()
.map(|ty_bound| ty_bound.rewrite(context, context.config.max_width, indent))
.intersperse(Some(" + ".to_string()))
.collect::<Option<String>>());
if bounds.len() > 0 {
format!(": {}", bound_str)
} else {
@ -1700,10 +1701,10 @@ fn rewrite_trait_bounds(context: &RewriteContext,
return Some(String::new());
}
let bound_str = bounds.iter()
.filter_map(|ty_bound| ty_bound.rewrite(&context, width, indent))
.collect::<Vec<String>>()
.join(" + ");
let bound_str = try_opt!(bounds.iter()
.map(|ty_bound| ty_bound.rewrite(&context, width, indent))
.intersperse(Some(" + ".to_string()))
.collect::<Option<String>>());
let mut result = String::new();
result.push_str(": ");

View File

@ -23,6 +23,7 @@ use rewrite::{Rewrite, RewriteContext};
use utils::{extra_offset, format_mutability, wrap_str};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use config::TypeDensity;
use itertools::Itertools;
// Does not wrap on simple segments.
pub fn rewrite_path(context: &RewriteContext,
@ -325,39 +326,40 @@ impl Rewrite for ast::WherePredicate {
let type_str = try_opt!(bounded_ty.rewrite(context, width, offset));
if !bound_lifetimes.is_empty() {
let lifetime_str = try_opt!(bound_lifetimes.iter()
let lifetime_str: String = try_opt!(bound_lifetimes.iter()
.map(|lt| {
lt.rewrite(context,
width,
offset)
})
.collect::<Option<Vec<_>>>())
.join(", ");
.intersperse(Some(", ".to_string()))
.collect());
// 8 = "for<> : ".len()
let used_width = lifetime_str.len() + type_str.len() + 8;
let budget = try_opt!(width.checked_sub(used_width));
let bounds_str = try_opt!(bounds.iter()
let bounds_str: String = try_opt!(bounds.iter()
.map(|ty_bound| {
ty_bound.rewrite(context,
budget,
offset + used_width)
})
.collect::<Option<Vec<_>>>())
.join(" + ");
.intersperse(Some(" + ".to_string()))
.collect());
format!("for<{}> {}: {}", lifetime_str, type_str, bounds_str)
} else {
// 2 = ": ".len()
let used_width = type_str.len() + 2;
let budget = try_opt!(width.checked_sub(used_width));
let bounds_str = try_opt!(bounds.iter()
let bounds_str: String = try_opt!(bounds.iter()
.map(|ty_bound| {
ty_bound.rewrite(context,
budget,
offset + used_width)
})
.collect::<Option<Vec<_>>>())
.join(" + ");
.intersperse(Some(" + ".to_string()))
.collect());
format!("{}: {}", type_str, bounds_str)
}
@ -449,11 +451,11 @@ impl Rewrite for ast::TyParam {
if !self.bounds.is_empty() {
result.push_str(": ");
let bounds = try_opt!(self.bounds
.iter()
.map(|ty_bound| ty_bound.rewrite(context, width, offset))
.collect::<Option<Vec<_>>>())
.join(" + ");
let bounds: String = try_opt!(self.bounds
.iter()
.map(|ty_bound| ty_bound.rewrite(context, width, offset))
.intersperse(Some(" + ".to_string()))
.collect());
result.push_str(&bounds);
}
@ -476,11 +478,12 @@ impl Rewrite for ast::TyParam {
impl Rewrite for ast::PolyTraitRef {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
if !self.bound_lifetimes.is_empty() {
let lifetime_str = try_opt!(self.bound_lifetimes
.iter()
.map(|lt| lt.rewrite(context, width, offset))
.collect::<Option<Vec<_>>>())
.join(", ");
let lifetime_str: String = try_opt!(self.bound_lifetimes
.iter()
.map(|lt| lt.rewrite(context, width, offset))
.intersperse(Some(", ".to_string()))
.collect());
// 6 is "for<> ".len()
let extra_offset = lifetime_str.len() + 6;
let max_path_width = try_opt!(width.checked_sub(extra_offset));
@ -604,10 +607,10 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
// This doesn't work out so nicely for mutliline situation with lots of
// rightward drift. If that is a problem, we could use the list stuff.
result.push_str(&try_opt!(bare_fn.lifetimes
.iter()
.map(|l| l.rewrite(context, try_opt!(width.checked_sub(6)), offset + 4))
.collect::<Option<Vec<_>>>())
.join(", "));
.iter()
.map(|l| l.rewrite(context, try_opt!(width.checked_sub(6)), offset + 4))
.intersperse(Some(", ".to_string()))
.collect::<Option<String>>()));
result.push_str("> ");
}