Skip to content

Commit

Permalink
Share some logic between impl Trait and dyn Trait parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 6, 2021
1 parent cfff4f5 commit 0beb488
Showing 1 changed file with 29 additions and 46 deletions.
75 changes: 29 additions & 46 deletions src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,31 +885,36 @@ pub mod parsing {
pub(crate) fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
Ok(TypeTraitObject {
dyn_token: input.parse()?,
bounds: {
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;
}
}
// Just lifetimes like `'a + 'b` is not a TraitObject.
if !at_least_one_type(&bounds) {
return Err(input.error("expected at least one type"));
}
bounds
},
bounds: Self::parse_bounds(input, allow_plus)?,
})
}

fn parse_bounds(
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;
}
}
// Just lifetimes like `'a + 'b` is not a TraitObject.
if !at_least_one_type(&bounds) {
return Err(input.error("expected at least one type"));
}
Ok(bounds)
}
}

#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
Expand All @@ -930,29 +935,7 @@ pub mod parsing {
pub(crate) fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
Ok(TypeImplTrait {
impl_token: input.parse()?,
bounds: {
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;
}
}
// Just lifetimes like `impl 'a` is not an ImplTrait.
if !at_least_one_type(&bounds) {
return Err(input.error("expected at least one type"));
}
bounds
},
bounds: TypeTraitObject::parse_bounds(input, allow_plus)?,
})
}
}
Expand Down

0 comments on commit 0beb488

Please sign in to comment.