Open bkinsey808 opened 3 years ago
I got the same error. I've tried &self.cells.as_slice();
and &self.cells.as_slice().clone();
, but neither worked.
Rust-generated WebAssembly functions cannot return borrowed references. Just remove the #[wasm_bindgen] attribute.
@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();
}
...
@fitzgen Think you could close this now
Describe the bug cannot return a borrowed ref with #[wasm_bindgen]
I'm in a WSL2 environment if that makes any difference
To Reproduce Steps to reproduce the behavior: paste the code from https://rustwasm.github.io/docs/book/game-of-life/testing.html
See the error:
cannot return a borrowed ref with #[wasm_bindgen]
Expected behavior Should not have error
Screenshots