5 ms·
> I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once. The simple way
by msbarnett 9y ago
> I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once.
The simple way to do that would be to allocate an array, and use indices into the array rather than pointers/references.
Doing it with pointers isn't so much harder in Rust than C as it is that Rust is making you deal with how hard it is to get this right, whereas in C the compiler is happy to let you think it's easy while you accidentally shoot yourself in the foot.
If you want the Rust compiler to accept your mistakes, you can always wrap it in an unsafe block ;)
- fish_fan 9y agoWhile it may be simpler, it also removes any safety that rust adds. I'd imagine you'd get more mileage out of just using pointers and unsafe blocks liberally to get lifetime checking where you can.
- skariel1 9y agoIt is still safer than c, as it will not allow any segfaults. The only thing that can happen is a runtime error but no memory corruption so it is more akin to say Java or Go
- steveklabnik 9y agoThe context of this thread is unsafe, which can segfault.
- ComputerGuru 9y agoYou are describing the behavior of standard, safe rust code. Using unsafe blocks throws all that away.
- jeffdavis 9y agoBut in my example this is not hard to get right in C. The tree is constructed (on the stack would be fine), then used for a while without mutating it, then freed all at once. The thing that makes this hard in rust is destructors. If there's a cycle between A and B, and you destruct A first, then B, then B's destructor would see a dangling reference to A. And vice versa if you destruct B first. But I don't need destructors, or at least ones that can see these references, so it's frustrating.
- burntsushi 9y agoSince you want to use the C method, have you tried using raw pointers?
- steveklabnik 9y ago> But I don't need destructors, or at least ones that can see these references, so it's frustrating. If you bound it so that it only accepts Copy types, then you can know there are no destructors.
- jeffdavis 9y agoInteresting. Then rust should not care about the destruction order, right?
- steveklabnik 9y agoFor this purpose, no, I don't think. That said I might be missing something and maybe th compiler doesn't understand this, so I'm not saying it's a panacea in your case, just might be worth looking at.
- dbaupp 9y agoThe tree itself will need destructors to clean up allocations, and that's what's problematic here: if the tree is destroyed top down, the destructor of the children may access the parent which has already been invalidated, and similarly destroying bottom up risks the destructor of the parent accessing the children.
- msbarnett 9y ago> But in my example this is not hard to get right in C. The tree is constructed (on the stack would be fine), then used for a while without mutating it, then freed all at once. It's still "hard to get right" in that at any time nothing is stopping you from violating any of the assumptions that make this "easy". It's never easy to write a solution that's "guaranteed to be safe" in C, but that's what you're trying to do by writing such a solution in Rust. To give but one example, nothing in C will stop your nodes from containing some resources which needs to be manually destructed and which get leaked when the stack frame is reclaimed. Rust is going to require you to make those assumptions explicit, so that it can enforce them -- in this case, you need to explicitly restrict your solution to dealing with Copy types, which by construction don't implement Drop, and therefore have no destructors. But at the end of the day, if all you want to do is swear to the compiler that you know what you're doing and you promise to not be stupid, wrap it in an unsafe and get C-style consequences if you got things wrong. You get C-Style easiness only by explicitly abandoning the attempt at guaranteeing safety for every type and scenario your solution could be used with.
- Animats 9y agoThe simple way to do that would be to allocate an array, and use indices into the array rather than pointers/references. I've done that in code for a collision detection engine. The object descriptions for convex polyhedra have lists of faces, lists of edges, and lists of vertices, all referencing each other. The original implementation (I-Collide) actually used lists for them. When I re-implemented that in C++, I used arrays with indices for each of those. When you're done with a polyhedron, all those structures, which are owned by the Polyhedron object, go away more or less simultaneously.