Is there a way to reduce the size of an optional type

The size of ?u7 is 2. Is there a way to reduce it to one?

const std = @import("std");

const T = ?u7;

const S = packed struct {
	x: T, // packed structs cannot contain fields of type '?u7'
};

pub fn main() void {
	std.debug.print("{} {}\n", .{@sizeOf(T), @bitSizeOf(T)}); // 2 16
}
2 Likes

I’ve faced the same problem, but it seems there’s no solution for it.
I have to manually do something like this:

const S = packed struct {
    x: u7,
    is_null: u1 = 1,
};
4 Likes

The solution should work. I’m just curious whether or not there are builtin ways to do it. Thanks for the answer.

7 Likes

You can also use bool instead of u1 to make this a little more convenient.

4 Likes