Originally posted by oleid
View Post
There are crates that deal with trees, yes, but none which is dynamically (and) mutable at the same time. So here's a virtual code example of a dynamically mutable tree:
Code:
class Tag { vector<Tag*> children; Tag *parent; // and methods that would tolerate aliased mutability - which is forbidden in Rust. "Safe" blocks don't cancel the borrow checker. int CountSize() { if (size_ == -1) { // not counted yet, need to count and cache it size_ = count my size; // size_ also depends on something from children or parents so need to call also into // their mutable methods but this isn't allowed. Full Stop. // What's worse you can catch this only at compile time because you're using RefCell and what's even worse it might not get triggered // until it detects aliased mutability. } return size_; }
a) you should be able to call mutable (non-const) methods of children or parents from your node's methods.
b) therefore it should never trigger a runtime error like RefCell does, which is why you can't use it because it's only a matter of time - as your code becomes larger it will start throwing runtime errors and you can't design your code around this.
Comment