How to Copy a Type and Send it Somewhere Even if it is a Pointer

I'm trying to implement a blocking work pool based on web workers for Rust when targeting WASM. So far I've got the following API for spawning tasks:

async fn spawn<D, R>(function: fn(D) -> R, data: D) -> R
        D: Send + Clone + 'static,
        R: Send + Clone + 'static;

I've gotten it working conceptually, except for a big 'ol facepalm I hit just now when I realized that if D is a String it is actually a pointer type, and even though I am copying the bytes of the pointer when sending it to the web worker I'm not copying the data that the pointer points to!


I already feel that the answer is "no", but is there any way to somehow copy the pointer data and the data it points to for reasonably any sort of type? There can't be can there?

Now this is seeming like a bad idea. The whole time I was testing this I was testing it with Copy types on the stack and thinking, "ah, it's working".

Ah, maybe I could create IntoBytes and FromBytes traits and then just require D to implement those. Then I'll just implement IntoBytes an FromBytes for Vec<u8> and String and anything else I need manually.

Edit: Nevermind, I can't feasibly implement IntoBytes for types in external traits that have pointers in private fields, which I need to in my case.

That looks like it shuts down my use-case. :disappointed:

Oh, well, I supposed real concurrency in Rust on web really does need shared memory. I was trying to avoid that so that I could target Safari.

I'm not at all familiar with web workers or WASM, but maybe serialization is the answer you're looking for? E.g. with serde you would have the trait bound Serialize + Deserialize which should be implemented for standard types, and which you can derive for you own.


Yeah, that makes the most sense.