From 3b550f5d3dad77a56455352579fae3071b42e86d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 29 Dec 2021 02:55:24 -0500 Subject: wip another complete refactor --- examples/async-std.rs | 19 ++++++++++--------- examples/basic.rs | 18 ++++++------------ examples/interhack.rs | 23 +++++++++-------------- examples/smol.rs | 19 ++++++++++--------- examples/tokio.rs | 31 ++++++++++++++++--------------- 5 files changed, 51 insertions(+), 59 deletions(-) (limited to 'examples') diff --git a/examples/async-std.rs b/examples/async-std.rs index 880c91b..6bf9412 100644 --- a/examples/async-std.rs +++ b/examples/async-std.rs @@ -1,13 +1,13 @@ mod raw_guard; -#[cfg(feature = "backend-async-std")] +#[cfg(feature = "async")] mod main { use async_std::io::prelude::WriteExt as _; use async_std::io::ReadExt as _; use async_std::prelude::FutureExt as _; pub async fn run( - child: &mut pty_process::async_std::Child, + child: &pty_process::Child, ) -> std::result::Result<(), Box> { let _raw = super::raw_guard::RawGuard::new(); @@ -55,17 +55,18 @@ mod main { } } -#[cfg(feature = "backend-async-std")] +#[cfg(feature = "async")] fn main() { use std::os::unix::process::ExitStatusExt as _; let status = async_std::task::block_on(async { - let pty = pty_process::async_std::Pty::new().unwrap(); + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::async_std::Command::new("tac"); - // cmd.args(&["500"]); - let mut child = cmd.spawn(pty).unwrap(); - main::run(&mut child).await.unwrap(); + let mut child = pty_process::Command::new("tac") + // .args(&["500"]) + .spawn(pty) + .unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -75,7 +76,7 @@ fn main() { ); } -#[cfg(not(feature = "backend-async-std"))] +#[cfg(not(feature = "async"))] fn main() { unimplemented!() } diff --git a/examples/basic.rs b/examples/basic.rs index 1e5913b..c9b5f6c 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,11 +1,10 @@ mod raw_guard; -#[cfg(feature = "backend-std")] mod main { use std::io::{Read as _, Write as _}; use std::os::unix::io::AsRawFd as _; - pub fn run(child: &mut pty_process::std::Child) { + pub fn run(child: &mut pty_process::blocking::Child) { let _raw = super::raw_guard::RawGuard::new(); let mut buf = [0_u8; 4096]; let pty = child.pty().as_raw_fd(); @@ -70,15 +69,15 @@ mod main { } } -#[cfg(feature = "backend-std")] fn main() { use std::os::unix::process::ExitStatusExt as _; - let pty = pty_process::std::Pty::new().unwrap(); + let pty = pty_process::blocking::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::std::Command::new("tac"); - // cmd.args(&["500"]); - let mut child = cmd.spawn(pty).unwrap(); + let mut child = pty_process::blocking::Command::new("tac") + // .args(&["500"]) + .spawn(pty) + .unwrap(); main::run(&mut child); @@ -89,8 +88,3 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } - -#[cfg(not(feature = "backend-std"))] -fn main() { - unimplemented!() -} diff --git a/examples/interhack.rs b/examples/interhack.rs index e636282..9d44bbf 100644 --- a/examples/interhack.rs +++ b/examples/interhack.rs @@ -1,11 +1,11 @@ mod raw_guard; -#[cfg(feature = "backend-smol")] +#[cfg(feature = "async")] mod main { use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; pub async fn run( - child: &mut pty_process::smol::Child, + child: &pty_process::Child, ) -> std::result::Result<(), Box> { let _raw = super::raw_guard::RawGuard::new(); @@ -71,11 +71,7 @@ mod main { 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); - } + eprintln!("pty read failed: {:?}", e); break; } } @@ -93,7 +89,7 @@ mod main { } } -#[cfg(feature = "backend-smol")] +#[cfg(feature = "async")] fn main() { use std::os::unix::process::ExitStatusExt as _; @@ -103,12 +99,11 @@ fn main() { (80, 24) }; let status = smol::block_on(async { - let pty = pty_process::smol::Pty::new().unwrap(); + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(h, w)).unwrap(); - let mut child = pty_process::smol::Command::new("nethack") - .spawn(pty) - .unwrap(); - main::run(&mut child).await.unwrap(); + let mut child = + pty_process::Command::new("nethack").spawn(pty).unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -118,7 +113,7 @@ fn main() { ); } -#[cfg(not(feature = "backend-smol"))] +#[cfg(not(feature = "async"))] fn main() { unimplemented!() } diff --git a/examples/smol.rs b/examples/smol.rs index b0b90e4..e8b1c4c 100644 --- a/examples/smol.rs +++ b/examples/smol.rs @@ -1,11 +1,11 @@ mod raw_guard; -#[cfg(feature = "backend-smol")] +#[cfg(feature = "async")] mod main { use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; pub async fn run( - child: &mut pty_process::smol::Child, + child: &pty_process::Child, ) -> std::result::Result<(), Box> { let _raw = super::raw_guard::RawGuard::new(); @@ -54,17 +54,18 @@ mod main { } } -#[cfg(feature = "backend-smol")] +#[cfg(feature = "async")] fn main() { use std::os::unix::process::ExitStatusExt as _; let status = smol::block_on(async { - let pty = pty_process::smol::Pty::new().unwrap(); + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::smol::Command::new("tac"); - // cmd.args(&["500"]); - let mut child = cmd.spawn(pty).unwrap(); - main::run(&mut child).await.unwrap(); + let mut child = pty_process::Command::new("tac") + // .args(&["500"]) + .spawn(pty) + .unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -74,7 +75,7 @@ fn main() { ); } -#[cfg(not(feature = "backend-smol"))] +#[cfg(not(feature = "async"))] fn main() { unimplemented!() } diff --git a/examples/tokio.rs b/examples/tokio.rs index bc62592..16083c7 100644 --- a/examples/tokio.rs +++ b/examples/tokio.rs @@ -1,11 +1,12 @@ mod raw_guard; -#[cfg(feature = "backend-tokio")] +#[cfg(feature = "async")] mod main { use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; + use tokio_util::compat::FuturesAsyncReadCompatExt as _; pub async fn run( - child: &mut pty_process::tokio::Child, + child: &pty_process::Child, ) -> std::result::Result<(), Box> { let _raw = super::raw_guard::RawGuard::new(); @@ -16,17 +17,18 @@ mod main { let mut stdout = tokio::io::stdout(); loop { + let mut pty = child.pty().compat(); tokio::select! { bytes = stdin.read(&mut in_buf) => match bytes { Ok(bytes) => { - child.pty_mut().write_all(&in_buf[..bytes]).await.unwrap(); + pty.write_all(&in_buf[..bytes]).await.unwrap(); } Err(e) => { eprintln!("stdin read failed: {:?}", e); break; } }, - bytes = child.pty_mut().read(&mut out_buf) => match bytes { + bytes = pty.read(&mut out_buf) => match bytes { Ok(bytes) => { stdout.write_all(&out_buf[..bytes]).await.unwrap(); stdout.flush().await.unwrap(); @@ -36,9 +38,7 @@ mod main { break; } }, - // _ = child.status_no_drop() => { - // break; - // } + _ = child.status_no_drop() => break, } } @@ -46,18 +46,19 @@ mod main { } } -#[cfg(feature = "backend-tokio")] +#[cfg(feature = "async")] #[tokio::main] async fn main() { use std::os::unix::process::ExitStatusExt as _; - let pty = pty_process::tokio::Pty::new().unwrap(); + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::tokio::Command::new("tac"); - // cmd.args(&["500"]); - let mut child = cmd.spawn(pty).unwrap(); - main::run(&mut child).await.unwrap(); - let status = child.wait().await.unwrap(); + let mut child = pty_process::Command::new("tac") + // .args(&["500"]) + .spawn(pty) + .unwrap(); + main::run(&child).await.unwrap(); + let status = child.status().await.unwrap(); std::process::exit( status .code() @@ -65,7 +66,7 @@ async fn main() { ); } -#[cfg(not(feature = "backend-tokio"))] +#[cfg(not(feature = "async"))] fn main() { unimplemented!() } -- cgit v1.2.3-54-g00ecf