diff options
| author | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2023-02-21 00:00:00 +0000 |
|---|---|---|
| committer | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2023-03-06 12:24:15 +0100 |
| commit | defa2456246a8272ceace9c1cdccdf2e4c36175e (patch) | |
| tree | 707a262f47a0659bdfe1d6698ac8ec99019c89b4 /library/std/src/process/tests.rs | |
| parent | 0fbfc3e76916521b509b63286296dd0762170d34 (diff) | |
| download | rust-defa2456246a8272ceace9c1cdccdf2e4c36175e.tar.gz rust-defa2456246a8272ceace9c1cdccdf2e4c36175e.zip | |
Implement read_buf for a few more types
Implement read_buf for TcpStream, Stdin, StdinLock, ChildStdout, ChildStderr (and internally for AnonPipe, Handle, Socket), so that it skips buffer initialization. The other provided methods like read_to_string and read_to_end are implemented in terms of read_buf and so benefit from the optimization as well. This commit also implements read_vectored and is_read_vectored where applicable.
Diffstat (limited to 'library/std/src/process/tests.rs')
| -rw-r--r-- | library/std/src/process/tests.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index b4f6cc2daba..d7f4d335de3 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -1,7 +1,8 @@ use crate::io::prelude::*; use super::{Command, Output, Stdio}; -use crate::io::ErrorKind; +use crate::io::{BorrowedBuf, ErrorKind}; +use crate::mem::MaybeUninit; use crate::str; fn known_command() -> Command { @@ -121,6 +122,37 @@ fn stdin_works() { #[test] #[cfg_attr(any(target_os = "vxworks"), ignore)] +fn child_stdout_read_buf() { + let mut cmd = if cfg!(target_os = "windows") { + let mut cmd = Command::new("cmd"); + cmd.arg("/C").arg("echo abc"); + cmd + } else { + let mut cmd = shell_cmd(); + cmd.arg("-c").arg("echo abc"); + cmd + }; + cmd.stdin(Stdio::null()); + cmd.stdout(Stdio::piped()); + let child = cmd.spawn().unwrap(); + + let mut stdout = child.stdout.unwrap(); + let mut buf: [MaybeUninit<u8>; 128] = MaybeUninit::uninit_array(); + let mut buf = BorrowedBuf::from(buf.as_mut_slice()); + stdout.read_buf(buf.unfilled()).unwrap(); + + // ChildStdout::read_buf should omit buffer initialization. + if cfg!(target_os = "windows") { + assert_eq!(buf.filled(), b"abc\r\n"); + assert_eq!(buf.init_len(), 5); + } else { + assert_eq!(buf.filled(), b"abc\n"); + assert_eq!(buf.init_len(), 4); + }; +} + +#[test] +#[cfg_attr(any(target_os = "vxworks"), ignore)] fn test_process_status() { let mut status = if cfg!(target_os = "windows") { Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap() |
