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,
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(..)
})
}
-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,
overflow::rewrite_with_parens(
context,
callee,
- &ptr_vec_to_ref_vec(args),
+ args.iter(),
shape,
span,
context.config.width_heuristics().fn_call_width,
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
}
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,
} else {
Some(SeparatorTactic::Never)
}
- } else if items.len() == 1 {
+ } else if is_singleton_tuple {
Some(SeparatorTactic::Always)
} else {
None
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)
}
}
}
}
+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,
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,
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)
}
overflow::rewrite_with_parens(
context,
¯o_name,
- &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>(),
+ arg_vec.iter(),
shape,
mac.span,
context.config.width_heuristics().fn_call_width,
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,
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,
.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>
.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>,
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,
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,
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,
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)
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.
}
}
let result = last_item_shape(
- self.items,
+ &self.items,
list_items,
self.one_line_shape,
self.item_max_width,
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(
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;
}
}
// 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(),
// 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};
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,
// 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};
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);
let generics_str = overflow::rewrite_with_angle_brackets(
context,
"",
- ¶m_list.iter().map(|e| &*e).collect::<Vec<_>>(),
+ param_list.iter(),
shape,
mk_sp(*span_lo, span_hi),
)?;
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)
}