Skip to content

Commit

Permalink
Extract function for printing const value in generic argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 15, 2024
1 parent 816a333 commit cc4bb32
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,36 +741,40 @@ pub(crate) mod printing {
match self {
GenericArgument::Lifetime(lt) => lt.to_tokens(tokens),
GenericArgument::Type(ty) => ty.to_tokens(tokens),
GenericArgument::Const(expr) => match expr {
Expr::Lit(expr) => expr.to_tokens(tokens),

Expr::Path(expr)
if expr.attrs.is_empty()
&& expr.qself.is_none()
&& expr.path.get_ident().is_some() =>
{
expr.to_tokens(tokens);
}

#[cfg(feature = "full")]
Expr::Block(expr) => expr.to_tokens(tokens),

#[cfg(not(feature = "full"))]
Expr::Verbatim(expr) => expr.to_tokens(tokens),

// ERROR CORRECTION: Add braces to make sure that the
// generated code is valid.
_ => token::Brace::default().surround(tokens, |tokens| {
expr.to_tokens(tokens);
}),
},
GenericArgument::Const(expr) => print_const_argument(expr, tokens),
GenericArgument::AssocType(assoc) => assoc.to_tokens(tokens),
GenericArgument::AssocConst(assoc) => assoc.to_tokens(tokens),
GenericArgument::Constraint(constraint) => constraint.to_tokens(tokens),
}
}
}

fn print_const_argument(expr: &Expr, tokens: &mut TokenStream) {
match expr {
Expr::Lit(expr) => expr.to_tokens(tokens),

Expr::Path(expr)
if expr.attrs.is_empty()
&& expr.qself.is_none()
&& expr.path.get_ident().is_some() =>
{
expr.to_tokens(tokens);
}

#[cfg(feature = "full")]
Expr::Block(expr) => expr.to_tokens(tokens),

#[cfg(not(feature = "full"))]
Expr::Verbatim(expr) => expr.to_tokens(tokens),

// ERROR CORRECTION: Add braces to make sure that the
// generated code is valid.
_ => token::Brace::default().surround(tokens, |tokens| {
expr.to_tokens(tokens);
}),
}
}

#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for AngleBracketedGenericArguments {
fn to_tokens(&self, tokens: &mut TokenStream) {
Expand Down

0 comments on commit cc4bb32

Please sign in to comment.