-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for borrowing during use if guaranteed immovability #2337
Comments
Mutations are still a problem even if it's immovable and single-threaded: |
Thank you very much for the article, good read. However, I would argue that the enum example that he talked about counts as moving so would not be allowed with such a borrow |
I'm not sure what you mean by that. If the "immovable mutable borrow" doesn't let you assign to a field of the borrowed thing, how is it mutable at all? |
It counts as moving if the change would invalidate a interator pointing at it. As to mutable, this is to be a restricted form so that so rust is easier to use is some use cases |
Isn't it possible for mutation to invalidate iterators even without moving? I think you could make it safe by assuming the iterator's referent contains no references of its own, but that rules out even the basic use case of iterating over collections. |
It is possible that some mutations invalidate iterators but not all of them. If you have a vec of items that includes a vec then pushing to that would not be allowed but writing to some i32 field would |
Motivation:
Currently you cannot borrow a value mutable why it is currently being borrowed. This is a core aspect of Rust and should not be removed, however, there are legitimate uses for wanting to do something like this and with the proposal this would become more easy to accomplish safely.
Example (of what is wanted to be done):
This was taken from here.
There are ways to work around this currently but generally it requires convoluted methods or extra allocations which are undesirable especially for people coming to rust.
A working example that leaves most of the algorithm the same has the following main, gotten from here:
Proposition:
A new type of borrowing, called
immovable borrow
which can be done at any time, even when another item has borrowed it. This would only allow mutations that are guaranteed not to require a move. These would only be allowed with mutable borrows. To my knowledge most actions on intrinsics are no-move but there may be others.This would be implemented by two item:
iter_mut_immov()
which would be for generated mutable immovable references for iterating through andimmov
which would be a keyword use in conjunction with&mut
(ie&mut immov
) for getting these sorts of borrows individually.An error that recommends this type of borrow would also be implemented.
This proposal does ask for adding a new keyword or at least a new contextual keyword (then it would be
&immov mut
)Example:
Using this proposition the following would be done:
The text was updated successfully, but these errors were encountered: