// except according to those terms.
use std::cmp::{Ordering, min};
-use std::ops::Deref;
use std::iter::ExactSizeIterator;
use std::fmt::Write;
rewrite_call_inner(context, &callee, args, span, shape, false).ok()
}
-fn rewrite_call_inner(
+fn rewrite_call_inner<'a, T>(
context: &RewriteContext,
callee_str: &str,
- args: &[ptr::P<ast::Expr>],
+ args: &[ptr::P<T>],
span: Span,
shape: Shape,
force_trailing_comma: bool,
-) -> Result<String, Ordering> {
+) -> Result<String, Ordering>
+where
+ T: Rewrite + Spanned + ToExpr + 'a,
+{
// 2 = `( `, 1 = `(`
let paren_overhead = if context.config.spaces_within_parens() {
2
})
}
-fn rewrite_call_args(
+fn rewrite_call_args<'a, T>(
context: &RewriteContext,
- args: &[ptr::P<ast::Expr>],
+ args: &[ptr::P<T>],
span: Span,
shape: Shape,
one_line_width: usize,
force_trailing_comma: bool,
-) -> Option<(bool, String)> {
+) -> Option<(bool, String)>
+where
+ T: Rewrite + Spanned + ToExpr + 'a,
+{
let mut item_context = context.clone();
item_context.inside_macro = false;
let items = itemize_list(
context.codemap,
args.iter(),
")",
- |item| item.span.lo,
- |item| item.span.hi,
+ |item| item.span().lo,
+ |item| item.span().hi,
|item| item.rewrite(&item_context, shape),
span.lo,
span.hi,
// Try letting the last argument overflow to the next line with block
// indentation. If its first line fits on one line with the other arguments,
// we format the function arguments horizontally.
- let tactic = try_overflow_last_arg(&item_context, &mut item_vec, args, shape, one_line_width);
+ let args = args.iter().filter_map(|e| e.to_expr()).collect::<Vec<_>>();
+ let tactic = try_overflow_last_arg(
+ &item_context,
+ &mut item_vec,
+ &args[..],
+ shape,
+ one_line_width,
+ );
let fmt = ListFormatting {
tactic: tactic,
fn try_overflow_last_arg(
context: &RewriteContext,
item_vec: &mut Vec<ListItem>,
- args: &[ptr::P<ast::Expr>],
+ args: &[&ast::Expr],
shape: Shape,
one_line_width: usize,
) -> DefinitiveListTactic {
last_arg_shape(&context, &item_vec, shape).map_or((None, None), |arg_shape| {
rewrite_last_arg_with_overflow(
&context,
- &args[args.len() - 1],
+ args[args.len() - 1],
&mut item_vec[args.len() - 1],
arg_shape,
)
fn rewrite_last_arg_with_overflow(
context: &RewriteContext,
- last_arg: &ptr::P<ast::Expr>,
+ last_arg: &ast::Expr,
last_item: &mut ListItem,
shape: Shape,
) -> (Option<String>, Option<String>) {
}
}
-fn can_be_overflowed(context: &RewriteContext, args: &[ptr::P<ast::Expr>]) -> bool {
+fn can_be_overflowed(context: &RewriteContext, args: &[&ast::Expr]) -> bool {
args.last().map_or(false, |x| {
can_be_overflowed_expr(context, &x, args.len())
})
}
}
-pub fn rewrite_tuple_type<'a, I>(
+fn rewrite_tuple_in_visual_indent_style<'a, T>(
context: &RewriteContext,
- mut items: I,
+ items: &[ptr::P<T>],
span: Span,
shape: Shape,
) -> Option<String>
where
- I: ExactSizeIterator,
- <I as Iterator>::Item: Deref,
- <I::Item as Deref>::Target: Rewrite + Spanned + 'a,
+ T: Rewrite + Spanned + ToExpr + 'a,
{
+ let mut items = items.iter();
// In case of length 1, need a trailing comma
- debug!("rewrite_tuple_type {:?}", shape);
+ debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
if items.len() == 1 {
// 3 = "(" + ",)"
let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1);
}
}
-pub fn rewrite_tuple(
+pub fn rewrite_tuple<'a, T>(
context: &RewriteContext,
- items: &[ptr::P<ast::Expr>],
+ items: &[ptr::P<T>],
span: Span,
shape: Shape,
-) -> Option<String> {
+) -> Option<String>
+where
+ T: Rewrite + Spanned + ToExpr + 'a,
+{
debug!("rewrite_tuple {:?}", shape);
- // Use old `rewrite_tuple`
- if context.config.fn_call_style() == IndentStyle::Visual {
- return rewrite_tuple_type(context, items.iter().map(|x| &**x), span, shape);
+ if context.use_block_indent() {
+ // We use the same rule as funcation call for rewriting tuple.
+ rewrite_call_inner(
+ context,
+ &String::new(),
+ items,
+ span,
+ shape,
+ items.len() == 1,
+ ).ok()
+ } else {
+ rewrite_tuple_in_visual_indent_style(context, items, span, shape)
}
-
- // We use the same rule as funcation call for rewriting tuple.
- // 1 = ","
- rewrite_call_inner(
- context,
- &String::new(),
- items,
- span,
- shape,
- items.len() == 1,
- ).ok()
}
pub fn rewrite_unary_prefix<R: Rewrite>(
};
rewrite_unary_prefix(context, operator_str, expr, shape)
}
+
+pub trait ToExpr {
+ fn to_expr(&self) -> Option<&ast::Expr>;
+}
+
+impl ToExpr for ast::Expr {
+ fn to_expr(&self) -> Option<&ast::Expr> {
+ Some(self)
+ }
+}
+
+impl ToExpr for ast::Ty {
+ fn to_expr(&self) -> Option<&ast::Expr> {
+ None
+ }
+}
use lists::{itemize_list, format_fn_args};
use rewrite::{Rewrite, RewriteContext};
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str, mk_sp, last_line_width};
-use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type};
+use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use config::{Style, TypeDensity};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
format!("[{}]", ty_str)
})
}
- ast::TyKind::Tup(ref items) => {
- rewrite_tuple_type(context, items.iter().map(|x| &**x), self.span, shape)
- }
+ ast::TyKind::Tup(ref items) => rewrite_tuple(context, items, self.span, shape),
ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
}