Trying to understand this from std::io::Read
:
impl Read for &[u8] {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// ... snip ...
}
It looks like it is only implemented for immutable slices, so how is calling read ever allowed? Self
is &[u8]
so how would you be allowed to make &mut self
? The documentation says that it changes the slice to be narrowed to refer to the remaining bytes, so I can see why it would need to change the slice itself, I just don't know how to reconcile how the mutability works here. As far as I can tell there are two things to worry about mutability of, the underlying bytes the slice refers to, and the position and length of the slice. How syntactically do I write the cross product of (slice field mutability) x (underlying byte mutability) ?
edit: ... or since Self
is a reference type then &mut self
is &mut &[u8]
?