3 ms·
Halfway through your comment I was going to say you were wrong, that ASTs aren't self-referential and that you can easily allocate them in a single block using
by withoutboats3 2y ago
Halfway through your comment I was going to say you were wrong, that ASTs aren't self-referential and that you can easily allocate them in a single block using a bump allocator, but then obviously you say so yourself in the second part of the post. This is exactly what this post proposes to do when it proposes to remove the safeguards and says it can't show a Rust version (the "AstContext" is just a sort of bad bump allocator), except of course that in Rust it's completely memory safe.
> Box<T> generally ruins your ability to do pattern matching, which is a deal-breaker when working with ASTs.
For what it's worth I find that I never want to pass ownership when traversing ASTs with pattern matching, so I just bind everything by reference and don't experience this problem. YMMV.
- sshine 2y agoOK, I didn't actually read the "I give up on doing this in Rust" section very carefully. The article capitulates: > last time I asked how to do that in my company’s Slack channel, the responses I received were something like “don’t” and “why would you do that?” and “someone please call security.” It should not have been a surprise, as an AST is basically a linked list with extra steps, and Rust hates linked lists. If I didn't tell myself "go find a good solution", I might have not found someone suggesting "Just use a bump allocator and make your nodes live until one exact, shared lifetime." The article does agree in principle with this solution: > In my experience, it’s quite easy to do away with the ceremony of making each node hold a reference count altogether, and instead decide on a more disciplined approach to ownership. I think this is one of those examples where you get a principled, elegant and safe solution. You just gotta find it first. But here it is. (Also, I read about it on a forum when searching for a better solution than box_patterns.)