3 ms·
This is interesting. If I want my object to be movable without the need for acquiring the locks for all outbound references, I need my references to be of type
by sesm 2y ago
This is interesting. If I want my object to be movable without the need for acquiring the locks for all outbound references, I need my references to be of type 'reference to mutable data'. So that could be one of the reasons why Rust reference types are like this.
But what happens to inbound references when I'm moving my object? Who is in charge of updating them?
- steveklabnik 2y agoYou get a compile time failure. There is no mechanism to automatically update them.
- sesm 2y agoOk, I see now. This is limited to cases when compiler can prove that the reference is not shared, but still can be useful when adding elements to collections.
- steveklabnik 2y agoYeah, among other things. It can be used to implement “session types” which basically means encoding a state machine in the type system. So for example, I’m building a web application at work, and users have permissions to access resources. The “please grab a foo from the database” function takes an Authorization as a parameter by move, so in order to call it I have to get one. The only way to get one is to ask the auth subsystem, which checks if a User has the proper permissions. And the only way to get a User is to ask the auth system to authenticate that they have a valid session. So as long as I make my “fetch resources” code require an Authorization as a parameter in the future, I’ll also guarantee that all of the rest of that stuff happens. It’s a little verbose but pretty cool.