nikis05 / derive-visitor

MIT License
23 stars 6 forks source link

Add an API to construct visitors without implementing a trait #20

Open Nadrieril opened 3 months ago

Nadrieril commented 3 months ago

I propose this alternative API for constructing visitors using a builder pattern. It looks like:

let mut count = 0;
let mut visitor = VisitorBuilder::new(&mut count)
    .on_enter(|state, x: &File| { *state += 1; })
    .on_enter(|state, x: &Directory| { *state += 1; });
val.drive(&mut visitor);

This can be more convenient than derives for small visitors with simple state.

The first two commits are nits I happened to find convenient for my usage; I can split them off if desired.

nikis05 commented 3 months ago

Hey, thanks for your contribution! I like this idea, will review soon