Skip to content

Commit c9ee26f

Browse files
committed
Extract ast TraitImplHeader
1 parent 4501819 commit c9ee26f

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

src/items.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -958,20 +958,19 @@ fn format_impl_ref_and_type(
958958
offset: Indent,
959959
) -> Option<String> {
960960
let ast::Impl {
961-
safety,
962-
polarity,
963-
defaultness,
964-
constness,
965-
ref generics,
966-
of_trait: ref trait_ref,
967-
ref self_ty,
968-
..
969-
} = *iimpl;
961+
generics,
962+
of_trait,
963+
self_ty,
964+
items: _,
965+
} = iimpl;
970966
let mut result = String::with_capacity(128);
971967

972968
result.push_str(&format_visibility(context, &item.vis));
973-
result.push_str(format_defaultness(defaultness));
974-
result.push_str(format_safety(safety));
969+
970+
if let Some(of_trait) = of_trait.as_deref() {
971+
result.push_str(format_defaultness(of_trait.defaultness));
972+
result.push_str(format_safety(of_trait.safety));
973+
}
975974

976975
let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
977976
Shape::indented(offset + last_line_width(&result), context.config)
@@ -984,28 +983,24 @@ fn format_impl_ref_and_type(
984983
};
985984
let generics_str = rewrite_generics(context, "impl", generics, shape).ok()?;
986985
result.push_str(&generics_str);
987-
result.push_str(format_constness_right(constness));
988986

989-
let polarity_str = match polarity {
990-
ast::ImplPolarity::Negative(_) => "!",
991-
ast::ImplPolarity::Positive => "",
992-
};
993-
994-
let polarity_overhead;
995987
let trait_ref_overhead;
996-
if let Some(ref trait_ref) = *trait_ref {
988+
if let Some(of_trait) = of_trait.as_deref() {
989+
result.push_str(format_constness_right(of_trait.constness));
990+
let polarity_str = match of_trait.polarity {
991+
ast::ImplPolarity::Negative(_) => "!",
992+
ast::ImplPolarity::Positive => "",
993+
};
997994
let result_len = last_line_width(&result);
998995
result.push_str(&rewrite_trait_ref(
999996
context,
1000-
trait_ref,
997+
&of_trait.trait_ref,
1001998
offset,
1002999
polarity_str,
10031000
result_len,
10041001
)?);
1005-
polarity_overhead = 0; // already written
10061002
trait_ref_overhead = " for".len();
10071003
} else {
1008-
polarity_overhead = polarity_str.len();
10091004
trait_ref_overhead = 0;
10101005
}
10111006

@@ -1020,17 +1015,15 @@ fn format_impl_ref_and_type(
10201015
} else {
10211016
0
10221017
};
1023-
let used_space =
1024-
last_line_width(&result) + polarity_overhead + trait_ref_overhead + curly_brace_overhead;
1018+
let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
10251019
// 1 = space before the type.
10261020
let budget = context.budget(used_space + 1);
10271021
if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
10281022
if !self_ty_str.contains('\n') {
1029-
if trait_ref.is_some() {
1023+
if of_trait.is_some() {
10301024
result.push_str(" for ");
10311025
} else {
10321026
result.push(' ');
1033-
result.push_str(polarity_str);
10341027
}
10351028
result.push_str(&self_ty_str);
10361029
return Some(result);
@@ -1042,12 +1035,10 @@ fn format_impl_ref_and_type(
10421035
// Add indentation of one additional tab.
10431036
let new_line_offset = offset.block_indent(context.config);
10441037
result.push_str(&new_line_offset.to_string(context.config));
1045-
if trait_ref.is_some() {
1038+
if of_trait.is_some() {
10461039
result.push_str("for ");
1047-
} else {
1048-
result.push_str(polarity_str);
10491040
}
1050-
let budget = context.budget(last_line_width(&result) + polarity_overhead);
1041+
let budget = context.budget(last_line_width(&result));
10511042
let type_offset = match context.config.indent_style() {
10521043
IndentStyle::Visual => new_line_offset + trait_ref_overhead,
10531044
IndentStyle::Block => new_line_offset,

tests/source/negative-impl.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
impl ! Display for JoinHandle { }
22

3-
impl ! Box < JoinHandle > { }
4-
53
impl ! std :: fmt :: Display for JoinHandle < T : std :: future :: Future + std :: marker :: Send + std :: marker :: Sync > { }
6-
7-
impl ! JoinHandle < T : std :: future :: Future < Output > + std :: marker :: Send + std :: marker :: Sync + 'static > + 'static { }

tests/target/negative-impl.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
impl !Display for JoinHandle {}
22

3-
impl !Box<JoinHandle> {}
4-
53
impl !std::fmt::Display
64
for JoinHandle<T: std::future::Future + std::marker::Send + std::marker::Sync>
75
{
86
}
9-
10-
impl
11-
!JoinHandle<T: std::future::Future<Output> + std::marker::Send + std::marker::Sync + 'static>
12-
+ 'static
13-
{
14-
}

0 commit comments

Comments
 (0)