Skip to content

Commit

Permalink
add test for afterInit, revert to std.mem.eql since it _has_ been opt…
Browse files Browse the repository at this point in the history
…imized for strings
  • Loading branch information
karlseguin committed Aug 9, 2024
1 parent 1ae749c commit 19198de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/httpz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {};
Expand Down
12 changes: 1 addition & 11 deletions src/key_value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 19198de

Please sign in to comment.