From 19198ded7af455464a60fa97fdb80b38eb3a3a3f Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Fri, 9 Aug 2024 12:26:49 +0800 Subject: [PATCH] add test for afterInit, revert to std.mem.eql since it _has_ been optimized for strings --- src/httpz.zig | 6 +++++- src/key_value.zig | 12 +----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/httpz.zig b/src/httpz.zig index ee214d5..b99cffe 100644 --- a/src/httpz.zig +++ b/src/httpz.zig @@ -547,7 +547,7 @@ pub fn upgradeWebsocket(comptime H: type, req: *Request, res: *Response, ctx: an try http_conn.stream.writeAll(&ws.Handshake.createReply(key)); if (comptime std.meta.hasFn(H, "afterInit")) { const params = @typeInfo(@TypeOf(H.afterInit)).Fn.params; - try if (comptime params.len == 1) hc.handler.afterInit() else hc.handler.afterInit(ctx); + try if (comptime params.len == 1) hc.handler.?.afterInit() else hc.handler.?.afterInit(ctx); } res.written = true; @@ -1432,6 +1432,10 @@ const TestWebsocketHandler = struct { }; } + pub fn afterInit(self: *WebsocketHandler, ctx: u32) !void { + try t.expectEqual(self.ctx, ctx); + } + pub fn clientMessage(self: *WebsocketHandler, data: []const u8) !void { if (std.mem.eql(u8, data, "close")) { self.conn.close(.{}) catch {}; diff --git a/src/key_value.zig b/src/key_value.zig index 6145fcd..e7f7875 100644 --- a/src/key_value.zig +++ b/src/key_value.zig @@ -90,17 +90,7 @@ fn MakeKeyValue(K: type, V: type, equalFn: fn (lhs: K, rhs: K) bool) type { } fn strEql(lhs: []const u8, rhs: []const u8) bool { - // This is largely a reminder to myself that std.mem.eql isn't - // particularly fast. Here we at least avoid the 1 extra ptr - // equality check that std.mem.eql does, but we could do better - // TODO: monitor https://github.com/ziglang/zig/issues/8689 - if (lhs.len != rhs.len) { - return false; - } - for (lhs, rhs) |l, r| { - if (l != r) return false; - } - return true; + return std.mem.eql(u8, lhs, rhs); } pub const KeyValue = MakeKeyValue([]const u8, []const u8, strEql);