
A macro designed to work with indextree crate that takes a human-readable tree structure and creates a tree in memory.
The implementation is written as a procedural macro, since I want to minimize the amount of recursion and keep the code readable considering its complexity.
Here's an example of the macro taken directly from its documentation:
let root_node = arena.new_node("root node");
tree!(
&mut arena,
root_node => {
"1",
"2" => {
"2_1" => { "2_1_1" },
"2_2",
},
"3",
}
);
let automagical_root_node = tree!(
&mut arena,
"root node, but automagically created" => {
"1",
"2" => {
"2_1" => { "2_1_1" },
"2_2",
},
"3",
}
);
It takes in a mutable reference to Arena
, and then it takes the tree structure itself. The result of calling tree!
is the NodeId
pointing to the root of the newly created node. It is also possible to give it a NodeId
and it will build upon an already existent node.
For automatically detecting whether or not the provided root node is a NodeId
or some other type, I used autoref specialization, since normal specialization is not yet added in Rust for general use.
Big thanks to Rust community for pointing out the soundness holes and the autoref specialization trick!
Here's the pull request that added the tree!
macro to the indextree crate:
https://github.com/saschagrunert/indextree/pull/110