memoffset icon indicating copy to clipboard operation
memoffset copied to clipboard

offset_of! for unsized structs

Open est31 opened this issue 4 years ago • 10 comments

This doesn't work with memoffset:

struct Foo {
    bar: f32,
    baz: [f32],
}
offset_of!(Foo, bar);

Even though this (unsound) code works:

struct Foo {
    bar: f32,
    baz: [f32],
}
let foo_ptr: &Foo = unsafe { mem::zeroed() };
let foo_offs = (&foo_ptr.bar) as *const _ as usize;

I ran into this problem when trying to port glium to memoffset in https://github.com/glium/glium/pull/1782. The gpgpu example has relied on offset of calculations supporting unsized structs. I could work around the issue by changing the gpgpu example, but that isn't really perfect.

est31 avatar Aug 19 '19 04:08 est31