From 60419aa08a02fadac2e6496cf12c0bb568202ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 20 Dec 2023 15:22:06 +0100 Subject: [PATCH] Refactor AST trait bound modifiers --- src/types.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/types.rs b/src/types.rs index a5a4244903c..cd2582e66be 100644 --- a/src/types.rs +++ b/src/types.rs @@ -537,28 +537,19 @@ fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option { impl Rewrite for ast::GenericBound { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { match *self { - ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => { + ast::GenericBound::Trait(ref poly_trait_ref, modifiers) => { let snippet = context.snippet(self.span()); let has_paren = snippet.starts_with('(') && snippet.ends_with(')'); - let rewrite = match trait_bound_modifier { - ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape), - ast::TraitBoundModifier::Maybe => poly_trait_ref - .rewrite(context, shape.offset_left(1)?) - .map(|s| format!("?{}", s)), - ast::TraitBoundModifier::MaybeConst(_) => poly_trait_ref - .rewrite(context, shape.offset_left(7)?) - .map(|s| format!("~const {}", s)), - ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref - .rewrite(context, shape.offset_left(8)?) - .map(|s| format!("~const ?{}", s)), - ast::TraitBoundModifier::Negative => poly_trait_ref - .rewrite(context, shape.offset_left(1)?) - .map(|s| format!("!{}", s)), - ast::TraitBoundModifier::MaybeConstNegative => poly_trait_ref - .rewrite(context, shape.offset_left(8)?) - .map(|s| format!("~const !{}", s)), - }; - rewrite.map(|s| if has_paren { format!("({})", s) } else { s }) + let mut constness = modifiers.constness.as_str().to_string(); + if !constness.is_empty() { + constness.push(' '); + } + let polarity = modifiers.polarity.as_str(); + let shape = shape.offset_left(constness.len() + polarity.len())?; + poly_trait_ref + .rewrite(context, shape) + .map(|s| format!("{constness}{polarity}{s}")) + .map(|s| if has_paren { format!("({})", s) } else { s }) } ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape), }