I'm not entirely sure what you mean, but if you just want to end a loop once a oneshot::Receiver has received its message, you can use try_recv to check for a value without waiting for it.
use tokio::sync::oneshot;
fn server(signal: oneshot::Receiver<()>) {
let mut server = create_server();
loop {
// do something...
if signal.try_recv().is_ok() { break }
}
server.cleanup();
}
If this doesn't work, please post a concrete code example that can reproduce the error you get, or at least the error message. (not whatever your editor shows you. Run cargo check in the terminal and paste the full error here.)
You probably should be using tokio::sync::watch::channel to construct the Sender/Receiver pair. As the documentation states, that is a multi-producer, multi-consumer channel; consequently, both Sender and Receiver implement Clone as needed by your code. You can use the Sender outside of start_a_server to Sender::send a message to allcloned Receivers, and then that same Sender can be Sender::closed to wait for each Receiver to be dropped. Each Receiver can wait for a signal via Receiver::changed. You will likely want to use tokio::select! or equivalent simple hand-rolled Future that polls the Futures returned by Receiver::changed and server.recv.
For example, let's say you simply want to handle a termination signal to gracefully shutdown the server. Then you can use () as the message type that is sent from Sender to Receiver.
what I meant was, if you find CancellationToken not usable because you need to cancel multiple times, then you will NOT be able to use a oneshot channel either, for the same reason, because oneshot channels are even more limited than CancellationToken.
Because taking ownership of a mutable reference does not destroy the original variable. Using &mut rx is error-prone since it can lead to panics when you use a future after it has completed, so that is why it is not the default.
Okay so a mut reference will consume it like ownership, but it can be called multiple times when not ready, and it has no idea weather it's been consumed, am I right?