Channel for sending non-Send types on the same thread

When using WebAssembly in the browser, one works with a single thread that can concurrently execute asynchronous tasks using the browsers event loop. The future's crate provides most of the functionality I need for these sort of applications, however, I often find I want to move a wasm_bindgen::JsValue to a different async task (on the same thread). To the best of my knowledge, this is both safe and correct and should be not be a problem, however, channels like oneshot and mpsc in the futures crate require Send so they can be used in multi-threaded environments.

Is there a way to workaround this problem or do I have to find a different implementation of these channels that allows for sending !Send types on the same thread?

No, they don't require Send. They only require Send if you use them in multi-threaded (or potentially multi-threaded[1]) environments.

The only Send requirement for the item type that you'll find in these channel APIs is for the Send or Sync implementations of the Sender and Receiver types themselves.


  1. for example, the tokio runtime conservatively always requires Send for spawning tasks, even when running in single-thread mode ↩︎

1 Like

You are right, I set the channel type to BoxFuture instead of LocalBoxFuture which is where the Send requirement came from. I should read the error messages more carefully.

6 Likes

Thanks for sharing the underlying issue you ended up having, maybe it can help others in the future.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.