pub struct Lookahead1<'a> { /* fields omitted */ }
Support for checking the next token in a stream to decide how to parse.
An important advantage over ParseStream::peek
is that here we
automatically construct an appropriate error message based on the token
alternatives that get peeked. If you are producing your own error message,
go ahead and use ParseStream::peek
instead.
Use ParseStream::lookahead1
to construct this object.
#[macro_use]
extern crate syn;
use syn::{ConstParam, Ident, Lifetime, LifetimeDef, TypeParam};
use syn::parse::{Parse, ParseStream, Result};
enum GenericParam {
Type(TypeParam),
Lifetime(LifetimeDef),
Const(ConstParam),
}
impl Parse for GenericParam {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Ident) {
input.parse().map(GenericParam::Type)
} else if lookahead.peek(Lifetime) {
input.parse().map(GenericParam::Lifetime)
} else if lookahead.peek(Token![const]) {
input.parse().map(GenericParam::Const)
} else {
Err(lookahead.error())
}
}
}
Looks at the next token in the parse stream to determine whether it
matches the requested type of token.
Note that this method does not use turbofish syntax. Pass the peek type
inside of parentheses.
input.peek(Token![struct])
input.peek(Token![==])
input.peek(Ident)
input.peek(Lifetime)
input.peek(token::Brace)
Triggers an error at the current position of the parse stream.
The error message will identify all of the expected token types that
have been peeked against this lookahead instance.
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)