I'm trying to build my first Rust project and got stuck with this issue.
Basically I have a singleton server instance (implemented as a struct) which has unique access to some external IO device. There are multiple clients in different threads submitting request to the IO device (through the server) and waiting for responses.
Ideally I see this as the server having a single queue where clients would use and server taking requests from the queue one-by-one and executing them. Clients upon submitting a request would get back a Future which resolves once there's a response.
server struct is not thread safe so I can't just pass it around in a mutex and this is where my knowledge of Rust ecosystem/idioms ends. I'd really appreciate any hints/links/examples of how something like the above can be implemented.
Such things are usually solved using channels. You'd have your server on its own thread, listening for requests from a channel, and execute them one by one. Channels are uni-directional, so it's up to you how you send the response back. The request may contain another channel, or sink, or just be a callback function.
Thanks kornel!
Actually, how to communicate back the response is the main issue for me atm.
Especially it is unclear how to do this in the async/await style, and if it makes sense to do it this way at all.
If you want to send a message of type M with a response of type R, then your message should be a (M, Sender<R>) where the Sender is an oneshot channel. This way the receiver can respond on the included channel.