winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
572 stars 44 forks source link

accept `FnMut` instead of just `Fn` as `take_till` parameter #558

Open BLucky-gh opened 4 months ago

BLucky-gh commented 4 months ago

Please complete the following tasks

winnow version

0.6.13

Describe your use case

I was trying to write a parser that runs take_till until it finds a matching bracket (keeping all the brackets found inside balanced), which requires a mutable counter variable to keep track of how many brackets have been found so far

Describe the solution you'd like

Allow passing FnMut to take_till to allow for more complex parsers without needing to make the entire parser stateful or juggling data in and out of a Cell

Alternatives, if applicable

I've used Cell and a move closure to keep the closure Fn while still allowing for mutating the counter, but I feel like it makes the closure unnecessarily complicated and long just to deal with juggling data in and out of the Cell, so I think if there's no functional reason to constraint the parameter to Fn, it should be relaxed

Additional Context

https://github.com/winnow-rs/winnow/discussions/552#discussioncomment-10032721

epage commented 4 months ago

By take_till, I'm assume you are referring to the impl ContainsToken<T> parameter that is used there and elsewhere.

That affects

This is an interesting one as I don't have a good feel for the trade offs of Fn vs FnMut with winnow.


For related conversations that might spark ideas:

For local context,

Looking back at nom's issues