Skip to content

Make @bitCast use @bitSizeOf instead of @sizeOf #8102

@Exonorid

Description

@Exonorid

Currently, the bitCast builtin function works such that it "Asserts that @sizeOf(@TypeOf(value)) == @sizeOf(DestType)." I'm proposing that this is changed to be bitSizeOf to exclude padding bytes.

Why?

The main place I've found this interferes with programming is bit-casting to an integer which is a non-power of two in size. For example,

const Foo = packed struct {a: u8, b: u16}
const m_foo = Foo{.a = 5, .b = 73}
const as_int: u24 = @bitCast(u24, m_foo);

would not work because u24 is 4 bytes wide according to sizeOf, while Foo is 3 bytes wide, as is expected. Using bitSizeOf, this would compile, since both are 24 bits wide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions