]> git.lizzy.rs Git - rust.git/commitdiff
Take impl Iterator for overflow routines
authorSeiichi Uchida <seuchida@gmail.com>
Sat, 29 Sep 2018 05:33:00 +0000 (14:33 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Sat, 29 Sep 2018 05:33:00 +0000 (14:33 +0900)
src/expr.rs
src/items.rs
src/macros.rs
src/overflow.rs
src/patterns.rs
src/rewrite.rs
src/spanned.rs
src/types.rs

index 674f719b5c74ba8957aece1eb818d6805fb73f29..f104a40bf872c6d3c5f50189f6d408cae64d9659 100644 (file)
@@ -73,7 +73,7 @@ pub fn format_expr(
     let expr_rw = match expr.node {
         ast::ExprKind::Array(ref expr_vec) => rewrite_array(
             "",
-            &ptr_vec_to_ref_vec(expr_vec),
+            expr_vec.iter(),
             expr.span,
             context,
             shape,
@@ -110,7 +110,7 @@ pub fn format_expr(
             shape,
         ),
         ast::ExprKind::Tup(ref items) => {
-            rewrite_tuple(context, &ptr_vec_to_ref_vec(items), expr.span, shape)
+            rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
         }
         ast::ExprKind::If(..)
         | ast::ExprKind::IfLet(..)
@@ -391,15 +391,18 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
         })
 }
 
-pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
-    name: &str,
-    exprs: &[&T],
+pub fn rewrite_array<'a, T: 'a>(
+    name: &'a str,
+    exprs: impl Iterator<Item = &'a T>,
     span: Span,
-    context: &RewriteContext,
+    context: &'a RewriteContext,
     shape: Shape,
     force_separator_tactic: Option<SeparatorTactic>,
     delim_token: Option<DelimToken>,
-) -> Option<String> {
+) -> Option<String>
+where
+    T: Rewrite + Spanned + ToExpr,
+{
     overflow::rewrite_with_square_brackets(
         context,
         name,
@@ -1329,7 +1332,7 @@ pub fn rewrite_call(
     overflow::rewrite_with_parens(
         context,
         callee,
-        &ptr_vec_to_ref_vec(args),
+        args.iter(),
         shape,
         span,
         context.config.width_heuristics().fn_call_width,
@@ -1722,17 +1725,17 @@ pub fn rewrite_field(
 
 fn rewrite_tuple_in_visual_indent_style<'a, T>(
     context: &RewriteContext,
-    items: &[&T],
+    mut items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
+    is_singleton_tuple: bool,
 ) -> Option<String>
 where
     T: Rewrite + Spanned + ToExpr + 'a,
 {
-    let mut items = items.iter();
     // In case of length 1, need a trailing comma
     debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
-    if items.len() == 1 {
+    if is_singleton_tuple {
         // 3 = "(" + ",)"
         let nested_shape = shape.sub_width(3)?.visual_indent(1);
         return items
@@ -1772,10 +1775,11 @@ fn rewrite_tuple_in_visual_indent_style<'a, T>(
 }
 
 pub fn rewrite_tuple<'a, T>(
-    context: &RewriteContext,
-    items: &[&T],
+    context: &'a RewriteContext,
+    items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
+    is_singleton_tuple: bool,
 ) -> Option<String>
 where
     T: Rewrite + Spanned + ToExpr + 'a,
@@ -1789,7 +1793,7 @@ pub fn rewrite_tuple<'a, T>(
             } else {
                 Some(SeparatorTactic::Never)
             }
-        } else if items.len() == 1 {
+        } else if is_singleton_tuple {
             Some(SeparatorTactic::Always)
         } else {
             None
@@ -1804,7 +1808,7 @@ pub fn rewrite_tuple<'a, T>(
             force_tactic,
         )
     } else {
-        rewrite_tuple_in_visual_indent_style(context, items, span, shape)
+        rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple)
     }
 }
 
@@ -2068,6 +2072,16 @@ fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
     }
 }
 
+impl<T: ToExpr> ToExpr for ptr::P<T> {
+    fn to_expr(&self) -> Option<&ast::Expr> {
+        (**self).to_expr()
+    }
+
+    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
+        (**self).can_be_overflowed(context, len)
+    }
+}
+
 pub fn is_method_call(expr: &ast::Expr) -> bool {
     match expr.node {
         ast::ExprKind::MethodCall(..) => true,
index 17a9e529692196099368013c890e87e74c21edae..3f8fb852497624c49b6eccba4a78689e45fef3b2 100644 (file)
@@ -1391,11 +1391,10 @@ fn format_tuple_struct(
         format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
     } else {
         let shape = Shape::indented(offset, context.config).sub_width(1)?;
-        let fields = &fields.iter().collect::<Vec<_>>();
         result = overflow::rewrite_with_parens(
             context,
             &result,
-            fields,
+            fields.iter(),
             shape,
             span,
             context.config.width_heuristics().fn_call_width,
@@ -2495,7 +2494,7 @@ fn rewrite_generics(
         return Some(ident.to_owned());
     }
 
-    let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
+    let params = generics.params.iter();
     overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
 }
 
index 282519286faa8756988546c2ba8864e69f76773b..886e929c43f2c17f9b5c3f40c4ebb210dc14fd0e 100644 (file)
@@ -278,7 +278,7 @@ pub fn rewrite_macro_inner(
             overflow::rewrite_with_parens(
                 context,
                 &macro_name,
-                &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>(),
+                arg_vec.iter(),
                 shape,
                 mac.span,
                 context.config.width_heuristics().fn_call_width,
@@ -334,11 +334,9 @@ pub fn rewrite_macro_inner(
                         force_trailing_comma = Some(SeparatorTactic::Vertical);
                     };
                 }
-                // Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
-                let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>();
                 let rewrite = rewrite_array(
                     macro_name,
-                    arg_vec,
+                    arg_vec.iter(),
                     mac.span,
                     context,
                     shape,
index acf6d7b4d387eab696463c142cef736f0e1f22e6..5c50e3b3a37ca742328453702236cff9fbe31ad1 100644 (file)
 
 const SHORT_ITEM_THRESHOLD: usize = 10;
 
-pub fn rewrite_with_parens<T>(
-    context: &RewriteContext,
-    ident: &str,
-    items: &[&T],
+pub fn rewrite_with_parens<'a, 'b, T: 'a>(
+    context: &'a RewriteContext,
+    ident: &'a str,
+    items: impl Iterator<Item = &'a T>,
     shape: Shape,
     span: Span,
     item_max_width: usize,
@@ -56,10 +56,10 @@ pub fn rewrite_with_parens<T>(
     .rewrite(shape)
 }
 
-pub fn rewrite_with_angle_brackets<T>(
-    context: &RewriteContext,
-    ident: &str,
-    items: &[&T],
+pub fn rewrite_with_angle_brackets<'a, T: 'a>(
+    context: &'a RewriteContext,
+    ident: &'a str,
+    items: impl Iterator<Item = &'a T>,
     shape: Shape,
     span: Span,
 ) -> Option<String>
@@ -81,10 +81,10 @@ pub fn rewrite_with_angle_brackets<T>(
     .rewrite(shape)
 }
 
-pub fn rewrite_with_square_brackets<T>(
-    context: &RewriteContext,
-    name: &str,
-    items: &[&T],
+pub fn rewrite_with_square_brackets<'a, T: 'a>(
+    context: &'a RewriteContext,
+    name: &'a str,
+    items: impl Iterator<Item = &'a T>,
     shape: Shape,
     span: Span,
     force_separator_tactic: Option<SeparatorTactic>,
@@ -115,7 +115,7 @@ pub fn rewrite_with_square_brackets<T>(
 
 struct Context<'a, T: 'a> {
     context: &'a RewriteContext<'a>,
-    items: &'a [&'a T],
+    items: Vec<&'a T>,
     ident: &'a str,
     prefix: &'static str,
     suffix: &'static str,
@@ -131,7 +131,7 @@ struct Context<'a, T: 'a> {
 impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
     pub fn new(
         context: &'a RewriteContext,
-        items: &'a [&'a T],
+        items: impl Iterator<Item = &'a T>,
         ident: &'a str,
         shape: Shape,
         span: Span,
@@ -153,7 +153,7 @@ pub fn new(
         let nested_shape = shape_from_indent_style(context, shape, used_width + 2, used_width + 1);
         Context {
             context,
-            items,
+            items: items.collect(),
             ident,
             one_line_shape,
             nested_shape,
@@ -192,7 +192,7 @@ fn rewrite_last_item_with_overflow(
                 ast::ExprKind::Closure(..) => {
                     // If the argument consists of multiple closures, we do not overflow
                     // the last closure.
-                    if closures::args_have_many_closure(self.items) {
+                    if closures::args_have_many_closure(&self.items) {
                         None
                     } else {
                         closures::rewrite_last_closure(self.context, expr, shape)
@@ -227,7 +227,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
         let combine_arg_with_callee = self.items.len() == 1
             && self.items[0].to_expr().is_some()
             && self.ident.len() < self.context.config.tab_spaces();
-        let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, self.items);
+        let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
 
         // Replace the last item with its first line to see if it fits with
         // first arguments.
@@ -241,7 +241,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
                 }
             }
             let result = last_item_shape(
-                self.items,
+                &self.items,
                 list_items,
                 self.one_line_shape,
                 self.item_max_width,
@@ -316,7 +316,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
 
                     if tactic == DefinitiveListTactic::Vertical {
                         if let Some((all_simple, num_args_before)) =
-                            maybe_get_args_offset(self.ident, self.items)
+                            maybe_get_args_offset(self.ident, &self.items)
                         {
                             let one_line = all_simple
                                 && definitive_tactic(
@@ -335,7 +335,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
                             if one_line {
                                 tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
                             };
-                        } else if is_every_expr_simple(self.items) && no_long_items(list_items) {
+                        } else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
                             tactic = DefinitiveListTactic::Mixed;
                         }
                     }
index 13fea046aafaf4c5680de2dd527800fa62a61791..8277a768e0ee99728a93c5ebca890d684b6b8d59 100644 (file)
@@ -359,12 +359,11 @@ fn rewrite_tuple_pat(
     // add comma if `(x,)`
     let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none() && !condensed;
     let path_str = path_str.unwrap_or_default();
-    let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
 
     overflow::rewrite_with_parens(
         &context,
         &path_str,
-        &pat_ref_vec,
+        pat_vec.iter(),
         shape,
         span,
         context.config.max_width(),
index f463732c0e497dbccd4fa9f4482fa592518c4b46..d151c922e812a18512ef48c1bb885dc72d9c521a 100644 (file)
@@ -11,6 +11,7 @@
 // A generic trait to abstract the rewriting of an element (of the AST).
 
 use syntax::parse::ParseSess;
+use syntax::ptr;
 use syntax::source_map::{SourceMap, Span};
 
 use config::{Config, IndentStyle};
@@ -25,6 +26,12 @@ pub trait Rewrite {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String>;
 }
 
+impl<T: Rewrite> Rewrite for ptr::P<T> {
+    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+        (**self).rewrite(context, shape)
+    }
+}
+
 #[derive(Clone)]
 pub struct RewriteContext<'a> {
     pub parse_session: &'a ParseSess,
index a7ba0f1a72dfbc24dec9344f580fc96321500f57..504d18fb7fd1752729af5b226100a1a825af3844 100644 (file)
@@ -8,8 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use syntax::ast;
-use syntax::source_map::Span;
+use syntax::{ast, ptr, source_map::Span};
 
 use macros::MacroArg;
 use utils::{mk_sp, outer_attributes};
@@ -21,6 +20,12 @@ pub trait Spanned {
     fn span(&self) -> Span;
 }
 
+impl<T: Spanned> Spanned for ptr::P<T> {
+    fn span(&self) -> Span {
+        (**self).span()
+    }
+}
+
 macro_rules! span_with_attrs_lo_hi {
     ($this:ident, $lo:expr, $hi:expr) => {{
         let attrs = outer_attributes(&$this.attrs);
index 5f71dd6bdc455772a85bf71509fb3e5f8a5905a9..8ab89848407f1e5285d17260344679b433ed08c4 100644 (file)
@@ -252,7 +252,7 @@ fn rewrite_segment(
                 let generics_str = overflow::rewrite_with_angle_brackets(
                     context,
                     "",
-                    &param_list.iter().map(|e| &*e).collect::<Vec<_>>(),
+                    param_list.iter(),
                     shape,
                     mk_sp(*span_lo, span_hi),
                 )?;
@@ -664,12 +664,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
                     .map(|ty_str| format!("[{}]", ty_str))
             }
-            ast::TyKind::Tup(ref items) => rewrite_tuple(
-                context,
-                &::utils::ptr_vec_to_ref_vec(items),
-                self.span,
-                shape,
-            ),
+            ast::TyKind::Tup(ref items) => {
+                rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
+            }
             ast::TyKind::Path(ref q_self, ref path) => {
                 rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
             }