Skip to content

Add @parentFieldPtr #895

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
alexnask opened this issue Apr 5, 2018 · 3 comments
Closed

Add @parentFieldPtr #895

alexnask opened this issue Apr 5, 2018 · 3 comments

Comments

@alexnask
Copy link
Contributor

alexnask commented Apr 5, 2018

Given a pointer to a struct, returns the pointer to a field.

@parentFieldPtr(comptime ParentType: type, comptime field_name: []const u8,
    parent_ptr: &ParentType) -> &FieldType

// Test usage
// get the i'th field of a struct
fn get(self: var, comptime i: usize) &(@memberType(@typeOf(self).Child, i)) {
    const Self = @typeOf(self).Child;
    const field_name = @memberName(Self, i);
    return @parentFieldPtr(Self, field_name, self);
}

const Foo = struct {
    a: usize,
    b: bool,
};

test "struct get" {
    var foo = Foo {
        .a = 0,
        .b = false
    };

    *get(foo, 0) = 1;
    
    assert(foo.a == 1);
    assert(!*get(foo, 1));
}
@andrewrk
Copy link
Member

andrewrk commented Apr 5, 2018

@alexnask
Copy link
Contributor Author

alexnask commented Apr 5, 2018

Sorry if I'm misunderstanding or I wasn't clear.
What I want to do is the exact opposite, given a pointer to the struct and the name of the field, I would like to get the pointer to the field.

@andrewrk
Copy link
Member

andrewrk commented Apr 5, 2018

#766

@alexnask alexnask closed this as completed Apr 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants