From 755d27a4241d9b2a6fa90118d9d40ae729fe5c68 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sat, 29 Sep 2018 14:33:00 +0900 Subject: [PATCH] Take impl Iterator for overflow routines --- src/expr.rs | 44 +++++++++++++++++++++++++++++--------------- src/items.rs | 5 ++--- src/macros.rs | 6 ++---- src/overflow.rs | 40 ++++++++++++++++++++-------------------- src/patterns.rs | 3 +-- src/rewrite.rs | 7 +++++++ src/spanned.rs | 9 +++++++-- src/types.rs | 11 ++++------- 8 files changed, 72 insertions(+), 53 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( - name: &str, - exprs: &[&T], +pub fn rewrite_array<'a, T: 'a>( + name: &'a str, + exprs: impl Iterator, span: Span, - context: &RewriteContext, + context: &'a RewriteContext, shape: Shape, force_separator_tactic: Option, delim_token: Option, -) -> Option { +) -> Option +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, span: Span, shape: Shape, + is_singleton_tuple: bool, ) -> Option 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, span: Span, shape: Shape, + is_singleton_tuple: bool, ) -> Option 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 ToExpr for ptr::P { + 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, diff --git a/src/items.rs b/src/items.rs index 17a9e529692..3f8fb852497 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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::>(); 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::>(); + let params = generics.params.iter(); overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span) } diff --git a/src/macros.rs b/src/macros.rs index 282519286fa..886e929c43f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -278,7 +278,7 @@ pub fn rewrite_macro_inner( overflow::rewrite_with_parens( context, ¯o_name, - &arg_vec.iter().map(|e| &*e).collect::>(), + 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::>(); let rewrite = rewrite_array( macro_name, - arg_vec, + arg_vec.iter(), mac.span, context, shape, diff --git a/src/overflow.rs b/src/overflow.rs index acf6d7b4d38..5c50e3b3a37 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -29,10 +29,10 @@ use std::cmp::min; const SHORT_ITEM_THRESHOLD: usize = 10; -pub fn rewrite_with_parens( - context: &RewriteContext, - ident: &str, - items: &[&T], +pub fn rewrite_with_parens<'a, 'b, T: 'a>( + context: &'a RewriteContext, + ident: &'a str, + items: impl Iterator, shape: Shape, span: Span, item_max_width: usize, @@ -56,10 +56,10 @@ where .rewrite(shape) } -pub fn rewrite_with_angle_brackets( - context: &RewriteContext, - ident: &str, - items: &[&T], +pub fn rewrite_with_angle_brackets<'a, T: 'a>( + context: &'a RewriteContext, + ident: &'a str, + items: impl Iterator, shape: Shape, span: Span, ) -> Option @@ -81,10 +81,10 @@ where .rewrite(shape) } -pub fn rewrite_with_square_brackets( - context: &RewriteContext, - name: &str, - items: &[&T], +pub fn rewrite_with_square_brackets<'a, T: 'a>( + context: &'a RewriteContext, + name: &'a str, + items: impl Iterator, shape: Shape, span: Span, force_separator_tactic: Option, @@ -115,7 +115,7 @@ where 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, ident: &'a str, shape: Shape, span: Span, @@ -153,7 +153,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { 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 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { 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 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { 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 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { } } let result = last_item_shape( - self.items, + &self.items, list_items, self.one_line_shape, self.item_max_width, @@ -316,7 +316,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { 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 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> { 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; } } diff --git a/src/patterns.rs b/src/patterns.rs index 13fea046aaf..8277a768e0e 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -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::>(); overflow::rewrite_with_parens( &context, &path_str, - &pat_ref_vec, + pat_vec.iter(), shape, span, context.config.max_width(), diff --git a/src/rewrite.rs b/src/rewrite.rs index f463732c0e4..d151c922e81 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -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; } +impl Rewrite for ptr::P { + fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + (**self).rewrite(context, shape) + } +} + #[derive(Clone)] pub struct RewriteContext<'a> { pub parse_session: &'a ParseSess, diff --git a/src/spanned.rs b/src/spanned.rs index a7ba0f1a72d..504d18fb7fd 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -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 Spanned for ptr::P { + 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); diff --git a/src/types.rs b/src/types.rs index 5f71dd6bdc4..8ab89848407 100644 --- a/src/types.rs +++ b/src/types.rs @@ -252,7 +252,7 @@ fn rewrite_segment( let generics_str = overflow::rewrite_with_angle_brackets( context, "", - ¶m_list.iter().map(|e| &*e).collect::>(), + param_list.iter(), shape, mk_sp(*span_lo, span_hi), )?; @@ -664,12 +664,9 @@ impl Rewrite for ast::Ty { 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) }