Struct StepCursor

struct StepCursor<'c, 'a> { ... }

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: 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.

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

fn clone(self: &Self) -> Self

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

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

fn deref(self: &Self) -> &<Self as >::Target

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

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

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

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

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

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

impl<P, T> Receiver for StepCursor<'c, 'a>

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

fn type_id(self: &Self) -> TypeId

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

fn borrow(self: &Self) -> &T

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

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

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

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

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

fn from(t: T) -> T

Returns the argument unchanged.

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

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

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

fn into(self: 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 for StepCursor<'c, 'a>

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

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

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