-
-
Notifications
You must be signed in to change notification settings - Fork 79
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
Safe offset_of
for packed structs using addr_of
?
#212
Comments
|
We could probably adjust things but it would have to be opt-in, because the offset_of module isn't feature gated and so it needs to work on 1.34 by default. |
Ah sorry, I missed the README note about the 1.34 MSRV. In that case, the easiest solution is probably to add a new |
Ah, well we could but I thought that the standard library offset_of was developing at a reasonable enough pace for people to just be able to use that sooner than later. Also, double checking the file itself I actually don't see the unsafe block that is being referred to, so is that note old? |
I wasn't aware that the standard library offset_of is so close to stabilization. I'm fine with waiting a bit and using a manual workaround based on
I think the unsafe block is required by Rust when you try to create a reference to a field of a packed struct, as it might be unaligned. |
phil why are you using packed structs to begin with? They're very bad for your health, you shouldn't ;3 |
Haha, yeah :D. I'm dealing with a file format that contains some 64-bit offsets that are only aligned to 4 bytes. It's just some prototype code to figure out the details of the format, so I'm ignoring endianness for now do a direct cast to Rust structs. |
https://docs.rs/pack1/latest/pack1/ may help you |
Thanks, this helps indeed! |
Hi, I just stumbled over:
bytemuck/src/offset_of.rs
Lines 60 to 65 in c705218
Wouldn't it be possible to do this safely using the
core::ptr::addr_of
macro to calculate the offset?The text was updated successfully, but these errors were encountered: