Struct StepCursor

pub struct StepCursor<'c, 'a> { /* private fields */ }

Cursor state associated with speculative parsing.

This type is the input of the closure provided to ParseStream::step.

Example

use proc_macro2::TokenTree;
use syn::Result;
use syn::parse::ParseStream;

// This function advances the stream past the next occurrence of `@`. If
// no `@` is present in the stream, the stream position is unchanged and
// an error is returned.
fn skip_past_next_at(input: ParseStream) -> Result<()> {
    input.step(|cursor| {
        let mut rest = *cursor;
        while let Some((tt, next)) = rest.token_tree() {
            match &tt {
                TokenTree::Punct(punct) if punct.as_char() == '@' => {
                    return Ok(((), next));
                }
                _ => rest = next,
            }
        }
        Err(cursor.error("no `@` was found after this point"))
    })
}
#
# fn remainder_after_skipping_past_next_at(
#     input: ParseStream,
# ) -> Result<proc_macro2::TokenStream> {
#     skip_past_next_at(input)?;
#     input.parse()
# }
#
# use syn::parse::Parser;
# let remainder = remainder_after_skipping_past_next_at
#     .parse_str("a @ b c")
#     .unwrap();
# assert_eq!(remainder.to_string(), "b c");

Implementations

impl<'c, 'a> StepCursor<'c, 'a>

fn error<T: Display>(self, message: T) -> Error

Triggers an error at the current position of the parse stream.

The ParseStream::step invocation will return this same error without advancing the stream state.

Trait Implementations

impl<'c, 'a> Clone for StepCursor<'c, 'a>

fn clone(&self) -> Self

impl<'c, 'a> Copy for StepCursor<'c, 'a>

impl<'c, 'a> Deref for StepCursor<'c, 'a>

type Target = Cursor<'c>;
fn deref(&self) -> &Self::Target

Auto Trait Implementations

impl<'c, 'a> !Send for StepCursor<'c, 'a>

impl<'c, 'a> !Sync for StepCursor<'c, 'a>

impl<'c, 'a> Freeze for StepCursor<'c, 'a>

impl<'c, 'a> RefUnwindSafe for StepCursor<'c, 'a>

impl<'c, 'a> Unpin for StepCursor<'c, 'a>

impl<'c, 'a> UnsafeUnpin for StepCursor<'c, 'a>

impl<'c, 'a> UnwindSafe for StepCursor<'c, 'a>

Blanket Implementations

impl<P, T> Receiver for StepCursor<'c, 'a> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for StepCursor<'c, 'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for StepCursor<'c, 'a> where T: ?Sized,

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

impl<T> CloneToUninit for StepCursor<'c, 'a> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for StepCursor<'c, 'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for StepCursor<'c, 'a> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for StepCursor<'c, 'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for StepCursor<'c, 'a> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for StepCursor<'c, 'a> where U: TryFrom<T>,

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