Skip to content

Commit

Permalink
Support generics on const item
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 3, 2023
1 parent c3b9685 commit 4b8b6ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ verbatim = ["syn/parsing"]

[dependencies]
proc-macro2 = { version = "1.0.63", default-features = false }
syn = { version = "2.0.30", default-features = false, features = ["full"] }
syn = { version = "2.0.31", default-features = false, features = ["full"] }

[dev-dependencies]
syn = { version = "2.0.30", default-features = false, features = ["parsing"] }
syn = { version = "2.0.31", default-features = false, features = ["parsing"] }

[lib]
doc-scrape-examples = false
Expand Down
26 changes: 24 additions & 2 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,9 @@ mod verbatim {
pub vis: Visibility,
pub defaultness: bool,
pub ident: Ident,
pub generics: Generics,
pub ty: Type,
pub value: Option<Expr>,
}

pub struct FlexibleItemFn {
Expand Down Expand Up @@ -1436,16 +1438,26 @@ mod verbatim {
) -> Result<Self> {
input.parse::<Token![const]>()?;
let ident = input.call(Ident::parse_any)?;
let mut generics: Generics = input.parse()?;
input.parse::<Token![:]>()?;
let ty: Type = input.parse()?;
let value = if input.parse::<Option<Token![=]>>()?.is_some() {
let expr: Expr = input.parse()?;
Some(expr)
} else {
None
};
generics.where_clause = input.parse()?;
input.parse::<Token![;]>()?;

Ok(FlexibleItemConst {
attrs,
vis,
defaultness,
ident,
generics,
ty,
value,
})
}
}
Expand Down Expand Up @@ -1587,16 +1599,26 @@ mod verbatim {
impl Printer {
pub fn flexible_item_const(&mut self, item: &FlexibleItemConst) {
self.outer_attrs(&item.attrs);
self.cbox(0);
self.cbox(INDENT);
self.visibility(&item.vis);
if item.defaultness {
self.word("default ");
}
self.word("const ");
self.ident(&item.ident);
self.generics(&item.generics);
self.word(": ");
self.cbox(-INDENT);
self.ty(&item.ty);
self.word(";");
self.end();
if let Some(value) = &item.value {
self.word(" = ");
self.neverbreak();
self.ibox(-INDENT);
self.expr(value);
self.end();
}
self.where_clause_oneline_semi(&item.generics.where_clause);
self.end();
self.hardbreak();
}
Expand Down

0 comments on commit 4b8b6ef

Please sign in to comment.