ratel-rust / toolshed

Arena allocator and a handful of useful data structures
Apache License 2.0
39 stars 6 forks source link

Closure-based initialization? #9

Closed Qwertycrackers closed 5 years ago

Qwertycrackers commented 5 years ago

I want to use this crate, but I can't seem to quite make the API fit my use case. I'm pulling an an unpredictable number of items into my program through I/O, and I want to place them in an arena. I can query how many items there will be, so the size of the array must be dynamic, but it will not be resized after this.

The arena has an allocate_from_slice function, but this requires that I already have a slice to copy onto the heap. I really want to initialize my objects directly into the arena.

My thinking is that this problem could be addressed with some "functional style" allocation methods on the Arena; something like alloc_from_closure<T: Copy,F: Fn(index: usize) -> T>(size: usize, source: &F) -> &'arena [T]. Similar methods could allow initialization with an interator, or a FnMut. I would be perfectly to build this functionality myself and upstream it if desired.

maciejhirsz commented 5 years ago

@Qwertycrackers would collecting an iterator into the arena allocated slice work for you?

Qwertycrackers commented 5 years ago

@maciejhirsz Yeah, that would be good. If you think it's a good idea I can write a draft implementation and submit a PR.

maciejhirsz commented 5 years ago

Sure, would love a PR. Basically the same signature you have proposed, but use Iterator instead of Fn closure. size is still necessary since we need to make sure there is enough room left in the current page, but it's cool if the iterator is shorter than the size (it's basically max capacity).

maciejhirsz commented 5 years ago

Thanks for the PR, this is now released on crates.