about summary refs log tree commit diff
path: root/src/expr.rs
diff options
context:
space:
mode:
authorSeiichi Uchida <seuchida@gmail.com>2018-09-29 14:33:00 +0900
committerSeiichi Uchida <seuchida@gmail.com>2018-09-29 14:33:00 +0900
commit755d27a4241d9b2a6fa90118d9d40ae729fe5c68 (patch)
treed76185889aab251256b7b6c30e26be057402a32c /src/expr.rs
parent4c1b0c2241bf72b2c66a73b3468e6698964e3f0a (diff)
Take impl Iterator for overflow routines
Diffstat (limited to 'src/expr.rs')
-rw-r--r--src/expr.rs44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 674f719b5c7..f104a40bf87 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -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 @@ pub fn format_expr(
         })
 }
 
-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 @@ where
 }
 
 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 @@ where
             } else {
                 Some(SeparatorTactic::Never)
             }
-        } else if items.len() == 1 {
+        } else if is_singleton_tuple {
             Some(SeparatorTactic::Always)
         } else {
             None
@@ -1804,7 +1808,7 @@ where
             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 @@ impl ToExpr for ast::GenericParam {
     }
 }
 
+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,