Skip to content

Commit

Permalink
Merge pull request #80 from dtolnay/precisecapture
Browse files Browse the repository at this point in the history
Pretty-print precise capture syntax (`use<>`)
  • Loading branch information
dtolnay committed Aug 25, 2024
2 parents 61bd508 + b54cd12 commit dd76fd6
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,19 @@ impl Printer {
#[cfg(feature = "verbatim")]
fn type_param_bound_verbatim(&mut self, tokens: &TokenStream) {
use syn::parse::{Parse, ParseStream, Result};
use syn::{parenthesized, token, Token};
use syn::{parenthesized, token, Ident, Lifetime, Token};

enum TypeParamBoundVerbatim {
Ellipsis,
PreciseCapture(Vec<Capture>),
TildeConst(TraitBound),
}

enum Capture {
Lifetime(Lifetime),
Type(Ident),
}

impl Parse for TypeParamBoundVerbatim {
fn parse(input: ParseStream) -> Result<Self> {
let content;
Expand All @@ -167,7 +173,33 @@ impl Printer {
(None, input)
};
let lookahead = content.lookahead1();
if lookahead.peek(Token![~]) {
if lookahead.peek(Token![use]) {
input.parse::<Token![use]>()?;
input.parse::<Token![<]>()?;
let mut captures = Vec::new();
loop {
let lookahead = input.lookahead1();
captures.push(if lookahead.peek(Lifetime) {
input.parse().map(Capture::Lifetime)?
} else if lookahead.peek(Ident) {
input.parse().map(Capture::Type)?
} else if lookahead.peek(Token![>]) {
break;
} else {
return Err(lookahead.error());
});
let lookahead = input.lookahead1();
if lookahead.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else if lookahead.peek(Token![>]) {
break;
} else {
return Err(lookahead.error());
}
}
input.parse::<Token![>]>()?;
Ok(TypeParamBoundVerbatim::PreciseCapture(captures))
} else if lookahead.peek(Token![~]) {
content.parse::<Token![~]>()?;
content.parse::<Token![const]>()?;
let mut bound: TraitBound = content.parse()?;
Expand All @@ -191,6 +223,19 @@ impl Printer {
TypeParamBoundVerbatim::Ellipsis => {
self.word("...");
}
TypeParamBoundVerbatim::PreciseCapture(captures) => {
self.word("use<");
for capture in captures.iter().delimited() {
match *capture {
Capture::Lifetime(lifetime) => self.lifetime(lifetime),
Capture::Type(ident) => self.ident(ident),
}
if !capture.is_last {
self.word(", ");
}
}
self.word(">");
}
TypeParamBoundVerbatim::TildeConst(trait_bound) => {
let tilde_const = true;
self.trait_bound(&trait_bound, tilde_const);
Expand Down

0 comments on commit dd76fd6

Please sign in to comment.