From 3381c6447aa36db1f442510efaa818b6d9d3901e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 19 Oct 2024 22:39:23 -0700 Subject: [PATCH] Pretty print TypeParamBound::PreciseCapture --- Cargo.toml | 4 +-- src/generics.rs | 78 ++++++++++++++++++------------------------------- 2 files changed, 30 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80651e7..cd6f8cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,13 +19,13 @@ verbatim = ["syn/parsing"] [dependencies] proc-macro2 = { version = "1.0.80", default-features = false } -syn = { version = "2.0.80", default-features = false, features = ["full"] } +syn = { version = "2.0.81", default-features = false, features = ["full"] } [dev-dependencies] indoc = "2" proc-macro2 = { version = "1.0.80", default-features = false } quote = { version = "1.0.35", default-features = false } -syn = { version = "2.0.80", default-features = false, features = ["parsing"] } +syn = { version = "2.0.81", default-features = false, features = ["parsing"] } [lib] doc-scrape-examples = false diff --git a/src/generics.rs b/src/generics.rs index 017e3af..6145345 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -5,9 +5,9 @@ use crate::INDENT; use proc_macro2::TokenStream; use std::ptr; use syn::{ - BoundLifetimes, ConstParam, GenericParam, Generics, LifetimeParam, PredicateLifetime, - PredicateType, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound, WhereClause, - WherePredicate, + BoundLifetimes, CapturedParam, ConstParam, GenericParam, Generics, LifetimeParam, + PreciseCapture, PredicateLifetime, PredicateType, TraitBound, TraitBoundModifier, TypeParam, + TypeParamBound, WhereClause, WherePredicate, }; impl Printer { @@ -109,6 +109,9 @@ impl Printer { self.trait_bound(trait_bound, tilde_const); } TypeParamBound::Lifetime(lifetime) => self.lifetime(lifetime), + TypeParamBound::PreciseCapture(precise_capture) => { + self.precise_capture(precise_capture); + } TypeParamBound::Verbatim(bound) => self.type_param_bound_verbatim(bound), _ => unimplemented!("unknown TypeParamBound"), } @@ -151,19 +154,13 @@ impl Printer { #[cfg(feature = "verbatim")] fn type_param_bound_verbatim(&mut self, tokens: &TokenStream) { use syn::parse::{Parse, ParseStream, Result}; - use syn::{parenthesized, token, Ident, Lifetime, Token}; + use syn::{parenthesized, token, Token}; enum TypeParamBoundVerbatim { Ellipsis, - PreciseCapture(Vec), TildeConst(TraitBound), } - enum Capture { - Lifetime(Lifetime), - Type(Ident), - } - impl Parse for TypeParamBoundVerbatim { fn parse(input: ParseStream) -> Result { let content; @@ -173,33 +170,7 @@ impl Printer { (None, input) }; let lookahead = content.lookahead1(); - if lookahead.peek(Token![use]) { - input.parse::()?; - input.parse::()?; - 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::()?; - } else if lookahead.peek(Token![>]) { - break; - } else { - return Err(lookahead.error()); - } - } - input.parse::]>()?; - Ok(TypeParamBoundVerbatim::PreciseCapture(captures)) - } else if lookahead.peek(Token![~]) { + if lookahead.peek(Token![~]) { content.parse::()?; content.parse::()?; let mut bound: TraitBound = content.parse()?; @@ -223,19 +194,6 @@ 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); @@ -379,4 +337,24 @@ impl Printer { } self.end(); } + + fn precise_capture(&mut self, precise_capture: &PreciseCapture) { + self.word("use<"); + for capture in precise_capture.params.iter().delimited() { + self.captured_param(&capture); + if !capture.is_last { + self.word(", "); + } + } + self.word(">"); + } + + fn captured_param(&mut self, capture: &CapturedParam) { + match capture { + #![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))] + CapturedParam::Lifetime(lifetime) => self.lifetime(lifetime), + CapturedParam::Ident(ident) => self.ident(ident), + _ => unimplemented!("unknown CapturedParam"), + } + } }