So I have been kind of stuck in my project where I am trying to port C code to Rust.
What appears to be very simple in C, seems impossible to do in Rust in the same way.
To recap:
My C project uses an allocator to dish out objects.
These objects are referred to in many other objects . I as a developer know that the lifetimes of these objects are all shorter than the allocator lifetime, so there is no problem with these memory references.
Additionally I need unique instances of objects - for example a string object only occurs once for a given string; I use Hash Sets to maintain unique sets of objects.
It seems that there is no way for me to implement this in Rust and convince the compiler that the code is safe ... that seems to be the gist of the feedback in this thread.
So I would like to implement following alternative.
I want to use regular Rust libraries and store each unique string in a Set. Then I would like to return an id that is not a reference to the object - but just an integer value. But I also need a way to map the id back to the object - so I am proposing to use a vector for that. In other words:
Given a string, find a unique String Object in the set that matches that string.
If not found, create a new String Object, store it in the set, assign it a unique id by appending it to a vector.
When presented the id, use the vector to map back to the object.
As far as I can tell since the object appears in two collections, I can use Rc to wrap it.
Is there a better way?
Regards
p.s. I could just use the reference counted object instead of an id I suppose. The id approach seems better in that it avoid having a fat reference everywhere.