Hi, I use tokio::select!
macro with broadcast::channel
and I send through this channel AppEvent
enum and got unexpected behavior for me:
#[tokio::main]
async fn main() {
let (tx, mut rx) = broadcast::channel(100);
tokio::spawn(async move {
loop {
let _ = sleep(Duration::from_secs(3));
tx.send(AppEvent::Message("HelloTokio".to_string())).expect("failed to send");
}
});
loop {
select! {
// This branch not executed and we get SendError panic
Ok(AppEvent::Message(msg)) = rx.recv() => {
println!("got message: {}", msg);
}
// BUT this work correctly and receive events
// Ok(e) = rx.recv() => {
// println!("got event: {:?}", e);
// }
else => {
println!("exit");
break;
},
}
}
}
#[derive(Debug, Clone)]
enum AppEvent {
Message(String),
}
All works great when we use pattern match in tokio::select!
like Ok(event) = rx.recv() => {}
but if we add nested resolve of enum value, like this Ok(AppEvent::Message(text)) = rx.recv() => {}
then rx
seems not recognize it and if we try tx.send(AppEvent::Message("Hello".to_string())).expect("fail")
we got panic SendError
.
Example:
Can you explain to me what the reason is? And is everything working as epected?