Skip to content

Commit

Permalink
Move TypeParamBound parse loop to associated function
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 1, 2022
1 parent 3e915e5 commit b8b0761
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
25 changes: 25 additions & 0 deletions src/generics.rs
Expand Up @@ -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> {
Expand Down
17 changes: 1 addition & 16 deletions src/ty.rs
Expand Up @@ -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"));
Expand Down

0 comments on commit b8b0761

Please sign in to comment.