From 885db6aacf7f7b64888509b22eda40b4414ebe2f Mon Sep 17 00:00:00 2001 From: JacobCrabill Date: Tue, 30 Jan 2024 21:37:38 -0800 Subject: [PATCH] =?UTF-8?q?Plow=20through=20all=20remaining=20zig-0.12-dev?= =?UTF-8?q?=20updates=20=F0=9F=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/bindataformpost/bindataformpost.zig | 7 ++++--- examples/hello_json/hello_json.zig | 4 ++-- examples/http_params/http_params.zig | 7 ++++--- examples/middleware/middleware.zig | 18 ++++++++--------- .../middleware_with_endpoint.zig | 12 +++++------ examples/simple_router/simple_router.zig | 2 +- tools/announceybot.zig | 10 +++++----- tools/pkghash.zig | 20 +++++++++---------- wrk/zigstd/main.zig | 8 +++----- 9 files changed, 42 insertions(+), 46 deletions(-) diff --git a/examples/bindataformpost/bindataformpost.zig b/examples/bindataformpost/bindataformpost.zig index 944b92e..507461d 100644 --- a/examples/bindataformpost/bindataformpost.zig +++ b/examples/bindataformpost/bindataformpost.zig @@ -16,7 +16,7 @@ const Handler = struct { // check for query params (for ?terminate=true) r.parseQuery(); - var param_count = r.getParamCount(); + const param_count = r.getParamCount(); std.log.info("param_count: {}", .{param_count}); // iterate over all params @@ -28,7 +28,8 @@ const Handler = struct { for (params.items) |kv| { if (kv.value) |v| { std.debug.print("\n", .{}); - std.log.info("Param `{s}` in owned list is {any}\n", .{ kv.key.str, v }); + // TODO: Zig fails to format 'v': + // std.log.info("Param `{s}` in owned list is {any}\n", .{ kv.key.str, v }); switch (v) { // single-file upload zap.Request.HttpParam.Hash_Binfile => |*file| { @@ -87,7 +88,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); Handler.alloc = allocator; diff --git a/examples/hello_json/hello_json.zig b/examples/hello_json/hello_json.zig index d5b2131..cd2d709 100644 --- a/examples/hello_json/hello_json.zig +++ b/examples/hello_json/hello_json.zig @@ -42,7 +42,7 @@ fn setupUserData(a: std.mem.Allocator) !void { } pub fn main() !void { - var a = std.heap.page_allocator; + const a = std.heap.page_allocator; try setupUserData(a); var listener = zap.HttpListener.init(.{ .port = 3000, @@ -53,7 +53,7 @@ pub fn main() !void { std.debug.print( \\ Listening on 0.0.0.0:3000 - \\ + \\ \\ Check out: \\ http://localhost:3000/user/1 # -- first user \\ http://localhost:3000/user/2 # -- second user diff --git a/examples/http_params/http_params.zig b/examples/http_params/http_params.zig index 2c4f123..07a9bee 100644 --- a/examples/http_params/http_params.zig +++ b/examples/http_params/http_params.zig @@ -61,9 +61,10 @@ pub fn main() !void { // iterate over all params const params = r.parametersToOwnedList(alloc, false) catch unreachable; defer params.deinit(); - for (params.items) |kv| { - std.log.info("Param `{s}` is {any}", .{ kv.key.str, kv.value }); - } + // for (params.items) |kv| { + // // TODO: fmt can't format kv.value + // // std.log.info("Param `{s}` is {any}", .{ kv.key.str, kv.value.? }); + // } // let's get param "one" by name std.debug.print("\n", .{}); diff --git a/examples/middleware/middleware.zig b/examples/middleware/middleware.zig index 5687f93..5c2bd90 100644 --- a/examples/middleware/middleware.zig +++ b/examples/middleware/middleware.zig @@ -71,9 +71,8 @@ const UserMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { - // this is how we would get our self pointer - var self = @fieldParentPtr(Self, "handler", handler); - _ = self; + // this is how we would get our self pointer: + // var self = @fieldParentPtr(Self, "handler", handler); // do our work: fill in the user field of the context context.user = User{ @@ -114,9 +113,8 @@ const SessionMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { - // this is how we would get our self pointer - var self = @fieldParentPtr(Self, "handler", handler); - _ = self; + // this is how we would get our self pointer: + // var self = @fieldParentPtr(Self, "handler", handler); context.session = Session{ .info = "secret session", @@ -149,10 +147,10 @@ const HtmlMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { + _ = handler; - // this is how we would get our self pointer - var self = @fieldParentPtr(Self, "handler", handler); - _ = self; + // this is how we would get our self pointer: + // var self = @fieldParentPtr(Self, "handler", handler); std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context}); @@ -190,7 +188,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); SharedAllocator.init(allocator); // we create our HTML middleware component that handles the request diff --git a/examples/middleware_with_endpoint/middleware_with_endpoint.zig b/examples/middleware_with_endpoint/middleware_with_endpoint.zig index 9e4b6c8..e74cfb0 100644 --- a/examples/middleware_with_endpoint/middleware_with_endpoint.zig +++ b/examples/middleware_with_endpoint/middleware_with_endpoint.zig @@ -59,9 +59,8 @@ const UserMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { - // this is how we would get our self pointer - var self = @fieldParentPtr(Self, "handler", handler); - _ = self; + // this is how we would get our self pointer: + // var self = @fieldParentPtr(Self, "handler", handler); // do our work: fill in the user field of the context context.user = User{ @@ -104,9 +103,8 @@ const SessionMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { - // this is how we would get our self pointer - var self = @fieldParentPtr(Self, "handler", handler); - _ = self; + // this is how we would get our self pointer: + // var self = @fieldParentPtr(Self, "handler", handler); context.session = Session{ .info = "secret session", @@ -200,7 +198,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); SharedAllocator.init(allocator); // create the endpoint diff --git a/examples/simple_router/simple_router.zig b/examples/simple_router/simple_router.zig index 94838ab..acff407 100644 --- a/examples/simple_router/simple_router.zig +++ b/examples/simple_router/simple_router.zig @@ -73,7 +73,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); var simpleRouter = zap.Router.init(allocator, .{ .not_found = not_found, diff --git a/tools/announceybot.zig b/tools/announceybot.zig index 6dbef9d..a87c767 100644 --- a/tools/announceybot.zig +++ b/tools/announceybot.zig @@ -88,7 +88,7 @@ fn get_tag_annotation(allocator: std.mem.Allocator, tagname: []const u8) ![]cons tagname, }; - const result = try std.ChildProcess.exec(.{ + const result = try std.ChildProcess.run(.{ .allocator = allocator, .argv = &args, }); @@ -161,13 +161,13 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json defer http_client.deinit(); // request - var req = try http_client.request(.POST, uri, h, .{}); + var req = try http_client.open(.POST, uri, h, .{}); defer req.deinit(); req.transfer_encoding = .chunked; // connect, send request - try req.start(); + try req.send(.{ .raw_uri = false }); // send POST payload try req.writer().writeAll(message_json); @@ -182,7 +182,7 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json fn sendToDiscord(allocator: std.mem.Allocator, url: []const u8, message: []const u8) !void { // json payload // max size: 100kB - var buf: []u8 = try allocator.alloc(u8, 100 * 1024); + const buf: []u8 = try allocator.alloc(u8, 100 * 1024); defer allocator.free(buf); var fba = std.heap.FixedBufferAllocator.init(buf); var string = std.ArrayList(u8).init(fba.allocator()); @@ -399,7 +399,7 @@ fn command_update_readme(allocator: std.mem.Allocator, tag: []const u8) !void { // we need to put the \n back in. // TODO: change this by using some "search" iterator that just // returns indices etc - var output_line = try std.fmt.allocPrint(allocator, "{s}\n", .{line}); + const output_line = try std.fmt.allocPrint(allocator, "{s}\n", .{line}); defer allocator.free(output_line); _ = try writer.write(output_line); } diff --git a/tools/pkghash.zig b/tools/pkghash.zig index f3997fc..6fa9ca9 100644 --- a/tools/pkghash.zig +++ b/tools/pkghash.zig @@ -56,22 +56,22 @@ fn showHelp() !void { pub const usage_pkg = \\Usage: pkghash [options] \\ - \\Options: + \\Options: \\ -h --help Print this help and exit. \\ -g --git Use git ls-files \\ - \\Sub-options: + \\Sub-options: \\ --allow-directory : calc hash even if no build.zig is present \\ applies in no-git mode only \\ - \\Sub-options for --git: + \\Sub-options for --git: \\ --tag= : specify git tag to use in template \\ defaults to tag pointing to HEAD \\ --template= : specify markdown template to render ; pub fn gitLatestTag(gpa: Allocator, pkg_dir: []const u8) ![]const u8 { - const result = try std.ChildProcess.exec(.{ + const result = try std.ChildProcess.run(.{ .allocator = gpa, .argv = &.{ "git", @@ -97,7 +97,7 @@ pub fn gitLatestTag(gpa: Allocator, pkg_dir: []const u8) ![]const u8 { } pub fn gitFileList(gpa: Allocator, pkg_dir: []const u8) ![]const u8 { - const result = try std.ChildProcess.exec(.{ + const result = try std.ChildProcess.run(.{ .allocator = gpa, .argv = &.{ "git", @@ -266,8 +266,8 @@ pub fn cmdPkg(gpa: Allocator, arena: Allocator, args: []const []const u8) !void // computePackageHash will close the directory after completion // std.debug.print("abspath: {s}\n", .{cwd_absolute_path}); - var cwd_copy = try fs.openIterableDirAbsolute(cwd_absolute_path, .{}); - errdefer cwd_copy.dir.close(); + var cwd_copy = try fs.openDirAbsolute(cwd_absolute_path, .{ .iterate = true }); + errdefer cwd_copy.close(); var thread_pool: ThreadPool = undefined; try thread_pool.init(.{ .allocator = gpa }); @@ -281,7 +281,7 @@ pub fn cmdPkg(gpa: Allocator, arena: Allocator, args: []const []const u8) !void }; break :blk try computePackageHashExcludingDirectories( &thread_pool, - .{ .dir = cwd_copy.dir }, + cwd_copy, excluded_directories, ); }; @@ -355,7 +355,7 @@ fn isExecutable(file: fs.File) !bool { pub fn computePackageHashExcludingDirectories( thread_pool: *ThreadPool, - pkg_dir: fs.IterableDir, + pkg_dir: fs.Dir, excluded_directories: []const []const u8, ) ![Manifest.Hash.digest_length]u8 { const gpa = thread_pool.allocator; @@ -405,7 +405,7 @@ pub fn computePackageHashExcludingDirectories( .failure = undefined, // to be populated by the worker }; wait_group.start(); - try thread_pool.spawn(workerHashFile, .{ pkg_dir.dir, hashed_file, &wait_group }); + try thread_pool.spawn(workerHashFile, .{ pkg_dir, hashed_file, &wait_group }); try all_files.append(hashed_file); } diff --git a/wrk/zigstd/main.zig b/wrk/zigstd/main.zig index 30e0636..680c7b2 100644 --- a/wrk/zigstd/main.zig +++ b/wrk/zigstd/main.zig @@ -4,11 +4,9 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); - var server = std.http.Server.init(allocator, .{ - .reuse_address = true, - }); + var server = std.http.Server.init(.{ .reuse_address = true }); defer server.deinit(); const address = try std.net.Address.parseIp("127.0.0.1", 3000); @@ -30,7 +28,7 @@ pub fn main() !void { res.transfer_encoding = .{ .content_length = server_body.len }; try res.headers.append("content-type", "text/plain"); try res.headers.append("connection", "close"); - try res.do(); + try res.send(); // do() -> send()? var buf: [128]u8 = undefined; _ = try res.readAll(&buf);