Skip to content
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

Mutability in web-sys #1061

Closed
dbrgn opened this issue Nov 29, 2018 · 3 comments
Closed

Mutability in web-sys #1061

dbrgn opened this issue Nov 29, 2018 · 3 comments

Comments

@dbrgn
Copy link
Contributor

dbrgn commented Nov 29, 2018

I noticed that some methods on Range (e.g. set_start_before) accept a &self reference, even though the state on the range is changed by that method. Shouldn't those require a mutable reference? Or are all DOM objects treated as having interior mutability?

@alexcrichton
Copy link
Contributor

A good question! Currently everything takes &self because everything in JS can have multiple references and effectively has interior mutability. There's no need for &mut self in Rust as we can't really do anything with the uniqueness guarantees! All JS objects are !Send and !Sync

@dbrgn
Copy link
Contributor Author

dbrgn commented Nov 29, 2018

Thanks! Then I guess multiple threads/workers could simply fetch multiple references to the same object and mutate it in parallel, without data races happening. Feels strange to modify a non-mut object in a Rust code base, but you're right that it doesn't make sense to limit borrowing if it's not necessary 🙂

@Pauan
Copy link
Contributor

Pauan commented Nov 30, 2018

@dbrgn We could use &mut instead, but that doesn't buy us anything, since even though Rust can guarantee a single writer (in Rust), the JavaScript code can still write to the object, which means there are multiple writers!

In other words, because Rust cannot guarantee that JS won't write to the object, using &mut would basically be a lie.

As for multiple threads/workers, that's a much trickier situation, which we haven't fully figured out yet. It might involve a JsValueSync wrapper (or similar) which uses atomic synchronization between workers to manage a JsValue (which is stored in the thread that created it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants