| 1 | #![cfg(feature = "tokio")] |
| 2 | |
| 3 | use io_process::{ |
| 4 | command::Command, |
| 5 | coroutines::spawn_then_wait::{SpawnThenWait, SpawnThenWaitResult}, |
| 6 | runtimes::tokio::handle, |
| 7 | }; |
| 8 | use tempfile::tempdir; |
| 9 | |
| 10 | #[tokio::main] |
| 11 | async fn main() { |
| 12 | env_logger::init(); |
| 13 | |
| 14 | let workdir = tempdir().unwrap(); |
| 15 | |
| 16 | let mut command = Command::new("touch"); |
| 17 | command.arg(workdir.path().join("file.tmp").to_string_lossy()); |
| 18 | |
| 19 | println!("spawn: {command:#?}"); |
| 20 | println!(); |
| 21 | |
| 22 | let mut arg = None; |
| 23 | let mut spawn = SpawnThenWait::new(command); |
| 24 | |
| 25 | let status = loop { |
| 26 | match spawn.resume(arg.take()) { |
| 27 | SpawnThenWaitResult::Ok(output) => break output, |
| 28 | SpawnThenWaitResult::Io(io) => arg = Some(handle(io).await.unwrap()), |
| 29 | SpawnThenWaitResult::Err(err) => panic!("{err}"), |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | println!("exit status: {status:#?}") |
| 34 | } |