This example in the docs is confusing to me, because I would expect the one shot future rx
to just get dropped if the "accept" loop every had a chance to start. Why is this not the case? Does select!
try more than one time? I have been putting thinking that I needed to put select!
into a loop in order to give the other "dropped/ cancelled" futures a chance to "compete" on the next cycle. But perhaps that looping / iterating is done internally somehow. Or maybe I am just misunderstanding this in some other way? Thanks!
################Tokio Docs on Select Below###################
Here, we select on a oneshot and accepting sockets from a TcpListener
.
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let (tx, rx) = oneshot::channel();
tokio::spawn(async move {
tx.send(()).unwrap();
});
let mut listener = TcpListener::bind("localhost:3465").await?;
tokio::select! {
_ = async {
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move { process(socket) });
}
// Help the rust type inferencer out
Ok::<_, io::Error>(())
} => {}
_ = rx => {
println!("terminating accept loop");
}
}
Ok(())
}
The accept loop runs until an error is encountered or rx
receives a value. The _
pattern indicates that we have no interest in the return value of the async computation.