Why I can invoke method taking &mut self on Arc warpped data?

Line 211-213 in Rust Playground

Here I move Arc to closure and create a mutable variable to store the reference, and to call a method with &mut self input

future.chain(move |_| {
    poll_fn(move |_| {
        let mut connection = &*read_connection_ref;
        match connection.read(&mut request[read..]) {

It can be compiled, I don't understand.

io::{Read, Write} are implemented for &TcpStream. The same is true for fs::File. The reason why such an impl can exist is that these types are merely handles to some OS-level data structure (basically, no more than an int index into some file descriptor table), and they don't themselves need to be modified in order to be written to or read from.


If you're familiar with interior mutability, aka shared mutability, you can think of OS data types like TcpStream as types that use shared mutability to change their state, even when you only have a shared reference to them.[1]

  1. Whatever changes are necessary happen in the OS, not Rust code, but I find this a handy mental model. ↩ī¸Ž


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.