Skip to content

Commit a06c664

Browse files
authored
Rollup merge of #89687 - Nicholas-Baron:move_read2_abbreviated, r=Mark-Simulacrum
Move `read2_abbreviated` function into read2.rs Work towards #89475.
2 parents 0481c67 + 8a4085d commit a06c664

File tree

2 files changed

+73
-70
lines changed

2 files changed

+73
-70
lines changed

src/tools/compiletest/src/read2.rs

+71
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,77 @@
22
// Consider unify the read2() in libstd, cargo and this to prevent further code duplication.
33

44
pub use self::imp::read2;
5+
use std::io;
6+
use std::process::{Child, Output};
7+
8+
pub fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
9+
use io::Write;
10+
use std::mem::replace;
11+
12+
const HEAD_LEN: usize = 160 * 1024;
13+
const TAIL_LEN: usize = 256 * 1024;
14+
15+
enum ProcOutput {
16+
Full(Vec<u8>),
17+
Abbreviated { head: Vec<u8>, skipped: usize, tail: Box<[u8]> },
18+
}
19+
20+
impl ProcOutput {
21+
fn extend(&mut self, data: &[u8]) {
22+
let new_self = match *self {
23+
ProcOutput::Full(ref mut bytes) => {
24+
bytes.extend_from_slice(data);
25+
let new_len = bytes.len();
26+
if new_len <= HEAD_LEN + TAIL_LEN {
27+
return;
28+
}
29+
let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice();
30+
let head = replace(bytes, Vec::new());
31+
let skipped = new_len - HEAD_LEN - TAIL_LEN;
32+
ProcOutput::Abbreviated { head, skipped, tail }
33+
}
34+
ProcOutput::Abbreviated { ref mut skipped, ref mut tail, .. } => {
35+
*skipped += data.len();
36+
if data.len() <= TAIL_LEN {
37+
tail[..data.len()].copy_from_slice(data);
38+
tail.rotate_left(data.len());
39+
} else {
40+
tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
41+
}
42+
return;
43+
}
44+
};
45+
*self = new_self;
46+
}
47+
48+
fn into_bytes(self) -> Vec<u8> {
49+
match self {
50+
ProcOutput::Full(bytes) => bytes,
51+
ProcOutput::Abbreviated { mut head, skipped, tail } => {
52+
write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
53+
head.extend_from_slice(&tail);
54+
head
55+
}
56+
}
57+
}
58+
}
59+
60+
let mut stdout = ProcOutput::Full(Vec::new());
61+
let mut stderr = ProcOutput::Full(Vec::new());
62+
63+
drop(child.stdin.take());
64+
read2(
65+
child.stdout.take().unwrap(),
66+
child.stderr.take().unwrap(),
67+
&mut |is_stdout, data, _| {
68+
if is_stdout { &mut stdout } else { &mut stderr }.extend(data);
69+
data.clear();
70+
},
71+
)?;
72+
let status = child.wait()?;
73+
74+
Ok(Output { status, stdout: stdout.into_bytes(), stderr: stderr.into_bytes() })
75+
}
576

677
#[cfg(not(any(unix, windows)))]
778
mod imp {

src/tools/compiletest/src/runtest.rs

+2-70
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::compute_diff::{write_diff, write_filtered_diff};
1212
use crate::errors::{self, Error, ErrorKind};
1313
use crate::header::TestProps;
1414
use crate::json;
15+
use crate::read2::read2_abbreviated;
1516
use crate::util::get_pointer_width;
1617
use crate::util::{logv, PathBufExt};
1718
use crate::ColorConfig;
@@ -27,7 +28,7 @@ use std::hash::{Hash, Hasher};
2728
use std::io::prelude::*;
2829
use std::io::{self, BufReader};
2930
use std::path::{Path, PathBuf};
30-
use std::process::{Child, Command, ExitStatus, Output, Stdio};
31+
use std::process::{Command, ExitStatus, Output, Stdio};
3132
use std::str;
3233

3334
use glob::glob;
@@ -3820,72 +3821,3 @@ enum AllowUnused {
38203821
Yes,
38213822
No,
38223823
}
3823-
3824-
fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
3825-
use crate::read2::read2;
3826-
use std::mem::replace;
3827-
3828-
const HEAD_LEN: usize = 160 * 1024;
3829-
const TAIL_LEN: usize = 256 * 1024;
3830-
3831-
enum ProcOutput {
3832-
Full(Vec<u8>),
3833-
Abbreviated { head: Vec<u8>, skipped: usize, tail: Box<[u8]> },
3834-
}
3835-
3836-
impl ProcOutput {
3837-
fn extend(&mut self, data: &[u8]) {
3838-
let new_self = match *self {
3839-
ProcOutput::Full(ref mut bytes) => {
3840-
bytes.extend_from_slice(data);
3841-
let new_len = bytes.len();
3842-
if new_len <= HEAD_LEN + TAIL_LEN {
3843-
return;
3844-
}
3845-
let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice();
3846-
let head = replace(bytes, Vec::new());
3847-
let skipped = new_len - HEAD_LEN - TAIL_LEN;
3848-
ProcOutput::Abbreviated { head, skipped, tail }
3849-
}
3850-
ProcOutput::Abbreviated { ref mut skipped, ref mut tail, .. } => {
3851-
*skipped += data.len();
3852-
if data.len() <= TAIL_LEN {
3853-
tail[..data.len()].copy_from_slice(data);
3854-
tail.rotate_left(data.len());
3855-
} else {
3856-
tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
3857-
}
3858-
return;
3859-
}
3860-
};
3861-
*self = new_self;
3862-
}
3863-
3864-
fn into_bytes(self) -> Vec<u8> {
3865-
match self {
3866-
ProcOutput::Full(bytes) => bytes,
3867-
ProcOutput::Abbreviated { mut head, skipped, tail } => {
3868-
write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
3869-
head.extend_from_slice(&tail);
3870-
head
3871-
}
3872-
}
3873-
}
3874-
}
3875-
3876-
let mut stdout = ProcOutput::Full(Vec::new());
3877-
let mut stderr = ProcOutput::Full(Vec::new());
3878-
3879-
drop(child.stdin.take());
3880-
read2(
3881-
child.stdout.take().unwrap(),
3882-
child.stderr.take().unwrap(),
3883-
&mut |is_stdout, data, _| {
3884-
if is_stdout { &mut stdout } else { &mut stderr }.extend(data);
3885-
data.clear();
3886-
},
3887-
)?;
3888-
let status = child.wait()?;
3889-
3890-
Ok(Output { status, stdout: stdout.into_bytes(), stderr: stderr.into_bytes() })
3891-
}

0 commit comments

Comments
 (0)