Skip to content

implement @byteOffsetOf and @bitOffsetOf in userland #1574

New issue

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

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

Already on GitHub? Sign in to your account

Closed
andrewrk opened this issue Sep 21, 2018 · 1 comment
Closed

implement @byteOffsetOf and @bitOffsetOf in userland #1574

andrewrk opened this issue Sep 21, 2018 · 1 comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

At runtime you can do this to get offset:

    var a: A = undefined;
    assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));

We could make this work at compile time with the concept of lazy evaluated comptime values. For example even though we don't know the actual integer address of &a at comptime or of &a.a, if the only thing the comptime code does with these values is subtract them, we can have that return the offset. That means we can delete @byteOffsetOf and make it a userland function.

As for @bitOffsetOf something similar could be done with taking the address of a bit packed field, plus @typeInfo, since the bit offset information is in the pointer type.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Sep 21, 2018
@andrewrk andrewrk added this to the 1.0.0 milestone Sep 21, 2018
@andrewrk andrewrk modified the milestones: 1.0.0, 0.7.0 Apr 15, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 10, 2020
@andrewrk
Copy link
Member Author

This is superseded by #8642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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

1 participant