Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
Exonorid opened this issue Feb 28, 2021 · 2 comments
Closed

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

Exonorid opened this issue Feb 28, 2021 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@Exonorid
Copy link
Contributor

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.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Feb 28, 2021
@Vexu Vexu added this to the 0.8.0 milestone Feb 28, 2021
@Exonorid
Copy link
Contributor Author

I made a patch to implement this, and from my testing it appears to work correctly
bitCast.patch.txt

@andrewrk
Copy link
Member

Rejected for the same reason as #10547.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants