async-book icon indicating copy to clipboard operation
async-book copied to clipboard

Executors and System IO: self reference should be mutable

Open uint opened this issue 4 years ago • 2 comments

The Executors and System IO chapter includes a (pseudo-)code snippet that outlines how an IO blocking primitive is generally structured. This is minor, but shouldn't the self reference this function takes be mutable?

    /// Express an interest in a particular IO event.
    fn add_io_event_interest(
        &self,

        /// The object on which the event will occur
        io_object: &IoObject,

        /// A set of signals that may appear on the `io_object` for
        /// which an event should be triggered, paired with
        /// an ID to give to events that result from this interest.
        event: Event,
    ) { /* ... */ }

Later on in the same snippet, we specifically make the variable holding an IoBlocker mutable, presumably because it needs to be mutable to call add_io_event_interest on it.

let mut io_blocker = IoBlocker::new();
io_blocker.add_io_event_interest(
    &socket_1,
    Event { id: 1, signals: READABLE },
);
io_blocker.add_io_event_interest(
    &socket_2,
    Event { id: 2, signals: READABLE | WRITABLE },
);
let event = io_blocker.block();

uint avatar Oct 05 '20 09:10 uint

I guess that implies the use of interior mutability.

taiki-e avatar Oct 09 '20 06:10 taiki-e

@taiki-e I don't think so. Marking the variable that holds the "wrapper" type with mut isn't needed to take advantage of interior mutability.

Here's proof. Note this in particular:

    let foo = Foo::new("original");
    // ^-- no `mut` here

uint avatar Oct 09 '20 09:10 uint