Hello. I am using the
embedded-hal-async crate and I was surfing the source code of it when I faced an implementation of a trait for a reference of a type implementing this trait, that is:
impl<T: Trait> Trait for &mut T.
I understand why this sometimes can be useful. For example, it can be used when the trait methods consume the object, but you still want the trait to be implemented somehow for the reference of this object.
But what if all the methods of the trait already take a reference, specifically a mutable reference?
What is the point of such impl for &mut T?
That's exactly what I see in the source of of
embedded-hal-async crate. Here is the link.
All the methods of the trait
&mut self. So having a mutable reference to a type implementing
SpiDevice trait already allows me to call the methods of the trait.
However, further you see an implementation of the trait
SpiDevice for mutable references to types implementing trait
impl<Word: Copy + 'static, T: SpiDevice<Word> + ?Sized> SpiDevice<Word> for &mut T ), where all the implemented methods just call the methods of the implementation for
Why is this done? How this can be useful?