From bc75e869043e84f11a3aae6d3d228beff9a34be6 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sat, 5 Apr 2025 12:01:52 -0400 Subject: Write the header as a packed int this seems like the best way to do it. --- src/saprus_message.zig | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/saprus_message.zig') diff --git a/src/saprus_message.zig b/src/saprus_message.zig index 6c46a8f..1f1d37e 100644 --- a/src/saprus_message.zig +++ b/src/saprus_message.zig @@ -79,16 +79,21 @@ pub const SaprusMessage = union(SaprusPacketType) { // Write the payload bytes as base64 to the growable string. try base64Enc.encodeWriter(buf_w, payload); - // Write the packet body to the output writer + // At this point, payload_list contains the base64 encoded payload. + + // Write the packet body to the output buf. try buf.*.appendSlice(asBytes(&nativeToBig(u16, @intCast(payload_list.items.len)))); - var h = header; - inline for (@typeInfo(Header).@"struct".fields) |f| { - @field(h, f.name) = nativeToBig(@TypeOf(@field(h, f.name)), @field(h, f.name)); - } + // Write the header bytes to the output buf. + const HeaderInt = @typeInfo(Header).@"struct".backing_integer.?; + std.mem.writePackedInt( + HeaderInt, + try buf.*.addManyAsSlice(@bitSizeOf(Header) / 8), + 0, + @bitCast(header), + .little, + ); - const h_bytes = asBytes(&h); - try buf.*.appendSlice(h_bytes[0 .. h_bytes.len - 2]); try buf.*.appendSlice(payload_list.items); } -- cgit