Skip to content

Commit fc22fce

Browse files
authored
Merge pull request #1757 from dtolnay/precisecapture
Implement Parse for PreciseCapture
2 parents c9bdfac + 3a45d69 commit fc22fce

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

src/generics.rs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -796,40 +796,16 @@ pub(crate) mod parsing {
796796
#[cfg(feature = "full")]
797797
{
798798
if input.peek(Token![use]) {
799-
let use_token: Token![use] = input.parse()?;
800-
let lt_token: Token![<] = input.parse()?;
801-
let mut params = Punctuated::new();
802-
loop {
803-
let lookahead = input.lookahead1();
804-
params.push_value(if lookahead.peek(Lifetime) {
805-
input.parse().map(CapturedParam::Lifetime)?
806-
} else if lookahead.peek(Ident) {
807-
input.parse().map(CapturedParam::Ident)?
808-
} else if lookahead.peek(Token![>]) {
809-
break;
810-
} else {
811-
return Err(lookahead.error());
812-
});
813-
let lookahead = input.lookahead1();
814-
params.push_punct(if lookahead.peek(Token![,]) {
815-
input.parse::<Token![,]>()?
816-
} else if lookahead.peek(Token![>]) {
817-
break;
818-
} else {
819-
return Err(lookahead.error());
820-
});
821-
}
822-
let gt_token: Token![>] = input.parse()?;
799+
let precise_capture: PreciseCapture = input.parse()?;
823800
return if allow_precise_capture {
824-
Ok(TypeParamBound::PreciseCapture(PreciseCapture {
825-
use_token,
826-
lt_token,
827-
params,
828-
gt_token,
829-
}))
801+
Ok(TypeParamBound::PreciseCapture(precise_capture))
830802
} else {
831803
let msg = "`use<...>` precise capturing syntax is not allowed here";
832-
Err(error::new2(use_token.span, gt_token.span, msg))
804+
Err(error::new2(
805+
precise_capture.use_token.span,
806+
precise_capture.gt_token.span,
807+
msg,
808+
))
833809
};
834810
}
835811
}
@@ -1060,6 +1036,43 @@ pub(crate) mod parsing {
10601036
}
10611037
}
10621038
}
1039+
1040+
#[cfg(feature = "full")]
1041+
#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1042+
impl Parse for PreciseCapture {
1043+
fn parse(input: ParseStream) -> Result<Self> {
1044+
let use_token: Token![use] = input.parse()?;
1045+
let lt_token: Token![<] = input.parse()?;
1046+
let mut params = Punctuated::new();
1047+
loop {
1048+
let lookahead = input.lookahead1();
1049+
params.push_value(if lookahead.peek(Lifetime) {
1050+
input.parse().map(CapturedParam::Lifetime)?
1051+
} else if lookahead.peek(Ident) {
1052+
input.parse().map(CapturedParam::Ident)?
1053+
} else if lookahead.peek(Token![>]) {
1054+
break;
1055+
} else {
1056+
return Err(lookahead.error());
1057+
});
1058+
let lookahead = input.lookahead1();
1059+
params.push_punct(if lookahead.peek(Token![,]) {
1060+
input.parse::<Token![,]>()?
1061+
} else if lookahead.peek(Token![>]) {
1062+
break;
1063+
} else {
1064+
return Err(lookahead.error());
1065+
});
1066+
}
1067+
let gt_token: Token![>] = input.parse()?;
1068+
Ok(PreciseCapture {
1069+
use_token,
1070+
lt_token,
1071+
params,
1072+
gt_token,
1073+
})
1074+
}
1075+
}
10631076
}
10641077

10651078
#[cfg(feature = "printing")]

0 commit comments

Comments
 (0)