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

Commit c302409

Browse files
committed
Get rid of ToExpr trait
1 parent 755d27a commit c302409

File tree

4 files changed

+164
-159
lines changed

4 files changed

+164
-159
lines changed

src/closures.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use syntax::parse::classify;
1313
use syntax::source_map::Span;
1414
use syntax::{ast, ptr};
1515

16-
use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond, ToExpr};
16+
use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
1717
use items::{span_hi_for_arg, span_lo_for_arg};
1818
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
19+
use overflow::OverflowableItem;
1920
use rewrite::{Rewrite, RewriteContext};
2021
use shape::Shape;
2122
use source_map::SpanUtils;
@@ -358,10 +359,7 @@ pub fn rewrite_last_closure(
358359
}
359360

360361
/// Returns true if the given vector of arguments has more than one `ast::ExprKind::Closure`.
361-
pub fn args_have_many_closure<T>(args: &[&T]) -> bool
362-
where
363-
T: ToExpr,
364-
{
362+
pub fn args_have_many_closure(args: &[OverflowableItem]) -> bool {
365363
args.iter()
366364
.filter(|arg| {
367365
arg.to_expr()

src/expr.rs

Lines changed: 12 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ use lists::{
2727
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
2828
struct_lit_tactic, write_list, ListFormatting, ListItem, Separator,
2929
};
30-
use macros::{rewrite_macro, MacroArg, MacroPosition};
30+
use macros::{rewrite_macro, MacroPosition};
3131
use matches::rewrite_match;
32-
use overflow;
32+
use overflow::{self, IntoOverflowableItem, OverflowableItem};
3333
use pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
34-
use patterns::{can_be_overflowed_pat, is_short_pattern, TuplePatField};
34+
use patterns::is_short_pattern;
3535
use rewrite::{Rewrite, RewriteContext};
3636
use shape::{Indent, Shape};
3737
use source_map::{LineRangeUtils, SpanUtils};
3838
use spanned::Spanned;
3939
use string::{rewrite_string, StringFormat};
40-
use types::{can_be_overflowed_type, rewrite_path, PathContext};
40+
use types::{rewrite_path, PathContext};
4141
use utils::{
4242
colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
4343
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
@@ -391,18 +391,15 @@ pub fn format_expr(
391391
})
392392
}
393393

394-
pub fn rewrite_array<'a, T: 'a>(
394+
pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
395395
name: &'a str,
396396
exprs: impl Iterator<Item = &'a T>,
397397
span: Span,
398398
context: &'a RewriteContext,
399399
shape: Shape,
400400
force_separator_tactic: Option<SeparatorTactic>,
401401
delim_token: Option<DelimToken>,
402-
) -> Option<String>
403-
where
404-
T: Rewrite + Spanned + ToExpr,
405-
{
402+
) -> Option<String> {
406403
overflow::rewrite_with_square_brackets(
407404
context,
408405
name,
@@ -1263,7 +1260,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
12631260
}
12641261

12651262
/// In case special-case style is required, returns an offset from which we start horizontal layout.
1266-
pub fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<(bool, usize)> {
1263+
pub fn maybe_get_args_offset(callee_str: &str, args: &[OverflowableItem]) -> Option<(bool, usize)> {
12671264
if let Some(&(_, num_args_before)) = SPECIAL_MACRO_WHITELIST
12681265
.iter()
12691266
.find(|&&(s, _)| s == callee_str)
@@ -1358,7 +1355,7 @@ fn is_simple_expr(expr: &ast::Expr) -> bool {
13581355
}
13591356
}
13601357

1361-
pub fn is_every_expr_simple<T: ToExpr>(lists: &[&T]) -> bool {
1358+
pub fn is_every_expr_simple(lists: &[OverflowableItem]) -> bool {
13621359
lists
13631360
.iter()
13641361
.all(|arg| arg.to_expr().map_or(false, is_simple_expr))
@@ -1723,16 +1720,13 @@ pub fn rewrite_field(
17231720
}
17241721
}
17251722

1726-
fn rewrite_tuple_in_visual_indent_style<'a, T>(
1723+
fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
17271724
context: &RewriteContext,
17281725
mut items: impl Iterator<Item = &'a T>,
17291726
span: Span,
17301727
shape: Shape,
17311728
is_singleton_tuple: bool,
1732-
) -> Option<String>
1733-
where
1734-
T: Rewrite + Spanned + ToExpr + 'a,
1735-
{
1729+
) -> Option<String> {
17361730
// In case of length 1, need a trailing comma
17371731
debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
17381732
if is_singleton_tuple {
@@ -1774,16 +1768,13 @@ where
17741768
Some(format!("({})", list_str))
17751769
}
17761770

1777-
pub fn rewrite_tuple<'a, T>(
1771+
pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
17781772
context: &'a RewriteContext,
17791773
items: impl Iterator<Item = &'a T>,
17801774
span: Span,
17811775
shape: Shape,
17821776
is_singleton_tuple: bool,
1783-
) -> Option<String>
1784-
where
1785-
T: Rewrite + Spanned + ToExpr + 'a,
1786-
{
1777+
) -> Option<String> {
17871778
debug!("rewrite_tuple {:?}", shape);
17881779
if context.use_block_indent() {
17891780
// We use the same rule as function calls for rewriting tuples.
@@ -1999,89 +1990,6 @@ fn rewrite_expr_addrof(
19991990
rewrite_unary_prefix(context, operator_str, expr, shape)
20001991
}
20011992

2002-
pub trait ToExpr {
2003-
fn to_expr(&self) -> Option<&ast::Expr>;
2004-
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool;
2005-
}
2006-
2007-
impl ToExpr for ast::Expr {
2008-
fn to_expr(&self) -> Option<&ast::Expr> {
2009-
Some(self)
2010-
}
2011-
2012-
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
2013-
can_be_overflowed_expr(context, self, len)
2014-
}
2015-
}
2016-
2017-
impl ToExpr for ast::Ty {
2018-
fn to_expr(&self) -> Option<&ast::Expr> {
2019-
None
2020-
}
2021-
2022-
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
2023-
can_be_overflowed_type(context, self, len)
2024-
}
2025-
}
2026-
2027-
impl<'a> ToExpr for TuplePatField<'a> {
2028-
fn to_expr(&self) -> Option<&ast::Expr> {
2029-
None
2030-
}
2031-
2032-
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
2033-
can_be_overflowed_pat(context, self, len)
2034-
}
2035-
}
2036-
2037-
impl<'a> ToExpr for ast::StructField {
2038-
fn to_expr(&self) -> Option<&ast::Expr> {
2039-
None
2040-
}
2041-
2042-
fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
2043-
false
2044-
}
2045-
}
2046-
2047-
impl<'a> ToExpr for MacroArg {
2048-
fn to_expr(&self) -> Option<&ast::Expr> {
2049-
match *self {
2050-
MacroArg::Expr(ref expr) => Some(expr),
2051-
_ => None,
2052-
}
2053-
}
2054-
2055-
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
2056-
match *self {
2057-
MacroArg::Expr(ref expr) => can_be_overflowed_expr(context, expr, len),
2058-
MacroArg::Ty(ref ty) => can_be_overflowed_type(context, ty, len),
2059-
MacroArg::Pat(..) => false,
2060-
MacroArg::Item(..) => len == 1,
2061-
}
2062-
}
2063-
}
2064-
2065-
impl ToExpr for ast::GenericParam {
2066-
fn to_expr(&self) -> Option<&ast::Expr> {
2067-
None
2068-
}
2069-
2070-
fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
2071-
false
2072-
}
2073-
}
2074-
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-
20851993
pub fn is_method_call(expr: &ast::Expr) -> bool {
20861994
match expr.node {
20871995
ast::ExprKind::MethodCall(..) => true,

0 commit comments

Comments
 (0)