Squeeze bits

I was hoping letters would align on 64 bits so my struct would be 16 bytes.
How can I squeeze?
Somehow `‘letters’ bitsize = 128.

pub const Move = struct
{
    // 61 bits data
    pub const Data = packed struct
    {
         // 61 bits stuff
    };

    data: Data = .{},
    letters: std.BoundedArray(u8, 7) = .{},
}

BoundedArray stores a usize for the len.
You could implement it’s functionality yourself using a smaller len type

letters: [7]u8,
letterLen: u3,

OUCH of course! That usize.
Yes I rolled my own little Mini BoundedArray in there.

They should have comptimed that len variable :slight_smile:

They did and then walked back on it. I didn’t get why.

1 Like

Inb4 BoundedArrayUnmanaged where you need to pass the size yourself. :wink:

I guess storing size and not including it in the array type makes it more convenient to pass around.

So this minimal example does fit in 64 bits for TinyBoundedArray(u8, 7)

const std = @import("std");

pub fn TinyBoundedArray(comptime T: type, comptime capacity: u8) type
{
    return struct
    {
        const Self = @This();

        buffer: [capacity]T,
        len: u8 = 0,

        pub fn slice(self: *const Self)[]const T
        {
            return self.buffer[0..self.len];
        }

        fn append(self: *Self, value: T) void
        {
            self.buffer[self.len] = value;
            self.len += 1;
        }

        fn insert(self: *Self, value: T) void
        {
            if (self.len > 0) std.mem.copyBackwards(T, self.buffer[1..1 + self.len], self.buffer[0..self.len]);
            self.buffer[0] = value;
            self.len += 1;
        }
    };
}
1 Like

Actually, now I need a dynamic version just because of that usize.
That is less convenient. I need it for (test) building a trie where the nodes must not be to big.