Emulating C flexible array members in Rust


I want to create a struct that has the same in-memory layout as this C struct:

struct needed_layout {
   size_t header;
   Value scheme_values[];

This is for a Scheme interpreter. Some unsafe code that needs to be written (specifically, the garbage collector) would be simplified by this layout, and a it would also have a memory usage advantage, so I would like to achieve it.


You can write the following in Rust:

struct S {
  header : usize,
  scheme_values: [Value]

Rust language documentation generally refers to a type like this as a DST (dynamically sized type).


@eefriedman AFAIK there is no way to actually create a value of this type when the length of the array is not a compile-time constant.


You can build the value using unsafe code and transmute a reference to it (assuming you mark the struct repr(C)). Granted, it’s awkward.


But how would you actually go about boxing such a type? If it’s just an array you can use vec![...].into_boxed_slice(), but I don’t see how you could do this even with unsafe code (at least on stable, on nightly you could call liballoc directly).