nikis05 / derive-visitor

MIT License
20 stars 6 forks source link

Implement mutable visits #9

Closed lovasoa closed 1 year ago

lovasoa commented 1 year ago

This PR implements two new traits: VisitorMut and DriveMut. It allows mutating the data structure that is being visited.

Here is an example of what is now possible :


#[derive(DriveMut)]
struct Chain {
    next: Option<Box<Chain>>,
}

#[derive(VisitorMut)]
#[visitor(Chain(enter))]
struct ChainCutter {
    cut_at_depth: usize,
}

impl ChainCutter {
    fn enter_chain(&mut self, item: &mut Chain) {
        if self.cut_at_depth == 0 {
            item.next = None;
        } else {
            self.cut_at_depth -= 1;
        }
    }
}

Closes #8

lovasoa commented 1 year ago

Great, thanks for merging !