Packed bgra color struct

Is there a trick possible to define this struct so that the direct u32 value is available without a get function?
if not: Will the init and get functions be as fast as if directly getting / setting a u32?
I mean:

const c1: Bgra = Bgra.init(0xffffffff); // same speed as line below?
const c2: u32 = 0xffffffff;
pub const Bgra = packed struct
{
    const Self = @This();

    b: u8 = 0,
    g: u8 = 0,
    r: u8 = 0,
    a: u8 = 0,

    pub fn init(u: u32) Self
    {
        return @bitCast(u);
    }

    pub fn get(self: Self) u32
    {
        return @bitCast(self);
    }
};

I asked godbolt.org and, yes, they output basically identical assembly under -O ReleaseFast [1].


  1. tested under zig v0.13.0. ↩︎

1 Like

Thanks, I have to learn to use that one!