What would be a good Rusty way to implement a data structure providing "handles" to its elements to allow their later manipulation? A prime example would be a binary heap implementation where you want to decrease element keys and remove elements.
(My particular motivation are Fibonacci and other heaps, e.g. for Dijkstra's algorithm.)
Unlike this related question I do not mind writing unsafe code as long as I can provide a safe interface with safe element references.
I can think of several directions:
Index the elements of the data structure by some unique key and always access it via this key. In a heap, this means an additional structure. This also complicates the internal structure quite a bit.
Wrap every node in an
Rc
, using weak refs for parent pointers. Every "element handle" returned is actually a wrappedRc
. This can be completely safe Rust, but I do not like this solution for all the runtime checks and using refcounting everywhere not being a really good design.Return handles with references or pointers lifetime-bound to the main structure. This option would likely not allow deleting individual elements before dropping the main structure. I am not sure about the details or options.
I would like to ask for help with the last option, or more generally if there are any other approaches I am missing. I know that "you can not have everything" ... I am asking for the right and most Rusty approach here.