Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unnamed struct/union type syntax #58

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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