Struct syn::parse::Lookahead1

source ·
pub struct Lookahead1<'a> { /* private fields */ }
Expand description

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.

Consuming tokens from the source stream after constructing a lookahead object does not also advance the lookahead object.

Example

use syn::{ConstParam, Ident, Lifetime, LifetimeParam, Result, Token, TypeParam};
use syn::parse::{Parse, ParseStream};

// A generic parameter, a single one of the comma-separated elements inside
// angle brackets in:
//
//     fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
//
// On invalid input, lookahead gives us a reasonable error message.
//
//     error: expected one of: identifier, lifetime, `const`
//       |
//     5 |     fn f<!Sized>() {}
//       |          ^
enum GenericParam {
    Type(TypeParam),
    Lifetime(LifetimeParam),
    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())
        }
    }
}

Implementations§

source§

impl<'a> Lookahead1<'a>

source

pub fn peek<T: Peek>(&self, token: T) -> bool

Looks at the next token in the parse stream to determine whether it matches the requested type of token.

Syntax

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)(does not accept keywords)
  • input.peek(Ident::peek_any)
  • input.peek(Lifetime)
  • input.peek(token::Brace)
source

pub fn error(self) -> Error

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.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Lookahead1<'a>

§

impl<'a> !Send for Lookahead1<'a>

§

impl<'a> !Sync for Lookahead1<'a>

§

impl<'a> Unpin for Lookahead1<'a>

§

impl<'a> UnwindSafe for Lookahead1<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.