For example, I have a struct with Deref implemented
use std::ops::{Deref, DerefMut, IndexMut};
#[derive(Debug)]
struct Selector<T> {
elements: Vec<T>,
current: usize
}
impl<T> Deref for Selector<T> {
type Target = T;
fn deref(&self) -> &T {
&self.elements[self.current]
}
}
impl<T> DerefMut for Selector<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.elements[self.current]
}
}
Then I do *s = 'w';
This means that rust is doing &s.elements[s.current] = 'w'
which is the same as s.elements.index_mut(s.current) = 'w';
except index_mut returns a reference and you can't assign a reference to something so I changed the above to *&mut *s.elements.index_mut(s.current) = 'w';
So *s is the same as *s.deref_mut() where deref_mut is my implementation of Deref. So this leads me to think that *s is more like **s, where the first * calls my deref_mut method and the second * turns the resulting &mut T into mut T. Is rust adding another deref method after my implementation of Deref because deref_mut returns a reference? If so what is this called?
Does this mean that * and the deref method are different where one follows a pointer to data and the other one allows you to do something to a reference and then return another reference? Is rust implicitly inserting another * to convert the reference?