This won't be possible to do safely.
The Send
bound is for transferring a value to another thread and a core principle of JavaScript is that it is single-threaded. That means no objects from JavaScript will implement Send
.
Of course, you can probably work under the assumption that objects from your WebAssembly code will only ever stay on the main JavaScript thread, so it's fine to create a wrapper with an unsafe impl Send
. However that unsafe
assumption is now your responsibility to maintain.
struct Sendable<T>(pub T);
// Safety: WebAssembly will only ever run in a single-threaded context.
unsafe impl<T> Send for Sendable<T> {}
For example, if you decided you'd like to implement multi-threaded WebAssembly by spinning up web workers and using a common SharedArrayBuffer
for their backing memory, then you'd need to go back and rethink whether your wrapper is still valid.