Created
February 5, 2024 02:14
-
-
Save zbentley/d05696b1f6268372cf858b157c6d7740 to your computer and use it in GitHub Desktop.
Rust Heterogenous Select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tokio::io::AsyncReadExt; | |
use tokio::net::TcpStream; | |
use tokio::select; | |
use tokio::task::spawn_blocking; | |
use tokio::time::{sleep, Duration}; | |
async fn main() { | |
let mut stream = TcpStream::connect("...").await?; | |
let thread_handle = spawn_blocking(|| { | |
do_something(); | |
}); | |
select! { | |
_ = stream.read(buf) => (), | |
_ = sleep(Duration::from_secs(1)) => (), | |
_ = thread_handle => () | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment