Skip to content

Commit

Permalink
Merge pull request #58 from dtolnay/unnamedfields
Browse files Browse the repository at this point in the history
Support unnamed struct/union type syntax
  • Loading branch information
dtolnay authored Sep 3, 2023
2 parents 1bd9b57 + b5aabc5 commit 4ec51af
Show file tree
Hide file tree
Showing 2 changed files with 48 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.23", default-features = false, features = ["full"] }
syn = { version = "2.0.30", default-features = false, features = ["full"] }

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

[lib]
doc-scrape-examples = false
Expand Down
48 changes: 46 additions & 2 deletions src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,25 @@ impl Printer {
fn type_verbatim(&mut self, tokens: &TokenStream) {
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{Token, TypeParamBound};
use syn::{token, FieldsNamed, Token, TypeParamBound};

enum TypeVerbatim {
Ellipsis,
AnonStruct(AnonStruct),
AnonUnion(AnonUnion),
DynStar(DynStar),
MutSelf(MutSelf),
NotType(NotType),
}

struct AnonStruct {
fields: FieldsNamed,
}

struct AnonUnion {
fields: FieldsNamed,
}

struct DynStar {
bounds: Punctuated<TypeParamBound, Token![+]>,
}
Expand All @@ -195,7 +205,15 @@ impl Printer {
impl Parse for TypeVerbatim {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![dyn]) {
if lookahead.peek(Token![struct]) {
input.parse::<Token![struct]>()?;
let fields: FieldsNamed = input.parse()?;
Ok(TypeVerbatim::AnonStruct(AnonStruct { fields }))
} else if lookahead.peek(Token![union]) && input.peek2(token::Brace) {
input.parse::<Token![union]>()?;
let fields: FieldsNamed = input.parse()?;
Ok(TypeVerbatim::AnonUnion(AnonUnion { fields }))
} else if lookahead.peek(Token![dyn]) {
input.parse::<Token![dyn]>()?;
input.parse::<Token![*]>()?;
let bounds = input.parse_terminated(TypeParamBound::parse, Token![+])?;
Expand Down Expand Up @@ -233,6 +251,32 @@ impl Printer {
TypeVerbatim::Ellipsis => {
self.word("...");
}
TypeVerbatim::AnonStruct(ty) => {
self.cbox(INDENT);
self.word("struct {");
self.hardbreak_if_nonempty();
for field in &ty.fields.named {
self.field(field);
self.word(",");
self.hardbreak();
}
self.offset(-INDENT);
self.end();
self.word("}");
}
TypeVerbatim::AnonUnion(ty) => {
self.cbox(INDENT);
self.word("union {");
self.hardbreak_if_nonempty();
for field in &ty.fields.named {
self.field(field);
self.word(",");
self.hardbreak();
}
self.offset(-INDENT);
self.end();
self.word("}");
}
TypeVerbatim::DynStar(ty) => {
self.word("dyn* ");
for type_param_bound in ty.bounds.iter().delimited() {
Expand Down

0 comments on commit 4ec51af

Please sign in to comment.