I’m trying to write Webassembly using Rust, I need some way of persisting data between calls from JavaScript. I was recommended using a static RefCell, which seems like a good idea, unfortunately it won’t compile.
use std::cell::RefCell;
struct Godobject{
data:u64,
}
static GODCELL:RefCell<Godobject>=RefCell::new(Godobject{data:0});
fn main(){
}
#[no_mangle]
pub fn count()->f64{
let mut god=GODCELL.borrow_mut();
(*god).data+=1;
(*god).data as f64
}
Gives me the following error:
error[E0277]: the trait bound `std::cell::RefCell<Godobject>: std::marker::Sync`
is not satisfied
--> wasmtest.rs:37:1
|
37 | static GODCELL:RefCell<Godobject>=RefCell::new(Godobject{data:0});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::ce
ll::RefCell<Godobject>` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `std::cell::RefC
ell<Godobject>`
= note: shared static variables must have a type that implements `Sync`
One can’t even use threads with Webassembly, so I guess I just need something to make the compiler shut up.
Alternatively, are there any better ways to make a persistent allocation with safe code?
Answers with working code much appreciated, I’m going nuts bouncing from one error message to the next.