You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is in a similar vein to #1338. To run the example
yarn run build
This runs correctly, nothing is wrong.
yarn run x
This mutably borrows data twice, something is wrong.
yarn run x2
Expected behavior: I'm not sure. More on this later.
Actual behavior: In yarn run x we can see the code behaving in a normal and intended way. In x2 however, the tokio runtime has two mutable borrows to the same data, which results in undefined and unsound behavior. This is not allowed per Chapter 4 section 2 of the Rust Book.
So what should we do about this? I have some ideas with varying trade offs.
Forbid &mut _ in async#[napi] methods.
Pros: JavaScript never has to think about Rust borrow rules and restrictions
Cons: Rust users can't use &mut _ with async functions anymore.
Use RefCell or something like it to dynamically check borrow rules.
Pros: We can keep writing async functions with &mut _ arguments.
Cons: JavaScript users are now exposed to the restrictions of Rust and all that entails in a likely surprising way.
My personal preference is for option 1.
The text was updated successfully, but these errors were encountered:
Xaeroxe
added a commit
to Xaeroxe/napi-rs
that referenced
this issue
Nov 11, 2022
Example project: napi-mut-twice.zip
This is in a similar vein to #1338. To run the example
This runs correctly, nothing is wrong.
This mutably borrows data twice, something is wrong.
Expected behavior: I'm not sure. More on this later.
Actual behavior: In
yarn run x
we can see the code behaving in a normal and intended way. Inx2
however, thetokio
runtime has two mutable borrows to the same data, which results in undefined and unsound behavior. This is not allowed per Chapter 4 section 2 of the Rust Book.So what should we do about this? I have some ideas with varying trade offs.
Forbid
&mut _
inasync
#[napi]
methods.Pros: JavaScript never has to think about Rust borrow rules and restrictions
Cons: Rust users can't use
&mut _
withasync
functions anymore.Use RefCell or something like it to dynamically check borrow rules.
Pros: We can keep writing async functions with
&mut _
arguments.Cons: JavaScript users are now exposed to the restrictions of Rust and all that entails in a likely surprising way.
My personal preference is for option 1.
The text was updated successfully, but these errors were encountered: