]> git.lizzy.rs Git - rust.git/commitdiff
Refactor string collects to itertools join
authorJacob Clark <jacob.jh.clark@googlemail.com>
Mon, 6 Jun 2016 21:30:40 +0000 (22:30 +0100)
committerJacob Clark <jacob.jh.clark@googlemail.com>
Mon, 6 Jun 2016 21:30:40 +0000 (22:30 +0100)
src/items.rs
src/types.rs

index 7c73d1a6787dd494064058195de627e27ccede24..fd0dd9d6a67cc6ce558d06ace5fe661384d9f13f 100644 (file)
@@ -21,6 +21,7 @@
 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};
@@ -1056,7 +1057,6 @@ pub fn rewrite_associated_type(ident: ast::Ident,
         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(" + ");
         if bounds.len() > 0 {
             format!(": {}", bound_str)
@@ -1702,7 +1702,6 @@ fn rewrite_trait_bounds(context: &RewriteContext,
 
     let bound_str = bounds.iter()
         .filter_map(|ty_bound| ty_bound.rewrite(&context, width, indent))
-        .collect::<Vec<String>>()
         .join(" + ");
 
     let mut result = String::new();
index b1b9b749dd8e19a4cfd5f3c033ab953a0d862877..0c35c82791f59853526ca2f092c31b6f1f636699 100644 (file)
@@ -23,6 +23,7 @@
 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,
@@ -331,8 +332,9 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                                               width,
                                                                               offset)
                                                                })
-                                                               .collect::<Option<Vec<_>>>())
-                                           .join(", ");
+                                                               .intersperse(Some(", ".to_string()))
+                                                               .collect::<Option<String>>());
+
                     // 8 = "for<> : ".len()
                     let used_width = lifetime_str.len() + type_str.len() + 8;
                     let budget = try_opt!(width.checked_sub(used_width));
@@ -342,8 +344,8 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                                          budget,
                                                                          offset + used_width)
                                                     })
-                                                    .collect::<Option<Vec<_>>>())
-                                         .join(" + ");
+                                                    .intersperse(Some(" + ".to_string()))
+                                                    .collect::<Option<String>>());
 
                     format!("for<{}> {}: {}", lifetime_str, type_str, bounds_str)
                 } else {
@@ -356,8 +358,8 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                                          budget,
                                                                          offset + used_width)
                                                     })
-                                                    .collect::<Option<Vec<_>>>())
-                                         .join(" + ");
+                                                    .intersperse(Some(" + ".to_string()))
+                                                    .collect::<Option<String>>());
 
                     format!("{}: {}", type_str, bounds_str)
                 }
@@ -450,10 +452,10 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             result.push_str(": ");
 
             let bounds = try_opt!(self.bounds
-                    .iter()
-                    .map(|ty_bound| ty_bound.rewrite(context, width, offset))
-                    .collect::<Option<Vec<_>>>())
-                .join(" + ");
+                .iter()
+                .map(|ty_bound| ty_bound.rewrite(context, width, offset))
+                .intersperse(Some(" + ".to_string()))
+                .collect::<Option<String>>());
 
             result.push_str(&bounds);
         }
@@ -477,10 +479,11 @@ 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(", ");
+                .iter()
+                .map(|lt| lt.rewrite(context, width, offset))
+                .intersperse(Some(", ".to_string()))
+                .collect::<Option<String>>());
+
             // 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("> ");
     }