Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 755d27a

Browse files
committed
Take impl Iterator for overflow routines
1 parent 4c1b0c2 commit 755d27a

File tree

8 files changed

+72
-53
lines changed

8 files changed

+72
-53
lines changed

src/expr.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn format_expr(
7373
let expr_rw = match expr.node {
7474
ast::ExprKind::Array(ref expr_vec) => rewrite_array(
7575
"",
76-
&ptr_vec_to_ref_vec(expr_vec),
76+
expr_vec.iter(),
7777
expr.span,
7878
context,
7979
shape,
@@ -110,7 +110,7 @@ pub fn format_expr(
110110
shape,
111111
),
112112
ast::ExprKind::Tup(ref items) => {
113-
rewrite_tuple(context, &ptr_vec_to_ref_vec(items), expr.span, shape)
113+
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
114114
}
115115
ast::ExprKind::If(..)
116116
| ast::ExprKind::IfLet(..)
@@ -391,15 +391,18 @@ pub fn format_expr(
391391
})
392392
}
393393

394-
pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
395-
name: &str,
396-
exprs: &[&T],
394+
pub fn rewrite_array<'a, T: 'a>(
395+
name: &'a str,
396+
exprs: impl Iterator<Item = &'a T>,
397397
span: Span,
398-
context: &RewriteContext,
398+
context: &'a RewriteContext,
399399
shape: Shape,
400400
force_separator_tactic: Option<SeparatorTactic>,
401401
delim_token: Option<DelimToken>,
402-
) -> Option<String> {
402+
) -> Option<String>
403+
where
404+
T: Rewrite + Spanned + ToExpr,
405+
{
403406
overflow::rewrite_with_square_brackets(
404407
context,
405408
name,
@@ -1329,7 +1332,7 @@ pub fn rewrite_call(
13291332
overflow::rewrite_with_parens(
13301333
context,
13311334
callee,
1332-
&ptr_vec_to_ref_vec(args),
1335+
args.iter(),
13331336
shape,
13341337
span,
13351338
context.config.width_heuristics().fn_call_width,
@@ -1722,17 +1725,17 @@ pub fn rewrite_field(
17221725

17231726
fn rewrite_tuple_in_visual_indent_style<'a, T>(
17241727
context: &RewriteContext,
1725-
items: &[&T],
1728+
mut items: impl Iterator<Item = &'a T>,
17261729
span: Span,
17271730
shape: Shape,
1731+
is_singleton_tuple: bool,
17281732
) -> Option<String>
17291733
where
17301734
T: Rewrite + Spanned + ToExpr + 'a,
17311735
{
1732-
let mut items = items.iter();
17331736
// In case of length 1, need a trailing comma
17341737
debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
1735-
if items.len() == 1 {
1738+
if is_singleton_tuple {
17361739
// 3 = "(" + ",)"
17371740
let nested_shape = shape.sub_width(3)?.visual_indent(1);
17381741
return items
@@ -1772,10 +1775,11 @@ where
17721775
}
17731776

17741777
pub fn rewrite_tuple<'a, T>(
1775-
context: &RewriteContext,
1776-
items: &[&T],
1778+
context: &'a RewriteContext,
1779+
items: impl Iterator<Item = &'a T>,
17771780
span: Span,
17781781
shape: Shape,
1782+
is_singleton_tuple: bool,
17791783
) -> Option<String>
17801784
where
17811785
T: Rewrite + Spanned + ToExpr + 'a,
@@ -1789,7 +1793,7 @@ where
17891793
} else {
17901794
Some(SeparatorTactic::Never)
17911795
}
1792-
} else if items.len() == 1 {
1796+
} else if is_singleton_tuple {
17931797
Some(SeparatorTactic::Always)
17941798
} else {
17951799
None
@@ -1804,7 +1808,7 @@ where
18041808
force_tactic,
18051809
)
18061810
} else {
1807-
rewrite_tuple_in_visual_indent_style(context, items, span, shape)
1811+
rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple)
18081812
}
18091813
}
18101814

@@ -2068,6 +2072,16 @@ impl ToExpr for ast::GenericParam {
20682072
}
20692073
}
20702074

2075+
impl<T: ToExpr> ToExpr for ptr::P<T> {
2076+
fn to_expr(&self) -> Option<&ast::Expr> {
2077+
(**self).to_expr()
2078+
}
2079+
2080+
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
2081+
(**self).can_be_overflowed(context, len)
2082+
}
2083+
}
2084+
20712085
pub fn is_method_call(expr: &ast::Expr) -> bool {
20722086
match expr.node {
20732087
ast::ExprKind::MethodCall(..) => true,

src/items.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,10 @@ fn format_tuple_struct(
13911391
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
13921392
} else {
13931393
let shape = Shape::indented(offset, context.config).sub_width(1)?;
1394-
let fields = &fields.iter().collect::<Vec<_>>();
13951394
result = overflow::rewrite_with_parens(
13961395
context,
13971396
&result,
1398-
fields,
1397+
fields.iter(),
13991398
shape,
14001399
span,
14011400
context.config.width_heuristics().fn_call_width,
@@ -2495,7 +2494,7 @@ fn rewrite_generics(
24952494
return Some(ident.to_owned());
24962495
}
24972496

2498-
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
2497+
let params = generics.params.iter();
24992498
overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
25002499
}
25012500

src/macros.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn rewrite_macro_inner(
278278
overflow::rewrite_with_parens(
279279
context,
280280
&macro_name,
281-
&arg_vec.iter().map(|e| &*e).collect::<Vec<_>>(),
281+
arg_vec.iter(),
282282
shape,
283283
mac.span,
284284
context.config.width_heuristics().fn_call_width,
@@ -334,11 +334,9 @@ pub fn rewrite_macro_inner(
334334
force_trailing_comma = Some(SeparatorTactic::Vertical);
335335
};
336336
}
337-
// Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
338-
let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>();
339337
let rewrite = rewrite_array(
340338
macro_name,
341-
arg_vec,
339+
arg_vec.iter(),
342340
mac.span,
343341
context,
344342
shape,

src/overflow.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ use std::cmp::min;
2929

3030
const SHORT_ITEM_THRESHOLD: usize = 10;
3131

32-
pub fn rewrite_with_parens<T>(
33-
context: &RewriteContext,
34-
ident: &str,
35-
items: &[&T],
32+
pub fn rewrite_with_parens<'a, 'b, T: 'a>(
33+
context: &'a RewriteContext,
34+
ident: &'a str,
35+
items: impl Iterator<Item = &'a T>,
3636
shape: Shape,
3737
span: Span,
3838
item_max_width: usize,
@@ -56,10 +56,10 @@ where
5656
.rewrite(shape)
5757
}
5858

59-
pub fn rewrite_with_angle_brackets<T>(
60-
context: &RewriteContext,
61-
ident: &str,
62-
items: &[&T],
59+
pub fn rewrite_with_angle_brackets<'a, T: 'a>(
60+
context: &'a RewriteContext,
61+
ident: &'a str,
62+
items: impl Iterator<Item = &'a T>,
6363
shape: Shape,
6464
span: Span,
6565
) -> Option<String>
@@ -81,10 +81,10 @@ where
8181
.rewrite(shape)
8282
}
8383

84-
pub fn rewrite_with_square_brackets<T>(
85-
context: &RewriteContext,
86-
name: &str,
87-
items: &[&T],
84+
pub fn rewrite_with_square_brackets<'a, T: 'a>(
85+
context: &'a RewriteContext,
86+
name: &'a str,
87+
items: impl Iterator<Item = &'a T>,
8888
shape: Shape,
8989
span: Span,
9090
force_separator_tactic: Option<SeparatorTactic>,
@@ -115,7 +115,7 @@ where
115115

116116
struct Context<'a, T: 'a> {
117117
context: &'a RewriteContext<'a>,
118-
items: &'a [&'a T],
118+
items: Vec<&'a T>,
119119
ident: &'a str,
120120
prefix: &'static str,
121121
suffix: &'static str,
@@ -131,7 +131,7 @@ struct Context<'a, T: 'a> {
131131
impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
132132
pub fn new(
133133
context: &'a RewriteContext,
134-
items: &'a [&'a T],
134+
items: impl Iterator<Item = &'a T>,
135135
ident: &'a str,
136136
shape: Shape,
137137
span: Span,
@@ -153,7 +153,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
153153
let nested_shape = shape_from_indent_style(context, shape, used_width + 2, used_width + 1);
154154
Context {
155155
context,
156-
items,
156+
items: items.collect(),
157157
ident,
158158
one_line_shape,
159159
nested_shape,
@@ -192,7 +192,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
192192
ast::ExprKind::Closure(..) => {
193193
// If the argument consists of multiple closures, we do not overflow
194194
// the last closure.
195-
if closures::args_have_many_closure(self.items) {
195+
if closures::args_have_many_closure(&self.items) {
196196
None
197197
} else {
198198
closures::rewrite_last_closure(self.context, expr, shape)
@@ -227,7 +227,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
227227
let combine_arg_with_callee = self.items.len() == 1
228228
&& self.items[0].to_expr().is_some()
229229
&& self.ident.len() < self.context.config.tab_spaces();
230-
let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, self.items);
230+
let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
231231

232232
// Replace the last item with its first line to see if it fits with
233233
// first arguments.
@@ -241,7 +241,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
241241
}
242242
}
243243
let result = last_item_shape(
244-
self.items,
244+
&self.items,
245245
list_items,
246246
self.one_line_shape,
247247
self.item_max_width,
@@ -316,7 +316,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
316316

317317
if tactic == DefinitiveListTactic::Vertical {
318318
if let Some((all_simple, num_args_before)) =
319-
maybe_get_args_offset(self.ident, self.items)
319+
maybe_get_args_offset(self.ident, &self.items)
320320
{
321321
let one_line = all_simple
322322
&& definitive_tactic(
@@ -335,7 +335,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
335335
if one_line {
336336
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
337337
};
338-
} else if is_every_expr_simple(self.items) && no_long_items(list_items) {
338+
} else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
339339
tactic = DefinitiveListTactic::Mixed;
340340
}
341341
}

src/patterns.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,11 @@ fn rewrite_tuple_pat(
359359
// add comma if `(x,)`
360360
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none() && !condensed;
361361
let path_str = path_str.unwrap_or_default();
362-
let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
363362

364363
overflow::rewrite_with_parens(
365364
&context,
366365
&path_str,
367-
&pat_ref_vec,
366+
pat_vec.iter(),
368367
shape,
369368
span,
370369
context.config.max_width(),

src/rewrite.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// A generic trait to abstract the rewriting of an element (of the AST).
1212

1313
use syntax::parse::ParseSess;
14+
use syntax::ptr;
1415
use syntax::source_map::{SourceMap, Span};
1516

1617
use config::{Config, IndentStyle};
@@ -25,6 +26,12 @@ pub trait Rewrite {
2526
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String>;
2627
}
2728

29+
impl<T: Rewrite> Rewrite for ptr::P<T> {
30+
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
31+
(**self).rewrite(context, shape)
32+
}
33+
}
34+
2835
#[derive(Clone)]
2936
pub struct RewriteContext<'a> {
3037
pub parse_session: &'a ParseSess,

src/spanned.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast;
12-
use syntax::source_map::Span;
11+
use syntax::{ast, ptr, source_map::Span};
1312

1413
use macros::MacroArg;
1514
use utils::{mk_sp, outer_attributes};
@@ -21,6 +20,12 @@ pub trait Spanned {
2120
fn span(&self) -> Span;
2221
}
2322

23+
impl<T: Spanned> Spanned for ptr::P<T> {
24+
fn span(&self) -> Span {
25+
(**self).span()
26+
}
27+
}
28+
2429
macro_rules! span_with_attrs_lo_hi {
2530
($this:ident, $lo:expr, $hi:expr) => {{
2631
let attrs = outer_attributes(&$this.attrs);

src/types.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn rewrite_segment(
252252
let generics_str = overflow::rewrite_with_angle_brackets(
253253
context,
254254
"",
255-
&param_list.iter().map(|e| &*e).collect::<Vec<_>>(),
255+
param_list.iter(),
256256
shape,
257257
mk_sp(*span_lo, span_hi),
258258
)?;
@@ -664,12 +664,9 @@ impl Rewrite for ast::Ty {
664664
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
665665
.map(|ty_str| format!("[{}]", ty_str))
666666
}
667-
ast::TyKind::Tup(ref items) => rewrite_tuple(
668-
context,
669-
&::utils::ptr_vec_to_ref_vec(items),
670-
self.span,
671-
shape,
672-
),
667+
ast::TyKind::Tup(ref items) => {
668+
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
669+
}
673670
ast::TyKind::Path(ref q_self, ref path) => {
674671
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
675672
}

0 commit comments

Comments
 (0)