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

Array view instead of direct wasm memory access #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mike-kfed
Copy link

Talking on discord I've learned that directly accessing wasm's linear memory to efficiently share data between the universes is not necessary anymore, because js_sys now has a view method for JavaScripts typed Arrays that does exactly that. To help other coders know about that feature, and more obviously show that this is unsafe code potentially, I adapted the code to use the view instead.

self.cells.as_ptr()
pub fn cells(&self) -> js_sys::Uint8Array {
unsafe {
let u8_cells = mem::transmute::<&Vec<Cell>, &Vec<u8>>(&self.cells);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can avoid transmute here and instead cast the slice to pointer, convert the pointer, and construct a new slice from that:

    let u8_cells: &[u8] = unsafe {
        let ptr = self.cells.as_ptr() as *const u8;
        let bytelen = self.cells.len() * std::mem::size_of::<Cell>();
        std::slice::from_raw_parts(ptr, bytelen)
    };

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=98b83b545fce22aec8fdf4a5e2ed5c55

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

Successfully merging this pull request may close these issues.

2 participants