syn icon indicating copy to clipboard operation
syn copied to clipboard

Use variable-capturing closure within parse_terminated?

Open recatek opened this issue 1 year ago • 0 comments

I'm trying to parse a set of declarations and then implementations, where the implementation parsing is dependent on information from the declaration portion. I'm having trouble getting that former information to the latter during the parsing process because the various parse functions (namely, parse_terminated) won't take variable-capturing closures, only fn.

impl Parse for Syntax {
    fn parse(input: ParseStream) -> Result<Self> {
        let declarations = input.parse_terminated::<Declaration, Token![;]>(parse_declaration);
        let declarations: Box<[Declaration]> = declarations.into_iter().collect();

        let parse_implementation = |input: ParseStream| parse_implementation(input, &declarations);
        let implementations = input.parse_terminated::<Implementation, Token![;]>(parse_implementation);
        //                                 Can't do this, since this isn't a fn() ^^^^^^^^^^^^^^^^^^^^

        // ...
    }
}

I can work around this by using a singleton, but that's quite ugly and I was wondering if there's a better way here, or if this is a worthwhile thing for syn to alllow?

recatek avatar Aug 28 '22 18:08 recatek