Refactor AST trait bound modifiers

This commit is contained in:
León Orell Valerian Liehr 2023-12-20 15:22:06 +01:00
parent ca2472edd7
commit 60419aa08a

View File

@ -537,28 +537,19 @@ fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option<String> {
impl Rewrite for ast::GenericBound { impl Rewrite for ast::GenericBound {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match *self { 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 snippet = context.snippet(self.span());
let has_paren = snippet.starts_with('(') && snippet.ends_with(')'); let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
let rewrite = match trait_bound_modifier { let mut constness = modifiers.constness.as_str().to_string();
ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape), if !constness.is_empty() {
ast::TraitBoundModifier::Maybe => poly_trait_ref constness.push(' ');
.rewrite(context, shape.offset_left(1)?) }
.map(|s| format!("?{}", s)), let polarity = modifiers.polarity.as_str();
ast::TraitBoundModifier::MaybeConst(_) => poly_trait_ref let shape = shape.offset_left(constness.len() + polarity.len())?;
.rewrite(context, shape.offset_left(7)?) poly_trait_ref
.map(|s| format!("~const {}", s)), .rewrite(context, shape)
ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref .map(|s| format!("{constness}{polarity}{s}"))
.rewrite(context, shape.offset_left(8)?) .map(|s| if has_paren { format!("({})", s) } else { s })
.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 })
} }
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape), ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
} }