Skip to content

implement @byteOffsetOf and @bitOffsetOf in userland #1574

@andrewrk

Description

@andrewrk

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.

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