Gilnaa / memoffset

offsetof for Rust
MIT License
224 stars 29 forks source link

add raw_field macro to compute raw ptr to field #40

Closed RalfJung closed 4 years ago

RalfJung commented 4 years ago

Expose the part of offset_of! that computes a raw pointer to a field as a separate macro. @Diggsey expressed that this would be interesting for their crate.

If rust-field-offset could depend on memoffset to use this macro, that would mean there is just one place we have to change once &raw (or an equivalent mechanism) becomes stable, so that everyone uses the blessed way to do this.

@Diggsey does this macro fit the needs of your crate? @Gilnaa are you okay with adding this to the API surface of memoffset?

Gilnaa commented 4 years ago

Sure. Has anything changed since this in Feb7?

RalfJung commented 4 years ago

That was about using offset_of!, which is not possible with the existing API of rust-field-offset.

This PR is implementing the proposal a bit up that thread:

If there was a crate which implemented this macro: raw_address_of!((*ptr_to_undef).bar) and which guaranteed in the future to compile down to the MIR operator your RFC introduced, whilst for the moment making a "best effort" attempt, then I would be happy to use that to implement (2) in this crate.

RalfJung commented 4 years ago

Before making a release, let's see if the API works for @Diggsey

Gilnaa commented 4 years ago

Sure

Diggsey commented 4 years ago

Yeah, I'd be happy to depend on this crate for the raw_field function.

Amanieu commented 4 years ago

@RalfJung Does this macro resolve your concern here? https://github.com/Gilnaa/memoffset/pull/21#issuecomment-526351780

RalfJung commented 4 years ago

@Amanieu this macro has nothing to do with container_of!. It just exposes something that we already did as part of offset_of! before, so there is no change to what happens to provenance.