Vec-like Allocator for Objects of Dynamic Size

As a part of the effort to enable scripting in shipyard, I was curious if anybody has ever written/used a crate that has a Vec-like struct that can be used to allocate things in contiguous memory, but that allows you to specify the size of the items at runtime, because I have no way of knowing the size at compile time.

I essentially need a Vec that I can store byte arrays of an arbitrary size in, so instead of running Vec::new() I would run Vec::new(size) and it would allow me to shove byte arrays/slices into the vec as long as they were size long.

Has anybody ever seen anything similar to that?

cc @leudz

There's the bumpalo crate, which looks like it might be similar.

1 Like

I don't think that bumpalo will work because:

  1. It doesn't guarantee contiguous memory when the arena overflows
  2. There isn't a Vec like interface to it that allows indexing into a collection that can contain items of a runtime-determined size

Granted the collection I'm looking for could probably be built on top of bumpalo just like they implemented Vec on bumpalo.

Ooh, I just found scroll which looks really promising.

@leudz Any thoughts on it. It looks like we could store custom components in a Vec<u8> and then use scroll to read and write the custom bytes to/from it.

Edit: actually maybe I'm just shooting in the dark again without fully understanding the needs for the project. :roll_eyes: I think I'm jumping the gun a little. I'll probably come back to this once I understand the problem a little better.