| 1 | # rctree |
| 2 |  |
| 3 | [](https://crates.io/crates/rctree) |
| 4 | [](https://docs.rs/rctree) |
| 5 | [](https://www.rust-lang.org) |
| 6 |  |
| 7 | |
| 8 | *rctree* is a "DOM-like" tree implemented using reference counting. |
| 9 | |
| 10 | ### Origin |
| 11 | |
| 12 | This is a fork of the [rust-forest](https://github.com/SimonSapin/rust-forest) rctree. |
| 13 | |
| 14 | ### Details |
| 15 | |
| 16 | "DOM-like" here means that data structures can be used to represent |
| 17 | the parsed content of an HTML or XML document, |
| 18 | like [*the* DOM](https://dom.spec.whatwg.org/) does, |
| 19 | but don't necessarily have the exact same API as the DOM. |
| 20 | That is: |
| 21 | |
| 22 | * A tree is made up of nodes. |
| 23 | * Each node has zero or more *child* nodes, which are ordered. |
| 24 | * Each node has a no more than one *parent*, the node that it is a *child* of. |
| 25 | * A node without a *parent* is called a *root*. |
| 26 | * As a consequence, each node may also have *siblings*: its *parent*'s other *children*, if any. |
| 27 | * From any given node, access to its |
| 28 | parent, previous sibling, next sibling, first child, and last child (if any) |
| 29 | can take no more than *O(1)* time. |
| 30 | * Each node also has data associated to it, |
| 31 | which for the purpose of this project is purely generic. |
| 32 | For an HTML document, the data would be either the text of a text node, |
| 33 | or the name and attributes of an element node. |
| 34 | * The tree is mutable: |
| 35 | nodes (with their sub-trees) can be inserted or removed anywhere in the tree. |
| 36 | |
| 37 | The lifetime of nodes is managed through *reference counting*. |
| 38 | To avoid reference cycles which would cause memory leaks, the tree is *asymmetric*: |
| 39 | each node holds optional *strong references* to its next sibling and first child, |
| 40 | but only optional *weak references* to its parent, previous sibling, and last child. |
| 41 | |
| 42 | Nodes are destroyed as soon as there is no strong reference left to them. |
| 43 | The structure is such that holding a reference to the root |
| 44 | is sufficient to keep the entire tree alive. |
| 45 | However, if for example the only reference that exists from outside the tree |
| 46 | is one that you use to traverse it, |
| 47 | you will not be able to go back "up" the tree to ancestors and previous siblings after going "down", |
| 48 | as those nodes will have been destroyed. |
| 49 | |
| 50 | Weak references to destroyed nodes are treated as if they were not set at all. |
| 51 | (E.g. a node can become a root when its parent is destroyed.) |
| 52 | |
| 53 | Since nodes are *aliased* (have multiple references to them), |
| 54 | [`RefCell`](http://doc.rust-lang.org/std/cell/index.html) is used for interior mutability. |
| 55 | |
| 56 | Advantages: |
| 57 | |
| 58 | * A single `Node` user-visible type to manipulate the tree, with methods. |
| 59 | * Memory is freed as soon as it becomes unused (if parts of the tree are removed). |
| 60 | |
| 61 | Disadvantages: |
| 62 | |
| 63 | * The tree can only be accessed from the thread is was created in. |
| 64 | * Any tree manipulation, including read-only traversals, |
| 65 | requires incrementing and decrementing reference counts, |
| 66 | which causes run-time overhead. |
| 67 | * Nodes are allocated individually, which may cause memory fragmentation and hurt performance. |
| 68 | |
| 69 | ### License |
| 70 | |
| 71 | *rctree* is licensed under the **MIT**. |