diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 16:00:24 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 16:02:31 -0400 |
commit | 0a441d2bce5ecca880196df283f31c3ef5c78aea (patch) | |
tree | 49e7484cf0e2755eea81746ad58bb459fd278953 | |
parent | 748991d35b5552475941fb75e3309e11c6d27529 (diff) |
Properly initialize the SaprusMessage fromBytes
Will actually use the provided packet type inline.
-rw-r--r-- | src/main.zig | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/main.zig b/src/main.zig index b4ae666..951666f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -84,7 +84,13 @@ const SaprusMessage = union(SaprusPacketType) { } }; -inline fn toBytesAux(comptime Header: type, header: Header, payload: []const u8, w: std.ArrayList(u8).Writer, allocator: Allocator) !void { +inline fn toBytesAux( + comptime Header: type, + header: Header, + payload: []const u8, + w: std.ArrayList(u8).Writer, + allocator: Allocator, +) !void { const payload_list = try encodeToList(payload, allocator); defer payload_list.deinit(); @@ -93,26 +99,20 @@ inline fn toBytesAux(comptime Header: type, header: Header, payload: []const u8, try w.writeAll(payload_list.items); } -inline fn fromBytesAux(comptime Packet: SaprusPacketType, comptime Header: type, r: StringReader, allocator: Allocator) !SaprusMessage { +inline fn fromBytesAux( + comptime packet: SaprusPacketType, + comptime Header: type, + r: StringReader, + allocator: Allocator, +) !SaprusMessage { const header = try r.readStructEndian(Header, .big); const len = try r.readInt(u16, .big); const payload = try decodeFromReader(r, len, allocator); - return switch (Packet) { - .relay => SaprusMessage{ - .relay = .{ - .header = header, - .payload = payload, - }, - }, - .connection => SaprusMessage{ - .connection = .{ - .header = header, - .payload = payload, - }, - }, - else => unreachable, - }; + return @unionInit(SaprusMessage, @tagName(packet), .{ + .header = header, + .payload = payload, + }); } pub fn main() !void { |