@@ -344,39 +344,36 @@ pub const RunResult = struct {
344
344
stderr : []u8 ,
345
345
};
346
346
347
- fn fifoToOwnedArrayList (fifo : * std.io.PollFifo ) std.ArrayList ( u8 ) {
347
+ fn fifoToOwnedSlice (fifo : * std.io.PollFifo ) ! [] u8 {
348
348
if (fifo .head != 0 ) fifo .realign ();
349
- const result = std .ArrayList (u8 ){
349
+ var result = std .ArrayListUnmanaged (u8 ){
350
350
.items = fifo .buf [0.. fifo .count ],
351
351
.capacity = fifo .buf .len ,
352
- .allocator = fifo .allocator ,
353
352
};
354
353
fifo .* = std .io .PollFifo .init (fifo .allocator );
355
- return result ;
354
+ return result . toOwnedSlice ( fifo . allocator ) ;
356
355
}
357
356
357
+ pub const Output = struct {
358
+ stdout : []u8 ,
359
+ stderr : []u8 ,
360
+ };
361
+
358
362
/// Collect the output from the process's stdout and stderr. Will return once all output
359
363
/// has been collected. This does not mean that the process has ended. `wait` should still
360
364
/// be called to wait for and clean up the process.
361
365
///
362
366
/// The process must be started with stdout_behavior and stderr_behavior == .Pipe
363
367
pub fn collectOutput (
364
368
child : ChildProcess ,
365
- stdout : * std . ArrayList ( u8 ),
366
- stderr : * std . ArrayList ( u8 ) ,
369
+ /// Used for ` stdout` and `stderr`.
370
+ allocator : Allocator ,
367
371
max_output_bytes : usize ,
368
- ) ! void {
372
+ ) ! Output {
369
373
assert (child .stdout_behavior == .Pipe );
370
374
assert (child .stderr_behavior == .Pipe );
371
375
372
- // we could make this work with multiple allocators but YAGNI
373
- if (stdout .allocator .ptr != stderr .allocator .ptr or
374
- stdout .allocator .vtable != stderr .allocator .vtable )
375
- {
376
- unreachable ; // ChildProcess.collectOutput only supports 1 allocator
377
- }
378
-
379
- var poller = std .io .poll (stdout .allocator , enum { stdout , stderr }, .{
376
+ var poller = std .io .poll (allocator , enum { stdout , stderr }, .{
380
377
.stdout = child .stdout .? ,
381
378
.stderr = child .stderr .? ,
382
379
});
@@ -389,8 +386,15 @@ pub fn collectOutput(
389
386
return error .StderrStreamTooLong ;
390
387
}
391
388
392
- stdout .* = fifoToOwnedArrayList (poller .fifo (.stdout ));
393
- stderr .* = fifoToOwnedArrayList (poller .fifo (.stderr ));
389
+ const stdout = try fifoToOwnedSlice (poller .fifo (.stdout ));
390
+ errdefer allocator .free (stdout );
391
+ const stderr = try fifoToOwnedSlice (poller .fifo (.stderr ));
392
+ errdefer allocator .free (stderr );
393
+
394
+ return .{
395
+ .stdout = stdout ,
396
+ .stderr = stderr ,
397
+ };
394
398
}
395
399
396
400
pub const RunError = posix .GetCwdError || posix .ReadError || SpawnError || posix .PollError || error {
@@ -420,20 +424,13 @@ pub fn run(args: struct {
420
424
child .expand_arg0 = args .expand_arg0 ;
421
425
child .progress_node = args .progress_node ;
422
426
423
- var stdout = std .ArrayList (u8 ).init (args .allocator );
424
- var stderr = std .ArrayList (u8 ).init (args .allocator );
425
- errdefer {
426
- stdout .deinit ();
427
- stderr .deinit ();
428
- }
429
-
430
427
try child .spawn ();
431
- try child .collectOutput (& stdout , & stderr , args .max_output_bytes );
428
+ const output = try child .collectOutput (args . allocator , args .max_output_bytes );
432
429
433
430
return RunResult {
434
431
.term = try child .wait (),
435
- .stdout = try stdout . toOwnedSlice () ,
436
- .stderr = try stderr . toOwnedSlice () ,
432
+ .stdout = output . stdout ,
433
+ .stderr = output . stderr ,
437
434
};
438
435
}
439
436
0 commit comments