Merge pull request #616 from dtolnay/doc

Preserve doc comments on enum variants
This commit is contained in:
David Tolnay 2020-12-30 00:08:35 -08:00 committed by GitHub
commit f90cc999de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 0 deletions

View File

@ -357,6 +357,9 @@ fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
write_atom(out, enm.repr);
writeln!(out, " {{");
for variant in &enm.variants {
for line in variant.doc.to_string().lines() {
writeln!(out, " //{}", line);
}
writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
}
writeln!(out, "}};");

View File

@ -280,9 +280,11 @@ fn expand_enum(enm: &Enum) -> TokenStream {
let repr = enm.repr;
let type_id = type_id(&enm.name);
let variants = enm.variants.iter().map(|variant| {
let doc = &variant.doc;
let variant_ident = &variant.name.rust;
let discriminant = &variant.discriminant;
Some(quote! {
#doc
pub const #variant_ident: Self = #ident { repr: #discriminant };
})
});

View File

@ -167,6 +167,7 @@ pub struct Receiver {
}
pub struct Variant {
pub doc: Doc,
pub name: Pair,
pub discriminant: Discriminant,
pub expr: Option<Expr>,

View File

@ -199,12 +199,14 @@ fn parse_variant(
variant: RustVariant,
discriminants: &mut DiscriminantSet,
) -> Result<Variant> {
let mut doc = Doc::new();
let mut cxx_name = None;
let mut rust_name = None;
attrs::parse(
cx,
&variant.attrs,
attrs::Parser {
doc: Some(&mut doc),
cxx_name: Some(&mut cxx_name),
rust_name: Some(&mut rust_name),
..Default::default()
@ -233,6 +235,7 @@ fn parse_variant(
let expr = variant.discriminant.map(|(_, expr)| expr);
Ok(Variant {
doc,
name,
discriminant,
expr,