change size treatment for PackedUser

This commit is contained in:
Motiejus Jakštys 2022-02-24 17:35:35 +02:00 committed by Motiejus Jakštys
parent 49a7d79b05
commit 751b51b859

View File

@ -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);