From 751b51b8596e5f5a2655db0ff8f05bcad4b52b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Thu, 24 Feb 2022 17:35:35 +0200 Subject: [PATCH] change size treatment for PackedUser --- src/user.zig | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/user.zig b/src/user.zig index 692b62a..da41488 100644 --- a/src/user.zig +++ b/src/user.zig @@ -21,11 +21,9 @@ pub const User = struct { }; pub const PackedUser = struct { - // TODO: use @bitSizeOf(Inner)/8: - //const InnerSize = @divExact(@bitSizeOf(Inner), 8); const shellIndexFnType = fn ([]const u8) ?u6; - const InnerSize = @sizeOf(Inner); + const InnerSize = @divExact(@bitSizeOf(Inner), 8); const Inner = packed struct { uid: u32, gid: u32, @@ -96,6 +94,7 @@ pub const PackedUser = struct { pub fn fromBytes(bytes: []const u8) PackedUser { const inner = std.mem.bytesAsValue( PackedUser, + // https://github.com/ziglang/zig/issues/10958 bytes[0..@sizeOf(Inner)][0..@sizeOf(Inner)], ); @@ -123,8 +122,10 @@ pub const PackedUser = struct { // packTo packs the User record and copies it to the given byte slice. The // slice must have at least maxRecordSize() bytes available. - pub fn packTo(buf1: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void { - var buf = buf1.*; + pub fn packTo(bufPtr: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void { + // function arguments are consts. We need to mutate the underlying + // slice, so passing it via pointer instead. + var buf = bufPtr.*; const bufStart = @ptrToInt(buf.ptr); const home_len = try downCast(u6, user.home.len - 1);