Include_bytes! with custom alignment?

What is the best way to use include_bytes! and give the resulting binary blob a specific alignment? For example if the blob contains a bunch of C structs, or any other number of reasons.

Is there a more clean way to achieve what I've done with this hack, beyond just hiding it with a macro?

#[repr(align(4))]
struct Wrapper([u8; include_bytes!("file").len()]);

const MY_DATA: &[u8] = &Wrapper(*include_bytes!("file")).0;

I'd imagine the double include_bytes! is not great for build times either...

1 Like

I think this is what you want: Can i conveniently compile bytes into a Rust program with a specific alignment?

7 Likes

Ah, that would work indeed! Thank you

1 Like

Yes AFAIK there still is no cleaner way than that, at least it avoids the double include_bytes.

1 Like

I've added a macro to the prior linked comment which encapsulates the construction of the static to keep things even cleaner.

(I still don't think it's big enough to be worth publishing and maintaining a crate for it, though!)

Edit: Actually, now that I'm thinking about it, maybe it's worth having a crate for, just for discoverability.

2 Likes

Thanks, just switched to that, that's perfect!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.