-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Document that the ptr
field of Allocator/Random should not be compared and remove existing comparison
#22691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,15 +344,17 @@ pub const RunResult = struct { | |
stderr: []u8, | ||
}; | ||
|
||
fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) { | ||
fn writeFifoDataToArrayList(allocator: Allocator, list: *std.ArrayListUnmanaged(u8), fifo: *std.io.PollFifo) !void { | ||
if (fifo.head != 0) fifo.realign(); | ||
const result = std.ArrayList(u8){ | ||
.items = fifo.buf[0..fifo.count], | ||
.capacity = fifo.buf.len, | ||
.allocator = fifo.allocator, | ||
}; | ||
fifo.* = std.io.PollFifo.init(fifo.allocator); | ||
return result; | ||
if (list.capacity == 0) { | ||
list.* = .{ | ||
.items = fifo.buf[0..fifo.count], | ||
.capacity = fifo.buf.len, | ||
}; | ||
fifo.* = std.io.PollFifo.init(fifo.allocator); | ||
} else { | ||
try list.appendSlice(allocator, fifo.buf[0..fifo.count]); | ||
} | ||
} | ||
|
||
/// Collect the output from the process's stdout and stderr. Will return once all output | ||
|
@@ -362,21 +364,16 @@ fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) { | |
/// The process must be started with stdout_behavior and stderr_behavior == .Pipe | ||
pub fn collectOutput( | ||
child: ChildProcess, | ||
stdout: *std.ArrayList(u8), | ||
stderr: *std.ArrayList(u8), | ||
/// Used for `stdout` and `stderr`. | ||
allocator: Allocator, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is going to break some people's code, let's put a little effort into making it more future-proof. My suggestion also happens to be more familiar to existing callsites.
Sure, this implementation uses that fifo abstraction but that's not necessarily how it has to work. And append-only array list is generally a very flexible API from the caller's perspective. I'll note also that this patch introduces more error handling that can be deleted with the above suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed a simple version of this API while I look into improving the implementation (the version I just pushed still uses Unsure how long it'll take to write the improved version; one option would be to leave improving the implementation as a follow-up issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed an attempt at making Let me know if it looks like I'm vaguely on the right track and I'll add it to this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I didn't look at your branch yet, I'll try to get to that later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, it's only a minor improvement after the |
||
stdout: *std.ArrayListUnmanaged(u8), | ||
stderr: *std.ArrayListUnmanaged(u8), | ||
max_output_bytes: usize, | ||
) !void { | ||
assert(child.stdout_behavior == .Pipe); | ||
assert(child.stderr_behavior == .Pipe); | ||
|
||
// we could make this work with multiple allocators but YAGNI | ||
if (stdout.allocator.ptr != stderr.allocator.ptr or | ||
stdout.allocator.vtable != stderr.allocator.vtable) | ||
{ | ||
unreachable; // ChildProcess.collectOutput only supports 1 allocator | ||
} | ||
|
||
var poller = std.io.poll(stdout.allocator, enum { stdout, stderr }, .{ | ||
var poller = std.io.poll(allocator, enum { stdout, stderr }, .{ | ||
.stdout = child.stdout.?, | ||
.stderr = child.stderr.?, | ||
}); | ||
|
@@ -389,8 +386,8 @@ pub fn collectOutput( | |
return error.StderrStreamTooLong; | ||
} | ||
|
||
stdout.* = fifoToOwnedArrayList(poller.fifo(.stdout)); | ||
stderr.* = fifoToOwnedArrayList(poller.fifo(.stderr)); | ||
try writeFifoDataToArrayList(allocator, stdout, poller.fifo(.stdout)); | ||
try writeFifoDataToArrayList(allocator, stderr, poller.fifo(.stderr)); | ||
} | ||
|
||
pub const RunError = posix.GetCwdError || posix.ReadError || SpawnError || posix.PollError || error{ | ||
|
@@ -420,22 +417,20 @@ pub fn run(args: struct { | |
child.expand_arg0 = args.expand_arg0; | ||
child.progress_node = args.progress_node; | ||
|
||
var stdout = std.ArrayList(u8).init(args.allocator); | ||
var stderr = std.ArrayList(u8).init(args.allocator); | ||
errdefer { | ||
stdout.deinit(); | ||
stderr.deinit(); | ||
} | ||
var stdout: std.ArrayListUnmanaged(u8) = .empty; | ||
errdefer stdout.deinit(args.allocator); | ||
var stderr: std.ArrayListUnmanaged(u8) = .empty; | ||
errdefer stderr.deinit(args.allocator); | ||
|
||
try child.spawn(); | ||
errdefer { | ||
_ = child.kill() catch {}; | ||
} | ||
try child.collectOutput(&stdout, &stderr, args.max_output_bytes); | ||
try child.collectOutput(args.allocator, &stdout, &stderr, args.max_output_bytes); | ||
|
||
return RunResult{ | ||
.stdout = try stdout.toOwnedSlice(), | ||
.stderr = try stderr.toOwnedSlice(), | ||
.stdout = try stdout.toOwnedSlice(args.allocator), | ||
.stderr = try stderr.toOwnedSlice(args.allocator), | ||
.term = try child.wait(), | ||
}; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.