|
3 | 3 | // Modifications Copyright Kani Contributors
|
4 | 4 | // See GitHub history for details.
|
5 | 5 |
|
6 |
| -// FIXME: This is a complete copy of `cargo/src/cargo/util/read2.rs` |
7 |
| -// Consider unify the read2() in libstd, cargo and this to prevent further code duplication. |
8 |
| - |
9 |
| -pub use self::imp::read2; |
10 | 6 | use std::io;
|
11 | 7 | use std::process::{Child, Output};
|
12 | 8 |
|
13 |
| -pub fn read2_abbreviated(mut child: Child) -> io::Result<Output> { |
14 |
| - use io::Write; |
15 |
| - use std::mem::replace; |
16 |
| - |
17 |
| - const HEAD_LEN: usize = 160 * 1024; |
18 |
| - const TAIL_LEN: usize = 256 * 1024; |
19 |
| - |
20 |
| - enum ProcOutput { |
21 |
| - Full(Vec<u8>), |
22 |
| - Abbreviated { head: Vec<u8>, skipped: usize, tail: Box<[u8]> }, |
23 |
| - } |
24 |
| - |
25 |
| - impl ProcOutput { |
26 |
| - fn extend(&mut self, data: &[u8]) { |
27 |
| - let new_self = match *self { |
28 |
| - ProcOutput::Full(ref mut bytes) => { |
29 |
| - bytes.extend_from_slice(data); |
30 |
| - let new_len = bytes.len(); |
31 |
| - if new_len <= HEAD_LEN + TAIL_LEN { |
32 |
| - return; |
33 |
| - } |
34 |
| - let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice(); |
35 |
| - let head = replace(bytes, Vec::new()); |
36 |
| - let skipped = new_len - HEAD_LEN - TAIL_LEN; |
37 |
| - ProcOutput::Abbreviated { head, skipped, tail } |
38 |
| - } |
39 |
| - ProcOutput::Abbreviated { ref mut skipped, ref mut tail, .. } => { |
40 |
| - *skipped += data.len(); |
41 |
| - if data.len() <= TAIL_LEN { |
42 |
| - tail[..data.len()].copy_from_slice(data); |
43 |
| - tail.rotate_left(data.len()); |
44 |
| - } else { |
45 |
| - tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]); |
46 |
| - } |
47 |
| - return; |
48 |
| - } |
49 |
| - }; |
50 |
| - *self = new_self; |
51 |
| - } |
52 |
| - |
53 |
| - fn into_bytes(self) -> Vec<u8> { |
54 |
| - match self { |
55 |
| - ProcOutput::Full(bytes) => bytes, |
56 |
| - ProcOutput::Abbreviated { mut head, skipped, tail } => { |
57 |
| - write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap(); |
58 |
| - head.extend_from_slice(&tail); |
59 |
| - head |
60 |
| - } |
61 |
| - } |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - let mut stdout = ProcOutput::Full(Vec::new()); |
66 |
| - let mut stderr = ProcOutput::Full(Vec::new()); |
| 9 | +pub fn read2(mut child: Child) -> io::Result<Output> { |
| 10 | + let mut stdout = Vec::new(); |
| 11 | + let mut stderr = Vec::new(); |
67 | 12 |
|
68 | 13 | drop(child.stdin.take());
|
69 |
| - read2( |
| 14 | + self::imp::read2( |
70 | 15 | child.stdout.take().unwrap(),
|
71 | 16 | child.stderr.take().unwrap(),
|
72 | 17 | &mut |is_stdout, data, _| {
|
73 |
| - if is_stdout { &mut stdout } else { &mut stderr }.extend(data); |
| 18 | + let out = if is_stdout { &mut stdout } else { &mut stderr }; |
| 19 | + out.extend(data.iter()); |
74 | 20 | data.clear();
|
75 | 21 | },
|
76 | 22 | )?;
|
77 | 23 | let status = child.wait()?;
|
78 | 24 |
|
79 |
| - Ok(Output { status, stdout: stdout.into_bytes(), stderr: stderr.into_bytes() }) |
| 25 | + Ok(Output { status, stdout, stderr }) |
80 | 26 | }
|
81 | 27 |
|
82 | 28 | #[cfg(not(any(unix, windows)))]
|
|
0 commit comments