Move TypeParamBound parse loop to associated function

This commit is contained in:
David Tolnay 2022-11-30 21:50:11 -08:00
parent 3e915e5c98
commit b8b0761cb8
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 26 additions and 16 deletions

View File

@ -828,6 +828,31 @@ pub mod parsing {
}
}
impl TypeParamBound {
pub(crate) fn parse_multiple(
input: ParseStream,
allow_plus: bool,
) -> Result<Punctuated<Self, Token![+]>> {
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<Self> {

View File

@ -924,22 +924,7 @@ pub mod parsing {
input: ParseStream,
allow_plus: bool,
) -> Result<Punctuated<TypeParamBound, Token![+]>> {
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"));