From e9a976fba8da44c298756e189180d7fe061fe7ba Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 6 Mar 2021 01:13:42 -0500 Subject: make plain `cargo test` work --- examples/smol.rs | 94 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 42 deletions(-) (limited to 'examples/smol.rs') diff --git a/examples/smol.rs b/examples/smol.rs index d7b816f..d519fbe 100644 --- a/examples/smol.rs +++ b/examples/smol.rs @@ -1,64 +1,69 @@ -use pty_process::Command as _; -use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; -use std::os::unix::process::ExitStatusExt as _; - mod raw_guard; -async fn run( - child: &pty_process::smol::Child, -) -> std::result::Result<(), Box> { - let _raw = raw_guard::RawGuard::new(); +#[cfg(feature = "backend-smol")] +mod main { + use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; - let ex = smol::Executor::new(); + pub async fn run( + child: &pty_process::smol::Child, + ) -> std::result::Result<(), Box> { + let _raw = super::raw_guard::RawGuard::new(); - let input = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdin = smol::Unblock::new(std::io::stdin()); - loop { - match stdin.read(&mut buf).await { - Ok(bytes) => { - child.pty().write_all(&buf[..bytes]).await.unwrap(); - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; + let ex = smol::Executor::new(); + + let input = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdin = smol::Unblock::new(std::io::stdin()); + loop { + match stdin.read(&mut buf).await { + Ok(bytes) => { + child.pty().write_all(&buf[..bytes]).await.unwrap(); + } + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; + } } } - } - }); - let output = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdout = smol::Unblock::new(std::io::stdout()); - loop { - match child.pty().read(&mut buf).await { - Ok(bytes) => { - stdout.write_all(&buf[..bytes]).await.unwrap(); - stdout.flush().await.unwrap(); - } - Err(e) => { - // EIO means that the process closed the other - // end of the pty - if e.raw_os_error() != Some(libc::EIO) { - eprintln!("pty read failed: {:?}", e); + }); + let output = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdout = smol::Unblock::new(std::io::stdout()); + loop { + match child.pty().read(&mut buf).await { + Ok(bytes) => { + stdout.write_all(&buf[..bytes]).await.unwrap(); + stdout.flush().await.unwrap(); + } + Err(e) => { + // EIO means that the process closed the other + // end of the pty + if e.raw_os_error() != Some(libc::EIO) { + eprintln!("pty read failed: {:?}", e); + } + break; } - break; } } - } - }); + }); - ex.run(smol::future::or(input, output)).await; + ex.run(smol::future::or(input, output)).await; - Ok(()) + Ok(()) + } } +#[cfg(feature = "backend-smol")] fn main() { + use pty_process::Command as _; + use std::os::unix::process::ExitStatusExt as _; + let status = smol::block_on(async { let mut child = smol::process::Command::new("sleep") .args(&["500"]) .spawn_pty(Some(&pty_process::Size::new(24, 80))) .unwrap(); - run(&child).await.unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -67,3 +72,8 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-smol"))] +fn main() { + unimplemented!() +} -- cgit v1.2.3-54-g00ecf