[Resolved] Add `include_byte_array` built-in?


#1

I would like to use include_bytes to bundle an ELF file in my crate (to load the ELF file later). The ELF file should have a specific alignment so that it can be loaded without copying it. I also want to put it in a specific linker section.

The problem now is that include_bytes creates a byte string literal, which is a reference to a byte array (&[u8; SIZE]). Since we only have a reference, we can’t apply any attributes to the underlying array. For example, when we put it into a static and and add a link_section attribute, the attribute only applies to the reference, not to the array itself.

So how about adding an inlude_byte_array built-in that creates an array ([u8; SIZE]) instead of a byte string? This still wouldn’t be optimal, since we need to know the exact size for creating a static from it (so we would need to update it every time the extern file changes or generate the code through a build script), but it least it would be possible to apply a link_section attribute. Another possibility would be to add an include_byte_array_as_static built-in (name is just a placeholder) that additionally takes a name for the static and directly creates a static array.

The question is whether something like this is useful enough to be added. What do you think?

The current alternative is to include the bytes through a linker script, but this has the disadvantage that one has to unsafely transmute linker script symbols to access the data, which is dangerous and error prone.


#2

How about this?


#3

As for the explicit length, wasn’t there an RFC that would allow omitting it for consts and statics too?


#4

Thank you, I don’t know why I didn’t think of this :smiley:


#5

Found it: https://github.com/rust-lang/rfcs/pull/2545