Hi,
Got some questions and would appreciate some feedback on a possible solution to a problem I have!
Problem:
Taking two or more mutable references to separate elements within a vector at the same time is not possible. I need to do this with almost no overhead (very performance critical part of the code).
Other solutions I’ve come up with so far:
-
split_at_mut()
- however this seems to have non-trivial overhead (creating two slices and then calculating the correct indexes in the new slices). - Use raw pointers. Not very pretty though.
- Take immutable references from the vector and wrap the elements (or the fields in the elements) in Cells. Doesn’t feel idiomatic. Is there any overhead associated with using cells?
- Trying to take the references at different times (in different scopes). Tried this quite a bit, but didn’t work out. Could be solved by running all the relevant code in a 200 LOC function, but that isn’t very nice.
Dream solution:
Having a type that you give a mutable reference to the vector. This type keeps track of what indexes you’ve asked for and you can ask it for a mutable reference from the vector with try_get(index: usize) -> Option<T>
. If you already asked for that index, or the index is out of bounds, you get None
. Otherwise you get the mutable reference that you asked for. Then there could be a version of the function that returns T
directly and panicking if that index has already been used or if out of bounds, because in my case, the caller can guarantee that the indexes are unique, and this would eliminate some overhead.
Would this work? Wouldn’t this be (relatively) easy to implement as a separate library? Are there any improvements that could be made to the core language to make things like this even better?
What do you think about this in general? After all, this seems to be a common problem where there doesn’t seem to be any simple idiomatic solution. Would you recommend me on giving my “dream solution” a try?