I am implementing a loop to parse mpsc messages. It looks like this :
pub struct SimConnectHandler {
rx_to_simconnect: Receiver<ChannelMessage>,
}
impl SimConnectHandler {
/// Main thread function
pub fn run(&mut self) {
loop {
for msg in self.rx_to_simconnect.try_iter() {
match msg.message_type {
ListOfMessageTypes::SimStart => {
self.start();
}
_ => {}
}
}
}
}
fn start(&mut self) {
// do some stuff
}
}
I get an error from rustc because self.start() requires a mutable on self while self.tx_to_simconnect.try_iter() already has one on the self object.
The only way forward I found so far is something like this :
pub struct SimConnectHandler {
rx_to_simconnect: Receiver<ChannelMessage>,
}
impl SimConnectHandler {
/// Main thread function
pub fn run(&mut self) {
loop {
let messages: Vec<ChannelMessage> = Vec::new();
for msg in self.rx_to_simconnect.try_iter() {
messages.push(msg);
}
for msg in messages {
match msg.message_type {
ListOfMessageTypes::SimStart => {
self.start();
}
_ => {}
}
}
}
}
fn start(&mut self) {
// do some stuff
}
}
It works but the pattern with two consecutive loops doesn't feel satisfactory. It looks pretty much like a workaround, not a solution.
Is there a better way to handle such a situation ? Thanks !