From b8b0761cb81a9bd5591041bb0a20a54803bb2b9a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 30 Nov 2022 21:50:11 -0800 Subject: [PATCH] Move TypeParamBound parse loop to associated function --- src/generics.rs | 25 +++++++++++++++++++++++++ src/ty.rs | 17 +---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/generics.rs b/src/generics.rs index 9c2802f87b..6d4fe847ed 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -828,6 +828,31 @@ pub mod parsing { } } + impl TypeParamBound { + pub(crate) fn parse_multiple( + input: ParseStream, + allow_plus: bool, + ) -> Result> { + let mut bounds = Punctuated::new(); + loop { + bounds.push_value(input.parse()?); + if !(allow_plus && input.peek(Token![+])) { + break; + } + bounds.push_punct(input.parse()?); + if !(input.peek(Ident::peek_any) + || input.peek(Token![::]) + || input.peek(Token![?]) + || input.peek(Lifetime) + || input.peek(token::Paren)) + { + break; + } + } + Ok(bounds) + } + } + #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for TraitBound { fn parse(input: ParseStream) -> Result { diff --git a/src/ty.rs b/src/ty.rs index 4068be3c75..5c3b31f71f 100644 --- a/src/ty.rs +++ b/src/ty.rs @@ -924,22 +924,7 @@ pub mod parsing { input: ParseStream, allow_plus: bool, ) -> Result> { - let mut bounds = Punctuated::new(); - loop { - bounds.push_value(input.parse()?); - if !(allow_plus && input.peek(Token![+])) { - break; - } - bounds.push_punct(input.parse()?); - if !(input.peek(Ident::peek_any) - || input.peek(Token![::]) - || input.peek(Token![?]) - || input.peek(Lifetime) - || input.peek(token::Paren)) - { - break; - } - } + let bounds = TypeParamBound::parse_multiple(input, allow_plus)?; // Just lifetimes like `'a + 'b` is not a TraitObject. if !at_least_one_type(&bounds) { return Err(input.error("expected at least one type"));