Skip to content

Commit b12de31

Browse files
committed
migrate std-docs.zig to std.Io
1 parent 5db9eaa commit b12de31

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lib/compiler/std-docs.zig

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub fn main() !void {
2727
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
2828
const gpa = general_purpose_allocator.allocator();
2929

30+
var threaded: std.Io.Threaded = .init(gpa);
31+
const io = threaded.io();
32+
3033
var argv = try std.process.argsWithAllocator(arena);
3134
defer argv.deinit();
3235
assert(argv.skip());
@@ -58,11 +61,11 @@ pub fn main() !void {
5861
}
5962
const should_open_browser = force_open_browser orelse (listen_port == 0);
6063

61-
const address = std.net.Address.parseIp("127.0.0.1", listen_port) catch unreachable;
62-
var http_server = try address.listen(.{
64+
const address = std.Io.net.IpAddress.parse("127.0.0.1", listen_port) catch unreachable;
65+
var http_server = try address.listen(io, .{
6366
.reuse_address = true,
6467
});
65-
const port = http_server.listen_address.in.getPort();
68+
const port = http_server.socket.address.getPort();
6669
const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
6770
std.fs.File.stdout().writeAll(url_with_newline) catch {};
6871
if (should_open_browser) {
@@ -73,30 +76,31 @@ pub fn main() !void {
7376

7477
var context: Context = .{
7578
.gpa = gpa,
79+
.io = io,
7680
.zig_exe_path = zig_exe_path,
7781
.global_cache_path = global_cache_path,
7882
.lib_dir = lib_dir,
7983
.zig_lib_directory = zig_lib_directory,
8084
};
8185

8286
while (true) {
83-
const connection = try http_server.accept();
84-
_ = std.Thread.spawn(.{}, accept, .{ &context, connection }) catch |err| {
87+
const stream = try http_server.accept(io);
88+
_ = std.Thread.spawn(.{}, accept, .{ &context, stream }) catch |err| {
8589
std.log.err("unable to accept connection: {s}", .{@errorName(err)});
86-
connection.stream.close();
90+
stream.close(io);
8791
continue;
8892
};
8993
}
9094
}
9195

92-
fn accept(context: *Context, connection: std.net.Server.Connection) void {
93-
defer connection.stream.close();
96+
fn accept(context: *Context, stream: std.Io.net.Stream) void {
97+
defer stream.close(context.io);
9498

9599
var recv_buffer: [4000]u8 = undefined;
96100
var send_buffer: [4000]u8 = undefined;
97-
var conn_reader = connection.stream.reader(&recv_buffer);
98-
var conn_writer = connection.stream.writer(&send_buffer);
99-
var server = std.http.Server.init(conn_reader.interface(), &conn_writer.interface);
101+
var conn_reader = stream.reader(context.io, &recv_buffer);
102+
var conn_writer = stream.writer(context.io, &send_buffer);
103+
var server = std.http.Server.init(&conn_reader.interface, &conn_writer.interface);
100104
while (server.reader.state == .ready) {
101105
var request = server.receiveHead() catch |err| switch (err) {
102106
error.HttpConnectionClosing => return,
@@ -124,6 +128,7 @@ fn accept(context: *Context, connection: std.net.Server.Connection) void {
124128

125129
const Context = struct {
126130
gpa: Allocator,
131+
io: std.Io,
127132
lib_dir: std.fs.Dir,
128133
zig_lib_directory: []const u8,
129134
zig_exe_path: []const u8,
@@ -218,12 +223,8 @@ fn serveSourcesTar(request: *std.http.Server.Request, context: *Context) !void {
218223
var file = try entry.dir.openFile(entry.basename, .{});
219224
defer file.close();
220225
const stat = try file.stat();
221-
var file_reader: std.fs.File.Reader = .{
222-
.file = file,
223-
.interface = std.fs.File.Reader.initInterface(&.{}),
224-
.size = stat.size,
225-
};
226-
try archiver.writeFile(entry.path, &file_reader, stat.mtime);
226+
var file_reader = file.reader(context.io, &.{});
227+
try archiver.writeFileTimestamp(entry.path, &file_reader, stat.mtime);
227228
}
228229

229230
{
@@ -255,7 +256,7 @@ fn serveWasm(
255256
const wasm_base_path = try buildWasmBinary(arena, context, optimize_mode);
256257
const bin_name = try std.zig.binNameAlloc(arena, .{
257258
.root_name = autodoc_root_name,
258-
.target = &(std.zig.system.resolveTargetQuery(std.Build.parseTargetQuery(.{
259+
.target = &(std.zig.system.resolveTargetQuery(context.io, std.Build.parseTargetQuery(.{
259260
.arch_os_abi = autodoc_arch_os_abi,
260261
.cpu_features = autodoc_cpu_features,
261262
}) catch unreachable) catch unreachable),
@@ -394,7 +395,7 @@ fn buildWasmBinary(
394395
}
395396

396397
if (result_error_bundle.errorMessageCount() > 0) {
397-
result_error_bundle.renderToStdErr(.{}, true);
398+
result_error_bundle.renderToStdErr(.{}, .auto);
398399
std.log.err("the following command failed with {d} compilation errors:\n{s}", .{
399400
result_error_bundle.errorMessageCount(),
400401
try std.Build.Step.allocPrintCmd(arena, null, argv.items),

0 commit comments

Comments
 (0)