I am learning how to target WASM with Rust through this tutorial. I want to be able to separate my domain code from the code that exposes it to WASM. That way, I could re-use the domain code in a non-WASM application with no fuss. I can't find any examples that do this, and I don't know that it's supported.
Right now, what I'm doing is wrapping my vanilla Rust struct with another struct that has wrappers around the domain class's public methods. I'm almost sure this is not the proper way to do it, but it works for now.
I want to be able to bind CellValue
to WASM.
// Inside a vanilla Rust library
// We could bind CellValue with #[wasm_bindgen],
// but I want to keep all WASM stuff out of this library
enum CellValue {
Live = 0,
Dead = 1
}
// ...
struct World {
// ...
cells: Vec<CellValue> // need to bind a direct reference to the values in here
}
This is how I am exposing World
to WASM - I'm wrapping it in GOL
and implementing methods in GOL
so that the WASM can interact with World
.
// Inside the wasm binding library
#[wasm_bindgen]
pub struct GOL {
world: gol::World
}
#[wasm_bindgen]
impl GOL {
pub fn make_new(rows: usize, cols: usize) -> Self
{
Self {
world: GameOfLife::make_new(rows, cols)
}
}
// and so on...
}
For CellValue
, I can't mimic the approach I took with GOL
because I need to be able to reference the data inside each cell that's held by World
.
Like I said, the whole reason I'm jumping through these hoops is so that I can avoid peppering my domain code with #[wasm_bindgen]
. Is it even possible to get this kind of binding?