Take impl Iterator for overflow routines

This commit is contained in:
Seiichi Uchida 2018-09-29 14:33:00 +09:00
parent 4c1b0c2241
commit 755d27a424
8 changed files with 72 additions and 53 deletions

View 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 @@ 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,

View 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)
}

View 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,

View File

@ -29,10 +29,10 @@ use std::cmp::min;
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 @@ where
.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 @@ where
.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 @@ 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<Item = &'a T>,
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;
}
}

View 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(),

View 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,

View 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);

View 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 @@ 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)
}