I did a bit of searching, and found this Reddit post from four years ago, but other than unsafe code:
fn find_mut <'a> (&'a mut self, elem: &T) -> Option<&'a mut Node<T>> {
unsafe{std::mem::transmute(self.find(elem))}
}
or a macro, I can't think of any ways to do this.
Alternative restatements of the problem:
- Make a function generic over mutability.
- Mutability modifier polymorphism.
Is there a way to do this now?
Motivation, just in case this is another XY problem: I'd like to write a function that processes a vector of references, reading them but not modifying them, and I would like to use those functions both in cases where I will have &T
s and &mut T
s.