This is a bit out of regular Rust usage, so I understand if some of this things are not possible to be expressed using Rust's type system.
I'm working on a "process" spawning library that will be used by Rust applications that compile to WebAssembly and run on a special WASM runtime. This runtime allows you to crate "processes" with a separate memory. I would like to allow spawning new "processes" just by passing a closure, but currently run into problems when a closure capture a non-copy type.
To better illustrate what I'm doing here is an example showing how code looks using my library:
fn main() {
let m = vec![42];
println!("Hello world {:?}", m);
spawn(|| {
println!("Hello world {:?}", m);
});
}
The signature of spawn looks like this:
pub fn spawn<F>(f: F) where F: FnOnce() + Copy
I want this code to fail when compiling, because it's not valid in my runtime. Here is the output of it:
Hello world [42]
Hello world []
The other "process" prints out an empty array, because it has a completely different memory space than the main "process".
If the closure captures the vector m
it should not be Copy
, but why does it still compile?
Eventually I would like to add a send()
function accepting Copy
types that is able to move data to other processes. Capturing Copy
types would be ok in this case.
Also, am I overlooking something else here that may cause me problems later down the road?