rustwasm / book

The Rust and WebAssembly Book
https://rustwasm.github.io/docs/book/
MIT License
1.75k stars 211 forks source link

cannot return a borrowed ref with #[wasm_bindgen] #264

Open bkinsey808 opened 3 years ago

bkinsey808 commented 3 years ago

Describe the bug cannot return a borrowed ref with #[wasm_bindgen]

$ wasm-pack --version
wasm-pack 0.10.1
$ rustc --version
rustc 1.55.0 (c8dfcfe04 2021-09-06)
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

I'm in a WSL2 environment if that makes any difference

PS C:\Users\bkinsey> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  docker-desktop-data    Stopped         2
  docker-desktop         Stopped         2

To Reproduce Steps to reproduce the behavior: paste the code from https://rustwasm.github.io/docs/book/game-of-life/testing.html

    /// Get the dead and alive values of the entire universe.
    pub fn get_cells(&self) -> &[Cell] {
        &self.cells
    }

See the error: cannot return a borrowed ref with #[wasm_bindgen]

$ wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling wasm-game-of-life v0.1.0 (/home/bkinsey/bkinsey808/rust/wasm-game-of-life)
error: cannot return a borrowed ref with #[wasm_bindgen]
   --> src/lib.rs:115:32
    |
115 |     pub fn get_cells(&self) -> &[Cell] {
    |                                ^^^^^^^

error: could not compile `wasm-game-of-life` due to previous error
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

Expected behavior Should not have error

Screenshots image

cramhead commented 2 years ago

I got the same error. I've tried &self.cells.as_slice(); and &self.cells.as_slice().clone();, but neither worked.

jdrinane commented 2 years ago

Rust-generated WebAssembly functions cannot return borrowed references. Just remove the #[wasm_bindgen] attribute.

houseofmercy-github commented 2 years ago

@jdrinane thank you for this. I'm doing this tutorial now. On https://rustwasm.github.io/docs/book/game-of-life/testing.html it is very easy to miss the part where it says

We are going to create another impl Universe block inside our wasm_game_of_life/src/lib.rs file without the #[wasm_bindgen] attribute.

just before it tells you to add the get_cells and set_cells methods...

This worked for me:

// critical this block doesn't have wasm_bindgen
impl Universe {

    pub fn get_cells(&self) -> &[Cell] {
        &self.cells
    }

    pub fn set_cells(&mut self, cells: &[(u32, u32)]) {
        for (row, col) in cells.iter().cloned() {
            let idx = self.get_index(row, col);
            self.cells[idx] = Live;
        }
    }
}

#[wasm_bindgen] 
impl Universe {

    pub fn set_width(&mut self, width: u32) {
        self.width = width;
        self.cells = (0..width * self.height).map(|_i| Dead).collect();
    }
   ... 
jdrinane commented 2 years ago

@fitzgen Think you could close this now