I've read a lot on Rust lately but am still only beginning to oxidize. My brain retains most of its C/C++ reflexes so pardon me if this question is not relevant because of how things are done in Rust.
Is it generally possible (desirable?) to allocate a block of an arbitrary size on the heap and then map a data structure over it with bindings taking ownership of smaller chunks of memory?
Using C99, one may write this:
typedef unsigned char BYTE;
typedef struct {
size_t size;
BYTE payload[];
} flex;
// ...
flex * flex_new(size_t _size) {
flex *f = malloc(sizeof(flex) + _size * sizeof(BYTE));
f->size = _size;
return f;
}
Flexible length arrays may prove useful e.g. when implementing memory pools with chunks of variable size or when allocating memory on the heap dedicated to a thread. The data structure which required size is only known at runtime is allocated in an 'atomic' fashion and its members packed contiguously.
I am wondering whether a similar Rust implementation is possible (without unsafe
constructs) and if it is, what it looks like. Maybe the compiler may infer some information using lifetime specifiers in the struct
definition?
In terms of 'flexible length arrays,' you can use
Vec::with_capacity
to allocate more than your immediate need:For the more general case, TypedArena is what you'd want to use, though.